简体   繁体   中英

Convert a plot with ellipses to binary image in MATLAB

This is my first question here and a I have a plot in matlab, with some ellipses. I want to convert this plot to a binary image. Can someone help me?

The image with the ellipses is shown here -

在此处输入图片说明

Thanks in advance for any help!

Code

%%// ---- Your Plot done until this point

%%// Remove frames
set(gca, 'visible', 'off')
set(gcf, 'color', 'w');

%%// Get the figure as a uint8 variable
im = export_fig;

%//  Output binary image
BW = ~im2bw(uint8(255.*im2double(im)),0.99);

Note: You need to get export_fig and the related functions from here .

Sample case 1

h = figure()
plot(1:10);

%%// ---- Your Plot done until this point

%%// Remove frames
set(gca, 'visible', 'off')
set(gcf, 'color', 'w');

%%// Get the figure as a uint8 variable
im = export_fig;

%//  Output binary image
BW = ~im2bw(uint8(255.*im2double(im)),0.99);

figure,imshow(BW)

Output

在此处输入图片说明

Sample case 2 with extended feature

You can perform binary skeletonization with bwmorph , to keep the edge width as 1 , which is done in this sample case.

Code

figure,
hold on

x1=-2;y1 = 0;x2=2;y2=0;
e = 0.6;
[x,y] = ellipse1(x1,y1,x2,y2,e);
plot(x,y,'b-')

x1=-15;y1 = 4;x2=-5;y2=3;
e = 0.95;
[x,y] = ellipse1(x1,y1,x2,y2,e);
plot(x,y,'b-');

%%// Remove frames
set(gca, 'visible', 'off')
set(gcf, 'color', 'w');

%%// Get the figure as a uint8 variable
im = export_fig;

%//  Output binary image
BW = ~im2bw(uint8(255.*im2double(im)),0.99);

%%// Remove
BW = bwmorph(BW,'skel',Inf);

figure,imshow(BW)

Associated function ( source )

function [x,y] = ellipse1(x1,y1,x2,y2,e)

a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
t = linspace(0,2*pi);
X = a*cos(t);
Y = b*sin(t);
w = atan2(y2-y1,x2-x1);
x = (x1+x2)/2 + X*cos(w) - Y*sin(w);
y = (y1+y2)/2 + X*sin(w) + Y*cos(w);

return;

Output

在此处输入图片说明

You can first grab the frame of the figure and then its "cdata". An example is given here: http://tipstrickshowtos.blogspot.com/2010/03/saving-image-of-matlab-figure.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM