检测线的端点

时间:2017-07-20 13:40:19

标签: matlab image-processing

我想检测下图中显示的点:

Image Link

到目前为止我已经这样做了:

[X,map] = rgb2ind(img,0.0);
img = ind2gray(X,map);    % Convert indexed to grayscale
level = graythresh(img);   % Compute an appropriate threshold
img_bw = im2bw(img,level);% Convert grayscale to binary
mask = zeros(size(img_bw));
mask(2:end-2,2:end-2) = 1;
img_bw(mask<1) = 1;
%invert image
img_inv =1-img_bw;
% find blobs
img_blobs = bwmorph(img_inv,'majority',10);
% figure, imshow(img_blobs);
[rows, columns] = size(img_blobs);
for col = 1 : columns
    thisColumn = img_blobs(:, col);
    topRow = find(thisColumn, 1, 'first');
    bottomRow = find(thisColumn, 1, 'last');
    img_blobs(topRow : bottomRow, col) = true;
end
inverted = imcomplement(img_blobs);
ed = edge(inverted,'canny');
figure, imshow(ed),title('inverted');

现在如何继续获取所需位置的坐标?

2 个答案:

答案 0 :(得分:1)

顶点显然是纵坐标最高的白色像素,很容易获得。

底点不是很明确。你能做的是

  • 按照峰值边缘,直到达到局部最小值,在左侧和右侧。这会为您提供一个线段,您可以通过顶点与垂直线相交。

  • 如果您知道峰宽,请尝试垂直点上的每个像素,向上,向下,并停止,直到它没有左边或右边的邻居,其距离等于峰值。

  • 如上所述,但当左右邻居之间的距离超过阈值时停止。

答案 1 :(得分:0)

在这种特殊情况下,您可以考虑在matlab中使用houghlines。设置所需的ThetaMinLength参数值,您应该能够获得与峰值平行的两条垂直线。您可以使用垂直线的终点来获取底部的点。

以下是示例代码。

[H,theta,rho] = hough(bw,'Theta',5:1:30);%This is the angle range
P = houghpeaks(H,500,'NHoodSize',[11 11]);
lines = houghlines(bw,theta,rho,P,'FillGap',10,'MinLength',300);

Here完整描述houghlines实际如何运作。