필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Manoj Kumar
Manoj Kumar 2014년 5월 2일
댓글: dpb 2014년 5월 2일
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...

답변 (2개)

dpb
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
Manoj Kumar
Manoj Kumar 2014년 5월 2일
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 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
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
Manoj Kumar
Manoj Kumar 2014년 5월 2일
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 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 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