MATLAB Answers

How to fill contours in a contour matrix

조회 수: 9(최근 30일)
Otis
Otis 9 Sep 2014
댓글: Otis 11 Sep 2014
Yeah, I know, I could use contourf, but I want to draw the contours generated by contourc on another plot. What I need help with is how to close the contours, in particular those at the edge of the data.
If you run this code, you will see what I mean. How to properly close the contour at the lower left corner and the one at the top.
data = [ ...
9 9 9 9 1 1 1 1 1 1 1 1 1 1 1 1; ...
9 9 9 9 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 9 9 9 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 9 9 9 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 9 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 9 9 9 1 1 1 1 1 1; ...
1 1 1 1 1 1 9 9 9 9 9 1 1 1 1 1];
figure(1)
clf
subplot(2,2,1)
imagesc(data)
set(gca, 'YDir', 'normal', 'XLim', [1 12], 'YLim', [1 12]);
subplot(2,2,2)
C = contourf(data, 1);
subplot(2,2,3)
kdx = 1;
while (1)
l = C(1,kdx);
n = C(2,kdx);
x = C(1,(kdx+1):(kdx+n-1));
y = C(2,(kdx+1):(kdx+n-1));
plot(x, y); hold on
patch(x, y, l);
kdx = kdx + n + 1;
if (kdx > size(C,2))
break;
end
end
set(gca, 'XLim', [1 12], 'YLim', [1 12]);
Thanks so much Bill
  댓글 수: 3
Otis
Otis 11 Sep 2014
I had to punt on this problem because of time, I needed to get something done today. But I still want to understand how the contour matrix, C, computed by contourc, is used to draw the boxes in subplot(2,2,2) and not subplot(2,2,3)
>> disp(C')
1.0000 1.0000
NaN NaN
5.0000 11.0000
11.5000 12.0000
11.0000 11.5000
10.5000 11.0000
10.0000 10.5000
9.5000 10.0000
9.0000 9.5000
8.5000 10.0000
8.0000 10.5000
7.5000 11.0000
7.0000 11.5000
6.5000 12.0000
5.0000 11.0000
10.0000 6.5000
10.5000 6.0000
10.5000 5.0000
10.0000 4.5000
9.0000 4.5000
8.0000 4.5000
7.5000 5.0000
7.5000 6.0000
8.0000 6.5000
9.0000 6.5000
10.0000 6.5000
5.0000 6.0000
1.0000 2.5000
2.0000 2.5000
3.0000 2.5000
4.0000 2.5000
4.5000 2.0000
4.5000 1.0000

댓글을 달려면 로그인하십시오.

답변(1개)

Image Analyst
Image Analyst 9 Sep 2014
I'd do it as an image. See http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/ If you only have one level for the contour, as your one example shows (just the "red" level) then you can threshold and get a binary image and overlay the binary image as Steve shows in his blog. If you want outlines, you can use bwboundaries() in the Image Processing Toolbox and then use plot() to plot the boundaries as graphical lines in the overlay above the image.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by