如何在matlab中实现多轮廓线内的颜色填充

如何在matlab中实现多轮廓线内的颜色填充,matlab,Matlab,我有一张包含两条等高线的图像。我想在这些轮廓线内填充不同的颜色。如何实施?这是我画两条等高线的代码 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]

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

    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
这是以前的事
这是在运行后

使用
contourc
计算轮廓并
面片
将其绘制为填充区域

遵循(经过修饰的;)代码

计算轮廓

cont = contourc(phi1, [0 0])';
cont = cont(2 : end, :);       % first line contains contour level and number of points; skip
并将其绘制为“补丁”:

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

结果:


对于
phi2
您当然需要简单的模拟代码。

请显示您拥有的和想要获得的示例图。你已经看过《轮廓》了吗?我更新了我需要的。请检查+1以使用Matlab附带的示例图像!:-)您确定需要填充轮廓吗?你使用的那些轮廓只是圆,所以也许你需要一个圆填充功能?@a。唐达:我的轮廓是指任何轮廓,这意味着它可以是圆、正方形……在我的问题中。我只提出简单的问题以便于理解。我想在轮廓内填充颜色。感谢对
contourc
patch
的良好演示@A。唐达:如果我有一个物体的两个圆如何。我想把同样的红色体色填充到这些圆圈里。怎么做?假设这些圆没有连接在一起。感谢you@user3336190,只需重复该过程,从
phi1=…
patch(…
)。
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')