How to prevent image that has been stored not overwrite using imwrite?
이전 댓글 표시
I want to save an image from current axes directly to a folder in my computer using this code :
img = getframe(gca);
folder = 'H:\SKRIPSI\Citra Latih 2\';
filePattern = fullfile(folder, '/*.*');
ImageFiles = dir(filePattern);
a = length(ImageFiles)-1;
for Index = 1:a
baseFileName = [num2str(Index),'.jpg'];
filename = fullfile(folder, baseFileName);
imwrite(img.cdata,fullfile(filename));
When i save the first image, it could be like this :

And the i try to save the 2nd image, but it is replace my first image become like this :

How can i prevent that overwrite my saved images but still save the next image with continue numbering filename?
채택된 답변
추가 답변 (1개)
Walter Roberson
2016년 6월 14일
Your statement
imwrite(img.cdata,fullfile(filename));
is writing the same data to each of the files. You need to do the getframe() inside the loop, or else you have to get several frames outside of the save loop, storing the results in different locations, and then referencing the locations when you do the imwrite. For example,
for F = 1 : 10
img{F} = getframe();
end
for F = 1 : 10
filename = sprintf('%d.jpg', F);
imwrite(img{F}, filename);
end
Question: why are you checking the contents of the directory when you do not read files from the directory and you do not take care to ensure you are not overwriting anything in the directory?
댓글 수: 7
Alvindra Pratama
2016년 6월 14일
편집: Alvindra Pratama
2016년 6월 14일
Walter Roberson
2016년 6월 14일
You do not appear to be updating the current axes in the loop, so getframe() is going to fetch the same image each time.
Why are you fetching ImageFiles = dir(filePattern) when all you do with that information is use the number of entries as the loop index?
Alvindra Pratama
2016년 6월 14일
Walter Roberson
2016년 6월 14일
Why are you using dir() for the for looping? Why are you saving a different number of frames depending on the number of files that already exist in the directory, even though you do not care whether you overwrite any of those files? It would make some sense if you were choosing to overwrite each of the existing files, but you are not. It would also make some sense if you need to read the image files from one place and want to re-save them as numbered files, which does not even require displaying them.
Alvindra Pratama
2016년 6월 14일
Alvindra Pratama
2016년 6월 14일
natanael correia
2020년 9월 1일
man, just make a copy of the folder where the images are.
Then you run the program, it will replace all the images in the copied folder, then just throw the files together, windows will ask if you want to replace the files, why they will be in the same names, then you select who wants to keep both files,
READY!
automatically a huge amount of images will start to come together and you will have them all!
카테고리
도움말 센터 및 File Exchange에서 Image Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!