【问题标题】:How to fill color inside multiple contour line in the matlab如何在matlab中的多条轮廓线内填充颜色
【发布时间】:2013-12-04 03:15:50
【问题描述】:

我有一张包含两条轮廓线的图像。我想在这些轮廓线内填充不同的颜色。如何实施?这是我绘制两条等高线的代码

    function  FillColorContour(Img,phi1,phi2,color1,color2)
        imagesc(uint8(Img),[0 255]),colormap(gray),axis off;axis equal,title('FillColorContour')
        hold on,[c,h1] = contour(phi1,[0 0],'r','linewidth',1); hold off
        hold on,[c,h2] = contour(phi2,[0 0],'r','linewidth',1); hold off
    end

使用它。我会通过命令调用:

        Img=imread('peppers.png');
        [Height Wide] = size(Img);
        [xx yy] = meshgrid(1:Wide,1:Height);
        phi1 = (sqrt(((xx - 60).^2 + (yy - 100).^2 )) - 15);
        phi2 = (sqrt(((xx - 100).^2 + (yy - 150).^2 )) - 15);

        FillColorContour(Img,phi1,phi2,'r','b') %Assume'r' is red, 'b' is blue

这是在https://www.dropbox.com/s/ll4npg3cmturt4c/contourex.PNG 之前 这是运行后https://www.dropbox.com/s/pqi4rxluxfegxhn/contourexfill.png

【问题讨论】:

  • 请展示您拥有的和想要获得的示例图。你已经看过 `contourf` 了?
  • 我更新了我需要的内容。请检查
  • +1 用于使用 Matlab 附带的示例图像! :-)
  • 您确定需要填充轮廓吗?您使用的那些轮廓只是圆形,所以也许您需要一个圆形填充功能?
  • @A. Donda:我的轮廓是任何轮廓,这意味着它可以是圆形,方形......在我的问题中。我只提出简单的问题以便于理解。我想在轮廓内填充颜色。谢谢

标签: matlab


【解决方案1】:

使用contourc 计算轮廓,使用patch 将其绘制为填充区域。

按照您的(美化的;)代码

Img = imread('peppers.png');
[Height, Width] = size(Img);
[xx, yy] = meshgrid(1 : Width, 1 : Height);
imagesc(Img,[0 255])
axis off
title('FillColorContour')
phi1 = (sqrt(((xx - 60).^2 + (yy - 100).^2 )) - 15);

计算轮廓

cont = contourc(phi1, [0 0])';
cont = cont(2 : end, :);       % first line contains contour level and number of points; skip

并将其绘制为“补丁”:

patch(cont(:, 1), cont(:, 2), 'r', 'EdgeColor', 'w')

您可以分别选择填充颜色和边缘颜色;我用了红色和白色。

结果:

对于phi2,您当然只需要类似的代码。

【讨论】:

  • 很好的演示contourcpatch
  • @A. Donda:如果我有一个物体的两个圆圈怎么样。我想在这些圆圈中填充相同的红色。怎么做?假设这些圈子没有连接在一起。谢谢
  • @user3336190,重复这个过程,从phi1 = …开始到patch(…
猜你喜欢
  • 2013-07-16
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 2021-11-26
  • 2014-05-22
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多