how to avoid overwriting the images while using imwrite() ?

5 views (last 30 days)
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...

Answers (2)

dpb
dpb on 2 May 2014
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 Comments
Manoj Kumar
Manoj Kumar on 2 May 2014
Can you please tell me what other alterations can i give to that. I will use this in other GUI doing other operations apart from gray scale and black& white.
thank you...
dpb
dpb on 2 May 2014
...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?".

Sign in to comment.


Image Analyst
Image Analyst on 2 May 2014
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 Comments
Manoj Kumar
Manoj Kumar on 2 May 2014
i will get the input file like this
axes(handles.axes1);
global im
global baseFileName
global folder
[baseFileName,folder]=uigetfile('*'); % read the input image
im=imread([folder,baseFileName]);
Please help me guys...
Image Analyst
Image Analyst on 2 May 2014
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.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!