Unable to save image using a SAVE button

조회 수: 8 (최근 30일)
Warid Islam
Warid Islam 2021년 5월 14일
댓글: Warid Islam 2021년 5월 18일
I have created a matlab GUI where I want to save the processed images using a SAVE button. I am using a slider where I can browse through the processed images. I want to save each image with the SAVE button. Unfortunately, the images are not saved in the correct format(I want to save it in the jpeg format). Any suggestions would be appreciated.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
filePattern=handles.filePattern;
num_files=handles.num_files;
theFiles=handles.theFiles;
myFolder=handles.myFolder;
value=handles.value;
seg=handles.seg;
% dirName = fullfile(pwd,'Results',myFolder);
% if ~exist(dirName,'dir')
% mkdir(dirName);
% end
[filename, foldername] = uiputfile('D:\regionGrowing_MLT');
complete_name = fullfile(foldername, filename);
imwrite(seg{value}, complete_name,'jpg');
guidata(hObject, handles);
end
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 5월 15일
The implication is that when you do the [filename, foldername] = uiputfile('D:\regionGrowing_MLT'); that the user is giving you a name without any file extension, and you are not appending an extension.
Your file j should be a jpeg file, considering the way you created it.
Warid Islam
Warid Islam 2021년 5월 15일
Your suggestions worked. Thanks a lot.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2021년 5월 15일
A more robust way is this:
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = 'D:\regionGrowing_MLT'; % Or "pwd" or wherever you want.
if ~isfolder(startingFolder)
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.jpg');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
% If they entered an extension, throw it away because (for some reason)
% we want to force it to be JPG format, not whatever other format they may have entered.
[~, baseFileNameNoExt, ext] = fileparts(baseFileName)
fullFileName = fullfile(folder, [baseFileNameNoExt, '.jpg'])
% Now save the JPG image.
imwrite(seg{value}, fullFileName);
Of course you're better off using PNG format than the lossy JPG which often shows bad compression artifacts.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by