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

图2

发布于 2017-05-26 08:22:08
这里有一个解决方案
对代码进行注释以了解更多细节:
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值来纠正这一点。
编辑:您也可能对创建凸包的convhull感兴趣。
[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结果:

https://stackoverflow.com/questions/44193189
复制相似问题