How to fill color inside multiple contour lines in matlab

I have one image containing two contour lines. I want to fill in different colors inside this contour line. How to implement it? This is my code for drawing two contour lines.

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 use it. I will call on 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 

This is before https://www.dropbox.com/s/ll4npg3cmturt4c/contourex.PNG And this is after starting https://www.dropbox.com/s/pqi4rxluxfegxhn/contourexfill.png

+1
source share
1 answer

Use contourc to calculate the path 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); 

calculate contour

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

and draw it as a β€œpatch”:

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

You can choose the fill color and edge color separately; I used red and white.

Result:

filled contour

For phi2 , of course, you just need similar code.

+2
source

Source: https://habr.com/ru/post/1268859/


All Articles