how to save imageof axes for high resolution in matlab GUI - matlab

I trying save image with the comand 'getframe' but resolution is very low, so I can not give zoom. for now I'm using the code:
[arq,dir] = uiputfile('*.jpg','Output Files');
fileName=fullfile(dir,arq);
f=getframe(handles.axes1);
[x,map]=frame2im(f);
imwrite(x,fileName,'jpg');
I need save in jpg and also need save the label (x,y) in graph. How do ? There is another command better than the getframe??
Print is a good command?
Thanks

try using something other than jpg, it doesn't scale well. eps is a good choice as it is a vector format.

Try exportfig from FEX: exportfig

Related

Copy figure in Matlab

I am asking for a solution to the blurred image I get when I use copy figure option in Matlab. For example, when I plot 3d figure in matlab I get the following image (It is the result of printscreen!)..
However, when I use the option copy figure I get a blurred image as the following image
.
How can I avoid this as I need to get high resolution image using copy figure option. Thank you in advance for any help you may lend.
By default, the figure is copied as an Enhanced Metafile (a color graphics format) which works for basic plots such as bar plots, line plots, and other 2D plots but may yield undesirable results for more complex plots as in your case. This is because the rendering of the Metafile content is controlled my Microsoft Word and it may render things differently than MATLAB.
If you need a high resolution image, I would recommend using the print command where you can specify the desired resolution (using the -r option). Then you can import the resulting image into Word
print(gcf, 'myfile.png', '-dpng', '-r300')
Alternately, you can use export_fig from the File Exchange which better preserves the appearance
Another option would be to change the format used by Copy Figure to tell it to use a bitmap instead
Or you can call Copy Figure programmatically and specify a different format (either a bitmap or PDF)
print(gcf, '-clipboard', '-dpdf')

Save image from imshow

I want to save the image to a file after doing imshow(im,[]); to display it later in GUI. I am trying the following code, but it doesn't work.
New= imshow(uint8(MHI{t}),[]);
imwrite(New,'TMHI.jpg','jpg')
Any help will be appreciated. Thank you.
The imshow function is only used to show the image in MATLAB. If you want to save it, you don't need the imshow at all. And: the value (New) returned by imshow() is the handle to the figure. You need that handle if you want to modify how the figure is shown on the screen.
To write the image to the disk, you only need the imwrite function, which has the syntax:
imwrite(A,filename)
where A is the image array.
If the file name ends with .jpg, then MATLAB will create a JPEG image by default, so you don't need to specify that. (But of course, you still can.)
But before saving: you have a problem with the normalization of the image. MATLAB assumes that a double image is scaled to [0,1] and that a uint8 image is scaled to [0,255]. With imshow(im,[]) you override these defaults and make MATLAB calculate new values. You will experience the same problem when saving. The solution is to normalize the image properly. This can be done using the im2uint8 function, which scales the input to a maximum value of 255, and converts it to uint8. Note that you'll have to remove the minimal value manually, if that is needed:
newImage = im2uint8(MHI{t} - min(MHI{t}(:)));
imwrite(newImage,'TMHI.jpg')
In case you really need to save the contents of the displayed figure in matlab (sometimes also useful when you use imagesc for display as it has some smart logic for properly scaling your value ranges) you might be interested in the savefig and saveas which lets you save the contents of a figure. Its also possible to save graphs or figures with subfigures like that.
In that case, you would use something like:
F = imshow(uint8(MHI{t}),[]);
saveas('MHI.png');
In case you really just need to save the image stored in MHI{t}, hbaderts 's answer is the way to go...
Just use my NormalizeImage function and save the image normaly:
img = NormalizeImage(imgDouble);
imwrite(img ,'MyImage.png');
My NormalizeImage function:
function img8bpp = NormalizeImage(imgDouble)
minImgDouble = min(imgDouble(:));
factor = (255-0)/(max(imgDouble(:)) - minImgDouble);
%img8bppB = im2uint8(imgDouble-minImgDouble);
img8bpp = uint8((imgDouble-minImgDouble).*factor);
%im2uint8 does not work, duno y
%imgDif = img8bppB - img8bpp;
end

Distortion while saving matlab figure as a eps file

saveas(gcf,'result.pdf');
After I using above command to output my figure into a eps file, I got the following result.
However, the actual figure is like the following.
I have tried using other commands like
saveas(gcf, 'result.eps');
print -depsc myplot.eps
or even the 'export_fig' library,but still can't get correct figure output.
Does anyone know where is the key point of this problem? Thanks.
[ Update ]
Following dpwe's comment, after zooming in the figure, the result of .eps is like this
and the original figure is like this
Yes, it seems that they are much more similar!
The saveas function uses a default size for saving figures, I think it's something like 3/4 of your screen resolution. You can figure it out by looking at the number of pixels in the image (for a png anyway). If you run
set(gcf,'Position',[a b c d]);
saveas(gcf, 'result.eps');
to resize the figure to the size that saveas will use to save it before it saves it, that might help.

Matlab imwrite() quality

I'm very new to Matlab, though I know a few other programming languages, so please forgive me if this is something simple. I have not been able to find any answers to this, either on StackOverflow or elsewhere.
I produce a figure using the following code:
figure(6),imageplot(P); drawnow;
Which looks like this:
I then save this image to my computer using the following commands:
imwrite(P, 'images/plot.png');
And the resulting image is tiny, and missing some of the color information:
If, however, I utilize the save function in the open figure (image #1) and save it manually, I get exactly what I want, which is that exact image stored on my computer.
How would I program that? I assumed that imwrite() would just write the image directly, but apparently I'm doing something wrong. Any advice? Perhaps it has something to do with the imageplot command? I cannot seem to get that to work in imwrite.
Update: Based on the comments below, I have begun using "imresize" with the "nearest" option. This scales the image properly, but the resulting image is still curiously darker (and therefore has less information) than if I hit the "save" button in the figure.
Image saved from figure:
Image using "imresize" with "nearest" option:
The MATLAB imwrite command saves exactly the number of pixels as specified in your image matrix. This is the actual result of your computation; the reason the output is "tiny" is because it is supposed to be. To make it larger, would be to simply scale/zoom it as required.
The save figure option however does something quite different: it rasterizes the the output you obtain in the figure window and gives you the option for saving it as an image. This is evident in the fact that when you do so, you obtain a white background in addition to your result which is really just the grey background you see before you save it; this can be adjusted by resizing the figure window before utilizing the save option.
If you're looking to simply make the output figure larger, I would recommend using something along the lines of the imresize command.
Say, if you want the default size to be twice the size of the real result, simply use:
imresize(P, 2.0);
For more options, try help imresize.
The command you need for the "Save As..." functionality of figures is called "print". I often use
print(gcf, '-dpng', 'some_filename.png')
or
print(gcf, '-depsc', 'some_filename.eps','-r0')
to save a figure as it is shown on screen. The format png offers a small filesize and excellent quality, and it is understood by most image viewers and browsers. The eps format is a vector format, which I use for printig. The '-r0' option specifies "use the same size as given by the screen resolution" for the vector format properties.

how save image with better or specificresolution in gui matlab?

I trying save image with the command getframe but resolution is very low, so I can not give zoom.
For now I'm using the code:
[arq,dir] = uiputfile('*.jpg','Output Files');
fileName=fullfile(dir,arq);
f=getframe(handles.axes1);
[x,map]=frame2im(f);
imwrite(x,fileName,'jpg');
I need save in JPG and also need save the label (x,y) in graph. How can I do this?
There is another command better than the getframe?