imwrite does not save file in specified folder :(

16 views (last 30 days)
Hi all,
I have searched the internet and Matlab Answers website and could not find an answer that worked for my problem, so I am asking here.
I am color-correcting the white balance of a jpeg image using the code below. I use the Image Processing Toolbox functions imread to open the raw image and imwrite to save the new color-corrected image (but with a slighty different name) in the same folder.
However, when I run the code and check the folder, no new files have been created. I tried re-defining the folder path within the for loop, and then within the imwrite function itself, but neither has worked. I also tried saving the image as a .jpg or .png or saving with the same name into a new empty folder, but these didn't work either.
I don't know why imwrite is not creating a new file, and any help would be much appreciated!!
newpath = 'C:\Users\551\Documents\raw-data\';
userpath(newpath)
file = dir('1.1-ERG-T2.jpeg');
for k = 1:length(file)
I = imread(file(k).name);
w1=3440:3500; w2=554:742;
R = I(w1,w2,1);
R = reshape(double(R),size(R,1) * size(R,2),1);
G = I(w1,w2,2);
G = reshape(double(G),size(G,1) * size(G,2),1);
B = I(w1,w2,3);
B = reshape(double(B),size(B,1) * size(B,2),1);
I(:,:,1) = double(I(:,:,1)) + 255-floor(mean(R));
I(:,:,2) = double(I(:,:,2)) + 255-floor(mean(G));
I(:,:,3) = double(I(:,:,3)) + 255-floor(mean(B));
imwrite(I,['new_' file(k).name(1:end-4) '.jpeg']); % add "new_" in the filename to avoid replacing raw image
end

Accepted Answer

Matt J
Matt J on 12 Dec 2019
Edited: Matt J on 12 Dec 2019
Try this,
dest=fullfile(newpath, ['new_' file(k).name(1:end-4) '.jpeg']);
imwrite(I,dest);
  7 Comments
LW
LW on 13 Dec 2019
Thanks for the additional thoughts. I mulled over everything and finally found the source of the problem! You are right it has nothing to do with imwrite itself. It is a matter of considering these three things:
1) the working directory of the code
2) the folder containing the raw images
3) the folder outputting the color-corrected images (which is a sub-folder of the raw image folder)
Even when I correctly specify the output path with
dest=fullfile('C:\Users\551\Documents\raw-data\color-corrected', ['new_' file(k).name(1:end-4) '.jpeg']);
imwrite(I,dest);,
imwrite does not output anything. I found that the code was attempting to locate this file path through the code's working directory (which is inside a completely separate file path within C:\Users\551\Documents\). I moved the code file to the Documents\ level and everything works.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!