I have a image and some contours as bellow figure. I want to save the output into image (png or jpg). The saved image only contains the image region without the matlab window. Let see my example in the figure. Could you have me implement it by matlab? This is my code to make output figure
img = imread('coins.png');
mask_red=zeros(size(img));
mask_green=zeros(size(img));
mask_red(30:160,40:170)=1;
mask_green(70:100,60:130)=1;
imagesc(uint8(img),[0 255]),colormap(gray),axis off;axis equal,
hold on;
[c1,h1] = contour(mask_red,[0 0],'r','Linewidth',3);
[c2,h2] = contour(mask_green,[0 0],'g','Linewidth',3);
hold off;
%% Save output figure
Use the getframe and cdata idiom. If the figure is open, simply do this:
f = getframe;
im = f.cdata;
im will contain the image that was contained inside your frame as a RGB image. Running your code in your post, then the above code, then doing imshow(im), we get:
If you want to save the image, just use imwrite:
imwrite(im, 'coins_final.png');
The image will be saved in a file called coins_final.png.
Related
I'm wondering how can I save or read all the frames of the image as a image. I wrote the following code, but the result from the movie_frame(fr) is a structure while I need a image with all frames as I need it for doing block matching between different frames of the image.
Thank you very much.
I tried imwrite() which is not working, because Movie_Frames(fr) type was struct. Movie_Frames is giving me a cdata which is different with the size of the image.
clc
clear
close all
load im_sim
load info
for fr =1:size(im_sim,3);
clf;imagesc(info.x,info.y,im_sim(:,:,fr)),colormap gray, axis image,
hold on; title(num2str(fr));
pause(0.05);
Movie_Frames(fr) = getframe;
end
I am trying to save an image that I generated on a Matlab Gui. I tried following code with a push button:
[Save,savename] = uiputfile('*.bmp','Save Figure As...')
fname=fullfile(savename,Save);
imwrite(handles.axes2, 'fname', 'bmp');
It doesn't work. Can anyone please help?
imwrite is for saving image data (as a matrix) to an image file. If you want to save the figure, you will want to use saveas.
If you need to save an image of a specific axes, you can use getframe to save a screenshot of the specified axes, then convert this to an image using frame2im and then use imwrite to save this image data to a file.
frame = getframe(handles.axes2);
im = frame2im(frame);
imwrite(im, fname)
I have a couple hundred images like this one:
They are in a single folder and am trying to figure out a way in Matlab to automatically analyze each image's pixel values at point (700,755). I know how to do this one-at-a-time, as such:
rgb=impixel(p,700,755)
This returns the red-green-blue values at that particular point for the image. I'm very new to Matlab...what piece of code would analyze each image in the folder and save the RGB value on separate lines in a table/array?
Also, I have selected an area using the pixel region tool: '[696.463836477986 750.095011390851 19.9889937106933 13.3672527600921]' How do I analyze all the pixel values in that area and get the statistics (min, max, mean, etc.)...plus do that for all 200 images I have in the folder?
I appreciate the help!
AP
impixel can work for all the image as well:
impixel(I)
or for a specific (pixel) column and row:
impixel(I,c,r)
But you need first to read the image into a matrix. The imread function, returns all the RGB data of an image in an array:
A = imread(filename, fmt)
it reads a grayscale or color image from the file specified by the string filename. A is an array containing the image data. If the file contains a 'grayscale' image, A is an 'M-by-N' array. If the file contains a 'truecolor' image, A is an 'M-by-N-by-3' array. (3: R-G-B)
To read a bunch of files in a folder do this:
files = dir('*.jpg');
for i=1:length(files)
eval(['imread ' files(i).name]);
end
You can use imcrop function to crop the images you have:
using mouse:
I2 = imcrop(I)
or using dimensions:
I2 = imcrop(I,[75 68 130 112]);
I use the below command to display the image
imshow(img,[]);
when i use the following command to save the image it is saved as an empty white page
imsave;
how to save the image in this case any command would do
You are probably running into an issue with matrix type and range. If img is type double it needs to be scaled between 0 and 1.
A common issue is to load an image in uint8 (scaled between 0 and 255), convert to double in order to do some processing on it, without scaling, and then try and save it out. When you do that, MATLAB tries to convert back to uint8, and any values in the image outside the [0 1] range are clipped. On many images this means that the file comes out all white.
To get around this, use functions like im2double and im2uint8 rather than just double or uint8 when converting images.
Try at the command line the difference between:
img = imread('pout.tif');
img = double(img);
imshow(img,[]);
imsave;
and
img = imread('pout.tif');
img = im2double(img);
imshow(img,[]);
imsave;
Convert image data into an actual image and try again:
h = image(img); %Convert to object
imsave(h); %Save image object
Notice that if you close the figure window generated by image(), the object is deleted and the handle has will point to nothing. Though this may be beyond of what you are asking for.
Hope this adjustment solved your problem
First convert the image to rgb using
img1=label2rgb(img);
then again convert the image into an gray image using
img2=rgb2gray(img1);
then u can use imshow to show the image and save it using imsave
imshow(img2);
imsave();
In MATLAB, how do you write a matrix into an image of EPS format?
It seems imwrite does not support EPS.
Convert is not working on the Linux server I am using:
$ convert exploss_stumps.jpg exploss_stumps.eps
convert: missing an image filename `exploss_stumps.eps' # convert.c/ConvertImageCommand/2838
Why?
I tried gnovice's idea under terminal mode:
figH = figure('visible','off') ;
imshow(img,'border','tight',... %# Display in a figure window without
'InitialMagnification',100); %# a border at full magnification
print(strcat(filepath,'/', dataset,'_feature_',num2str(j), '.eps'),'-depsc2');
close(figH) ;
However I got:
??? Error using ==> imshow at 191
IMSHOW requires Java to run.
Error in ==> study_weaker at 122
imshow(img,'border','tight',... %# Display in a figure window without
191 error(eid,'%s requires Java to run.',upper(mfilename));
How can I fix it?
One possible solution is to plot your image using IMSHOW, then print the entire figure as a .eps using PRINT:
img = imread('peppers.png'); %# A sample image
imshow(img,'Border','tight',... %# Display in a figure window without
'InitialMagnification',100); %# a border at full magnification
print('new_image.eps','-deps'); %# Print the figure as a B&W eps
One drawback to this solution is that if the image is too big to fit on the screen, IMSHOW will shrink it to fit, which will reduce the on-screen resolution of the image. However, you can adjust the final resolution of the saved image using the -r<number> option for the PRINT function. For example, you can print your figure as an Encapsulated Level 2 Color PostScript with a resolution of 300 dpi by doing the following:
print('new_image.eps','-depsc2','-r300');
EDIT: If you are unable to use IMSHOW (either because you don't have the Image Processing Toolbox or because you are using a MATLAB mode that doesn't allow it), here is an alternative way to create and print the figure:
img = imread('peppers.png'); %# A sample image
imagesc(img); %# Plot the image
set(gca,'Units','normalized',... %# Set some axes properties
'Position',[0 0 1 1],...
'Visible','off');
set(gcf,'Units','pixels',... %# Set some figure properties
'Position',[100 100 size(img,2) size(img,1)]);
print(gcf,'new_image.eps','-depsc2','-r300'); %# Print the figure
You can also take a look at this documentation to see how printing works without a display.
It should work using imwrite. You would have to add a colormap for it to work though.
However, ckecking the help pages I see that it is NOT possible to use imwrite to write an EPS file.
Following code may help you to convert png file to eps.
fileName = 'FarmerStats'; % your FILE NAME as string
A = imread(fileName,'png');
set(gcf,'visible','off') %suppress figure
image(A);
axis image % resolution based on image
axis off % avoid printing axis
set(gca,'LooseInset',get(gca,'TightInset')); % removing extra white space in figure
saveas(gcf,fileName,'epsc'); % save as COLOR eps file