【问题标题】:Convert a plot with ellipses to binary image in MATLAB在 MATLAB 中将带有椭圆的图转换为二进制图像
【发布时间】:2014-05-18 00:14:49
【问题描述】:

这是我在这里的第一个问题,我在 matlab 中有一个绘图,带有一些省略号。我想将此图转换为二进制图像。 有人可以帮我吗?

这里显示的是带有省略号的图像-

提前感谢您的帮助!

【问题讨论】:

    标签: image matlab image-processing binary blob


    【解决方案1】:

    代码

    %%// ---- 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);
    

    注意:需要从here获取export_fig及相关函数。

    示例案例 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)
    

    输出

    具有扩展功能的示例案例 2

    您可以使用bwmorph 执行二进制骨架化,以保持边缘宽度为1,这在本示例中完成。

    代码

    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)
    

    关联函数 (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;
    

    输出

    【讨论】:

      【解决方案2】:

      您可以先抓取图形的框架,然后抓取它的“cdata”。这里给出一个例子:http://tipstrickshowtos.blogspot.com/2010/03/saving-image-of-matlab-figure.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        • 1970-01-01
        • 2014-01-01
        • 2013-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多