how to avoid overwriting the images while using imwrite() ?
4 次查看(过去 30 天)
显示 更早的评论
When I use imwrite() in the GUI to save the image. At first I convert the image to grayscale and save the image. when I convert to black and white and click save, it is overwriting the first image.
The code is given below for save the image push button:
axes(handles.axes2);
f=getframe(handles.axes2);%select axes in GUIfigure(); %new figure
[im3] = frame2im(f);
[~,baseFileName,extension]=fileparts(baseFileName);
outputBaseFileName = sprintf('%s_converted.tif', baseFileName); % to save the image with the original file name + converted
outputFullFileName = fullfile(folder, outputBaseFileName);
imwrite(im3, outputFullFileName);
Can you please help in how to avoid the overwriting the image?
Thanks...
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/162033/image.jpeg)
0 个评论
回答(2 个)
dpb
2014-5-2
outputBaseFileName = sprintf('%s_converted.tif', baseFileName); % original file name + converted
Add some other changing information to the filename as well--you've created just a static name with one alteration from the original, but that's not unique. Typical "tricks" are to add a sequential number or a timestamp or somesuch. Or, if you only doing B&W and/or GS on the base name for a given file, just use some variation on that for the two cases.
2 个评论
dpb
2014-5-2
...Typical "tricks" are to add a sequential number or a timestamp or somesuch...
Pick something and run with it...it would be a good thing (tm) if there were some reason behind the selection so the user has some idea of what the file(s) are going to be, but it can be anything just so it's unique to avoid the overwrite.
Say you've got a revision/modification number, modNo, that you increment each time the user goes to save. Then something like
modNo=modNo+1;
outputBaseFileName = sprintf('%s_conv_mod%04d.tif', baseFileName, modNo)
will generate a sequence _0001, _0002, etc., each time thru.
Of course, this still isn't necessarily unique if the user starts over again and you haven't checked for that...in that case you might consider asking if want to overwrite. That's the challenge in writing user-friendly, robust code; all these little nitty details to take care of neatly and with reasonable choices of "how?".
Image Analyst
2014-5-2
You need to make sure that baseFileName is the correct name when you go to save the image. If it's set somewhere else, in some other function (like in the callback of a button that calls uigetfile), then see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
2 个评论
Image Analyst
2014-5-2
Whatever function you're changing baseFileName in needs to have it declared global also or else it will be just a local variable in that function and vanish when the function exists and thus not be updated in your other functions that do have it declared global.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!