imwrite does not save file in specified folder :(

조회 수: 17 (최근 30일)
LW
LW 2019년 12월 12일
댓글: LW 2019년 12월 13일
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

채택된 답변

Matt J
Matt J 2019년 12월 12일
편집: Matt J 2019년 12월 12일
Try this,
dest=fullfile(newpath, ['new_' file(k).name(1:end-4) '.jpeg']);
imwrite(I,dest);
  댓글 수: 7
Walter Roberson
Walter Roberson 2019년 12월 12일
What shows up for
fileattrib(newpath)
LW
LW 2019년 12월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by