imwrite does not save file in specified folder :(

5 visualizzazioni (ultimi 30 giorni)
LW
LW il 12 Dic 2019
Commentato: LW il 13 Dic 2019
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

Risposta accettata

Matt J
Matt J il 12 Dic 2019
Modificato: Matt J il 12 Dic 2019
Try this,
dest=fullfile(newpath, ['new_' file(k).name(1:end-4) '.jpeg']);
imwrite(I,dest);
  7 Commenti
LW
LW il 13 Dic 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by