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

5 Ansichten (letzte 30 Tage)
Manoj Kumar
Manoj Kumar am 2 Mai 2014
Kommentiert: dpb am 2 Mai 2014
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...

Antworten (2)

dpb
dpb am 2 Mai 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 Kommentare
Manoj Kumar
Manoj Kumar am 2 Mai 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 am 2 Mai 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?".

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 2 Mai 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 Kommentare
Manoj Kumar
Manoj Kumar am 2 Mai 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 am 2 Mai 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by