Save image using Guide GUI does not work

When trying to save an image from my GUI I get the error:
-----------------------------------------------------------------------------
Error using imwrite (line 442)
Expected DATA to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical
Instead its type was matlab.graphics.axis.Axes.
-------------------------------------------------------------------------------------------
I want to save an image that I modified, where I added squares and/or other images (see image). This is the code I am using:
first_c= 'C:\Users\';
user_name= getenv('username');
second_c='\Desktop';
com_path= fullfile(first_c,user_name,second_c);
a= handles.axes1;
defaultFileName = fullfile(com_path, '*.png*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName);
imwrite(a, fullFileName);
--------------------------------------------------------------------------------------
Can someone please help me? It is for an important project.

 Risposta accettata

You put some variable into the axes. So you don't need to get that variable back out of that axes -- you can jsut use the same variable you loaded into it. If you did want to get the image out, you'd use getimage()
displayedImage = getimage(handles.axes1);
% Then create fullFileName. Then save.
imwrite(displayedImage, fullFileName);
But that won't get any graphics you drew over it in the overlay (lines, etc.). To get that you can use exportgraphics().

7 Commenti

Thank you for the help! I still get this error with the code :(
----------------------------------------------------------------------------------
Error using imwrite (line 448)
Unable to determine the file format from the file name.
Error in GUI>pushbutton3_Callback (line 441)
imwrite(displayedImage, fullFileName);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI (line 20)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
----------------------------------------------------------------------------------
I am using this code:
first_c= 'C:\Users\';
user_name= getenv('username');
second_c='\Desktop';
com_path= fullfile(first_c,user_name,second_c);
displayedImage = getimage(handles.axes1);
defaultFileName = fullfile(com_path, '*.png*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName);
imwrite(displayedImage, fullFileName);
I think the user did not enter an extension or just put a dot. Here is some more robust code. It works - I tested it. If you enter a different extension, make sure it's one that shows up when you type imformats on the command line.
%---------------------------------------------------------------
% Get the output folder name.
cUsersFolder = 'C:\Users\';
user_name = getenv('username');
secondFolder = '\Desktop';
outputFolder = fullfile(cUsersFolder, user_name, secondFolder);
% Get the image in the axes.
displayedImage = getimage(handles.axes1);
% Get the output base file name.
defaultFileName = fullfile(outputFolder, '*.png');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
% If they ended the name with a dot but not extension, it will automatically add .png so you'll have two dots
% like abs..png. Get rid of any double dots.
baseFileName = strrep(baseFileName, '..', '.');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
% Make sure it has an extension.
[f, baseFileNameNoExt, ext] = fileparts(baseFileName);
if isempty(ext)
% If not extension provided by the user, make it PNG format.
fullFileName = fullfile(folder, [baseFileNameNoExt, '.png']);
else
% User provided an extension.
fullFileName = fullfile(folder, baseFileName);
end
fprintf('Writing %s.\n', fullFileName);
imwrite(displayedImage, fullFileName);
Thanks a lot for the help!! now it saves, but not the whole edited picture, just the backround image... (See backround image)
Here is the code used for the editing of the picture:
%Load backround
I1= imread('backround.png');
imshow(I1);
% Pixels: x-> 3200 & y-> 2400
hold on
%Parameters Points
x_end_first= 100;
y_end= 2400;
x_end_second= 80;
points1= 400;
point2= 700;
point_size1= 50;
point_size2= 50;
x_end_first= 100;
y_end= 2400;
x_end_second= 80;
points1= 400;
point2= 700;
point_size1= 50;
point_size2= 50;
%-------------------------------
rectangle('Position',[1 1 x_end_first y_end],'FaceColor','w','EdgeColor',[1 1 1]) %First
rectangle('Position',[x_end_first 1 x_end_second y_end],'FaceColor','w','EdgeColor',[1 1 1]) %Second
%Area 1
x=rand(1,points1)*x_end_first;
y=rand(1,points1)*y_end;
sz = linspace(1,point_size1,points1);
scatter(x,y,sz,'filled','MarkerFaceColor',[0 .7 .7])
%Area 2
x=rand(1,point2)*x_end_second+x_end_first;
y=rand(1,point2)*y_end;
sz = linspace(1,point_size2,point2);
scatter(x,y,sz,'filled','MarkerFaceColor',[0 .7 .7])
hold on
Then I implemented your code to save.
If you want to save graphics in the overlay also, such as rectangles, you'll need to use exportgraphics().
It works!!
I still had to change:
displayedImage = getimage(handles.axes1); ---> displayedImage = handles.axes1;
Now it saves the whole grapic. Thanks a lot for the help :)
Yes. You're welcome. To explain...
If you want to use exportgraphics() you pass the handle to the figure or axes.
If you want to use imwrite() you need to pass the image itself, not the handle to the figure or axes.
The help documentation will tell you what any function expects for its arguments.
All clear, thanks!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Convert Image Type in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by