在Matlab中的pcolor中找到轮廓/边缘 [英] Find contour/edge in pcolor in Matlab
问题描述
我正在尝试在Matlab的pcolor
图中绘制遵循像素"边缘的轮廓.这可能是图片中最好的解释.这是我的数据图.黄色数据(data == 1)和蓝色数据(data == 0)之间有明显的界限:
I'm trying to make a contour that follows the edges of the 'pixels' in a pcolor
plot in Matlab. This is probably best explained in pictures. Here is a plot of my data. There is a distinct boundary between the yellow data (data==1) and the blue data (data==0):
请注意,这是一个pcolor
图,因此每个正方形"本质上都是一个像素.我想返回遵循黄色数据 pixels 的 faces 的轮廓,不只是黄色数据的边缘.
Note that this is a pcolor
plot so each 'square' is essentially a pixel. I want to return a contour that follows the faces of the yellow data pixels, not just the edge of the yellow data.
因此,输出轮廓(绿线)穿过像素的脸部(红点)的中点.
So the output contour (green line) passes through the mid-points of the face (red dots) of the pixels.
请注意,我不希望轮廓跟随数据的中心点(黑点),这会像绿线一样.使用contour
即可轻松实现.
Note that I don't want the contour to follow the centre points of the data (black dots), which would do something like this green line. This could be achieved easily with contour
.
此外,如果有帮助,我会提供一些有用的网格.我在像素中间有个点(显然,这就是我在这里绘制的),在角上也有点,在西/东面和北/南面上也有点.如果您熟悉荒川网格,则这是荒川C网格,所以我有rh-,u-,v-和psi-点.
Also, if it's any help, I have a few grids which may be useful. I have the points in the middle of the pixels (obviously, as that's what I've plotted here), I also have the points on the corners, AND I have the points on the west/east faces and the north/south faces. IF you're familiar with Arakawa grids, this is an Arakawa-C grid, so I have the rho-, u-, v- and psi- points.
我尝试了插值,网格交织以及其他一些功能,但是我没有任何运气.任何帮助将不胜感激,并将阻止我发疯.
I've tried interpolation, interweaving grids, and a few other things but I'm not having any luck. Any help would be HUGELY appreciated and would stop me going crazy.
干杯,戴夫
对不起,我简化了图像,使我想解释的内容更加明显,但这是我要分离的区域的较大图像(放大后):
Sorry, I simplified the images to make what I was trying to explain more obvious, but here is a larger (zoomed out) image of the region I'm trying to separate:
如您所见,它是一个复杂的轮廓,它向西南"方向前进,然后环绕并向东北"移动.这是我想通过黑点画出的红线:
As you can see, it's a complex outline which heads in a "southwest" direction before wrapping around and moving back "northeast". And here is the red line that I'd like to draw, through the black points:
推荐答案
您可以通过对解决方案I进行一些修改来解决此问题发布到相关问题.我在问题data
的一部分中使用了示例图像蒙版的一部分.首先,您需要填充蒙版中的孔,您可以使用 图像处理工具箱中的imfill
:>
You can solve this with a couple of modifications to a solution I posted to a related question. I used a section of the sample image mask in the question for data
. First, you will need to fill the holes in the mask, which you can do using imfill
from the the Image Processing Toolbox:
x = 1:15; % X coordinates for pixels
y = 1:17; % Y coordinates for pixels
mask = imfill(data, 'holes');
接下来,应用我的其他答案中的方法来计算轮廓坐标的有序集合(位于像素角上):
Next, apply the method from my other answer to compute an ordered set of outline coordinates (positioned on the pixel corners):
% Create raw triangulation data:
[cx, cy] = meshgrid(x, y);
xTri = bsxfun(@plus, [0; 1; 1; 0], cx(mask).');
yTri = bsxfun(@plus, [0; 0; 1; 1], cy(mask).');
V = [xTri(:) yTri(:)];
F = reshape(bsxfun(@plus, [1; 2; 3; 1; 3; 4], 0:4:(4*nnz(mask)-4)), 3, []).';
% Trim triangulation data:
[V, ~, Vindex] = unique(V, 'rows');
V = V-0.5;
F = Vindex(F);
% Create triangulation and find free edge coordinates:
TR = triangulation(F, V);
freeEdges = freeBoundary(TR).';
xOutline = V(freeEdges(1, [1:end 1]), 1); % Ordered edge x coordinates
yOutline = V(freeEdges(1, [1:end 1]), 2); % Ordered edge y coordinates
最后,您可以在像素边缘的中心处获得所需的坐标,如下所示:
Finally, you can get the desired coordinates at the centers of the pixel edges like so:
ex = xOutline(1:(end-1))+diff(xOutline)./2;
ey = yOutline(1:(end-1))+diff(yOutline)./2;
这是显示结果的图:
imagesc(x, y, data);
axis equal
set(gca, 'XLim', [0.5 0.5+size(mask, 2)], 'YLim', [0.5 0.5+size(mask, 1)]);
hold on;
plot(ex([1:end 1]), ey([1:end 1]), 'r', 'LineWidth', 2);
plot(ex, ey, 'k.', 'LineWidth', 2);
这篇关于在Matlab中的pcolor中找到轮廓/边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!