0
votes

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

enter image description here

1
Do you want the result on the left or the right? Try print and check its documentation. - David
I want to result in right side. The left side is output figure that made by above code - john2182

1 Answers

2
votes

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:

enter image description here

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.