Convert a plot with ellipses to binary image in MATLAB

爱⌒轻易说出口 提交于 2019-12-11 08:52:05

问题


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!


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/22883859/convert-a-plot-with-ellipses-to-binary-image-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!