Save exact image output from imagesc in matlab
Asked Answered
C

3

7

Rainbow

Hi , I want to save this image produced from imagesc(magic(3)), the exact rainbow representation, is it possible?

Thanks.

This question might look like a duplicate , but it is not . I looked at the solution to the similar question at this site , but it did not satisfy me . I have looked into the Matlab help center and the close answer that I got was this one , at the bottom of http://goo.gl/p907wR

Cobweb answered 16/8, 2013 at 4:2 Comment(8)
Is saveas(gcf,'filename','format') what you want? Try a vectorized format as .epsBulldozer
@Werner: Thanks for answering , could you please elaborate .Cobweb
@Werner: How do I get rid of the axes ?Cobweb
What do you mean the axes? You dont want the ticks? Do set(gca,'XTick',[]) set(gca,'YTick',[]). I think you may want to set the background as white also: set(gcf,'Color','w')Bulldozer
That will be it , for now .Cobweb
possible duplicate of Plotting and Saving as File in MATLABBulldozer
@Werner: Do you really have to say whether this is a duplicate or not ; nice of you that you pointed .I am adding your reply as a possible answer .Cobweb
Well, I am not saying that you didn't research before your question, just that you were unfortunate. Anyway, if people decide that this is not a duplicate, let's facilitate the things x)Bulldozer
B
14

To save the figure as a file (don't matter how it was created), one should do:

saveas(figureHandle,'filename','format')

where figureHandle could be the gcf handle, which means: get current figure.

As pointed in the discussion, if someone doesn't want the ticks to be shown, the person can add:

set(gca,'XTick',[])
set(gca,'YTick',[])

where gca is the handle to the current axis, just as gcf. If you have more than one axis, don't forget to "handle the handles". They are returned to you when you create them, i.e.:

hFig = figure(pairValuedProperties); % Create and get the figure handle
hAxes1 = suplot(2,1,1,pairValuedProperties); % Create and get the upper axes handle
hAxes2 = suplot(2,1,2,pairValuedProperties); % Create and get the bottom axes handle

where the pair value are the figure or axes properties declared in the following syntax:

'PropertyName1',PropertyValue1,'PropertyName2',PropertyValue2,…

Here are the matlab documentation about the Figure and Axes Properties, and about the saveas method.


Example:

The image saved with the following code:

figure 
imagesc(magic(3))
set(gca,'XTick',[]) % Remove the ticks in the x axis!
set(gca,'YTick',[]) % Remove the ticks in the y axis
set(gca,'Position',[0 0 1 1]) % Make the axes occupy the hole figure
saveas(gcf,'Figure','png')

Figure without the white border

Bulldozer answered 16/8, 2013 at 15:35 Comment(6)
Well I couldn't avoid the ticks from being shown in my image , and there was a white border around the image as a frame , which is also I didn't want . But I am satisfied that my I have a plausible answer to my schools assignment .Thanks again .Cobweb
@MotiurRahman Added an example. Beware that the gca changes if you plot another figure or axes.Bulldozer
Hugs for the effort , your example works , but I am in Archlinux and nautilus is framing an white border around the image , even though I am not getting the axis right . Thing is I have to do an average filtering of this image , and the white border( which can not be recognized , because the webpage is white ) is really messing up my image( which is actually a grainy image , not this 3x3 image above).So I have to resort to median filtering , which is okayish, but average filtering would have been better .Cobweb
My image also have the white border. To remove it, make the axes occupy the hole figure: set(gca,'Position',[0 0 1 1])Bulldozer
Haha , you are just relentless , finally I am happy .Cobweb
Congratulations on your intervention. It was of immense relevance for our work. Just one note, please try to provide information regarding the imagesc(magic(3)) code line.Grahamgrahame
D
4

You can use:

print -djpeg99 'foo.jpg'

This will save it as 'foo.jpg' as you need.

Dishearten answered 24/10, 2013 at 11:48 Comment(0)
F
2

You can use the following code

 imagesc(A);
 %%saving the image
 hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg');
 set(gcf,'PaperUnits','inches','PaperPosition',[0 0 4 4]);
 print -djpeg filename.jpg -r10

Here A will be the matrix from which you will have an image. And the image will be saved as filename.jpg in the directory.

Farmstead answered 7/4, 2017 at 4:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.