Cutting part of image and saving to file in MATLAB - 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.

Related

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

Saving GUI as .jpg in matlab

In matlab, I have created a GUI with several plots and edit boxes filled with text for an experiment with the bike. These plots and edit box values vary depending on the data I input (120 different sets). I was able to save the GUI as a figure with
saveas(handles.speedbike,fullfile(savename), 'fig');
Speedbike is the handle I gave to one of the plots and savename is the name that the figure is saved under, which changes with each set.
Now I also wish to save all the sepparate GUI's as jpegs, but using the same code as above, but with 'jpg' instead of 'fig' only saves a small corner of the figure as a jpeg, not the whole GUI.
Is there any function that I can use to correctly save a gui as a jpeg, or any way to open a .fig file, and then saving a copy of that as a jpeg.?
You need to use the handle of the whole figure for your GUI, instead of the handle of one of the plots. You probably also want to use print for saving to jpg.
Ive found satisfactory results with this:
hg=get(0,'Children');
saveas(hg,'.\gui.fig','fig');
optsave.Format='png';
hgexport(hg,'.\gui.png',optsave);
Why this do not work?. This normally do with normal figures, and failed with GUI's:
optsave.Format='meta';
hgexport(hg,'.\gui.wmf',optsave);

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?

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

image save in matlab

I have an image and after drawing some features(ellipses and text) on it I want to save it as JPEG.
h= figure(1);
imagesc(im_name);
colormap('gray');
hold on
for i=1:no_of_points;
//draw features and write some text
end
hold off
imsave (h);
I am getting a figure with features drawn on it but when I save it, it is an image (which is my orignal image 'im_name') without new features on it.
I tried also
.
.
.
imsave (h);
hold off
Thanx in advance for your help.
Maybe you should try the function saveas
saveas
Save figure or Simulink block diagram using specified format
Alternatives
Use File > Save As on the figure window menu to access the Save As
dialog, in which you can select a graphics format. For details, see
Exporting in a Specific Graphics Format in the MATLAB Graphics
documentation. Sizes of files written to image formats by this GUI and
by saveas can differ due to disparate resolution settings. Syntax
saveas(h,'filename.ext') saveas(h,'filename','format')
Description
saveas(h,'filename.ext') saves the figure or Simulink block diagram
with the handle h to the file filename.ext. The format of the file is
determined by the extension, ext. Allowable values for ext are listed
in this table.
You can pass the handle of any Handle Graphics object to saveas, which
then saves the parent figure to the object you specified should h not
be a figure handle. This means that saveas cannot save a subplot
without also saving all subplots in its parent figure.
When using the saveas function the resolution isn't as good as when manually saving the figure with File-->Save As..., It's more recommended to use hgexport instead, as follows:
hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg');
This will do exactly as manually saving the figure.
Source.