如何在matlab中通过边缘点绘制图像的光滑边界

如何在matlab中通过边缘点绘制图像的光滑边界,matlab,boundary,Matlab,Boundary,我有一个分段区域,如图1所示。我想通过连接下边缘点,用matlab绘制下边界,如图2所示。我不能像图2那样绘图。所以我做了一些形态学操作,比如填充、加厚、闭合,但没有得到绘图的想法。你能提供matlab代码吗 图1 图2 这里有一个解决方案 设置图像的阈值,使其仅为二进制,因为此图像很简单 确定每列中最低像素的位置 只需取第n列的最大值即可平滑。如果你只想要最低点,那么你可以停在下面代码的第4行 有关更多详细信息,请对代码进行注释: img = rgb2gray(imread('1.jpg

我有一个分段区域,如图1所示。我想通过连接下边缘点,用matlab绘制下边界,如图2所示。我不能像图2那样绘图。所以我做了一些形态学操作,比如填充、加厚、闭合,但没有得到绘图的想法。你能提供matlab代码吗

图1

图2

这里有一个解决方案

  • 设置图像的阈值,使其仅为二进制,因为此图像很简单
  • 确定每列中最低像素的位置
  • 只需取第n列的最大值即可平滑。如果你只想要最低点,那么你可以停在下面代码的第4行
有关更多详细信息,请对代码进行注释:

img = rgb2gray(imread('1.jpg')); % Read image
img = img > 0.5;                 % Threshold to get binary image
% Get last row where there is a 1 pixel in the image for each column
lastrow = max(repmat((1:size(img,1))', 1, size(img,2)).*img,[],1);
res = 30;                        % Pixel resolution for line averaging
% Ensure res divides num. columns by padding the end of the vector
lastrowpadded = [lastrow, NaN(1, res - mod(numel(lastrow),res))]; 
% Reshape into columns of length 'res', then get the max row number
lastrow2 = max(reshape(lastrowpadded,res,[]),[],1);
% Plots
imshow(img); 
hold on
plot(1:size(img,2), lastrow, '.')
plot(res/2:res:size(lastrowpadded,2)-res/2, lastrow2, 'linewidth', 1.5)
hold off
legend('lowest points', 'smoothed lowest points')
结果:

注意:因为图像是用(0,0)在左上角打印的,所以如果您在没有图像的情况下打印,此打印将颠倒。从图像高度减去
lastrow2
lastrow
值,以纠正此错误


编辑:您可能还对创建凸面外壳感兴趣

[X,Y] = find(img); % After thresholding image as before, get X,Y coords
K = convhull(X,Y); % Get convex hull indices
imshow(img)        % Show image
hold on    
plot(Y(K),X(K),'linewidth',1.5) % Plot convex hull
结果:


首先用散文定义什么是“下边缘点”。从你的照片上看,似乎不是“最低y坐标”,也不是“凸面包络”。虽然不是凸面信封可能只是红线的不精确。(顺便说一句,有趣的图片触发了幻觉)我们能用上面的概念找到最高点并绘制吗?是的,我用
max
得到了
lastrow
,用
min
得到了第一行