imwrite does not save file in specified folder :(

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

Thank you very much for the suggestion, but it still doesn't produce the new image.... I first replaced my imwrite line with your two lines. When that didn't seem to work, I then tried different modifications to the inputs of fullfile(), but none were successful.
I wonder if there is something inherently wrong with the way I am specifying the folder path and/or if imwrite is not the best file-saving function for my code.
Maybe there are write permissions on your folder that are blocking files from being created...? Dunno. The code should work if the file path exists.
How are you checking whether the files were created? You should try,
>>ls(newpath)
I'm running as the administrator and checked that I have read/write access to the folders I'm using, so I don't think permissions are the issue.
I did use ls(newpath), which returned an empty list, and I used the command window of my operating system, which also returned an empty list. In Matlab, when I try to call the new file with dir() or imread(), I still get "No such file or directory."
I've started experimenting with other functions that prepare images for saving, like Image = getframe(gcf), but I'm trying to avoid loss in pixel resolution/information.
I really appreciate your help thus far. Do you have any other ideas?
Matt J
Matt J il 12 Dic 2019
Modificato: Matt J il 12 Dic 2019
Try your code on a different computer (to see if it's a machine-specific problem)?
I think you're barking up the wrong tree trying to pursue alternatives to imwrite. There's no reason it shouldn't be working, or at least giving you an error message.
One other thing. This is definitely not the way to direct the output of imwrite to a particular folder,
userpath(newpath)
If the destination folder is not the folder where your code is executing, you need to specify the output file with its full pathname, using fullfile or something similar.
What shows up for
fileattrib(newpath)
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 Centro assistenza e File Exchange

Richiesto:

LW
il 12 Dic 2019

Commentato:

LW
il 13 Dic 2019

Community Treasure Hunt

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

Start Hunting!

Translated by