如何在matlab中的多个轮廓线内填充颜色 [英] How to fill color inside multiple contour line in the matlab
问题描述
我有一幅包含两条轮廓线的图像.我想在这些轮廓线内填充不同的颜色.如何执行呢?这是我的代码绘制两条轮廓线
I have one image that contains two contour line. I want to fill different colors inside these contour line. How to implement it? This is my code to draw two contour line
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
要使用它.我将通过命令调用:
To using it. I will call by a command:
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
推荐答案
使用contourc
计算轮廓,并使用patch
将其绘制为填充区域.
Use contourc
to compute the contour and patch
to draw it as a filled area.
按照您的代码(修饰过的;)
Following your (prettified ;) code
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')
您可以分别选择填充颜色和边缘颜色.我用红色和白色.
You can choose the fill color and the edge color separately; I used red and white.
结果:
对于phi2
,您当然只需要类似的代码.
For phi2
you of course need simply the analogous code.
这篇关于如何在matlab中的多个轮廓线内填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!