how save image with better or specificresolution in gui matlab? - 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?

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

Cutting part of image and saving to file in MATLAB

Lets assume i have image 'image.jpg'.
I want to choose part of it with imrect function, and save to new file 'newimg.jpg'.
Do you have any idea how to implement in a simple way?
Thanks.
Use the imcrop function to cut out the region, and then imwrite to save it into a separate image.

how to save imageof axes for high resolution in matlab GUI

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

Saving contents of figure to a matrix in Matlab ( whithout using getframe, nor with saving to file)

The function getframe captures whatever is visible on the screen. However in my application I want the figure to be invisible when storing its content in a matrix. So what the getframe does is that, for a short period, it makes the figure visible and captures its contents and then sets the 'visibile' property back to what it was before screen capture.
I do not want that flash happening on the screen. As well, saving in file and reading it back reduces speed. There has got to be a way to get around this.
hFig=figure('Visible','off'')
text ('String','ABC','fontsize',300)
imageData = getframe(hFig);
img = imageData.cdata; % img is what I am interested in
The only way I know to do this is to print the figure to a temporary file. For example:
%Create a figure
hFig=figure('Visible','off')
text ('String','ABC','fontsize',300)
%Save the figure to a tiff file using a salted name
tmpName = sprintf('Temp_Figure_%04d.tiff', floor(rand*1000));
print(['-f' num2str(hFig)], '-dtiff', tmpName)
%Read the data
img = imread(tmpName);
%Delete the temporary figure
delete(tmpName);
Not the prettiest thing, but it seems to work.