Matlab-Hough变换检测到粗糙的线条(二值化后并非完全正确)

时间:2018-07-24 13:39:40

标签: matlab hough-transform

我正在尝试使用霍夫变换来检测图像中的线条。我几乎明白了,但是二值化后的线条太粗糙了,不能被认为是笔直的(参见图像,可能您需要以完整尺寸看到它们)。有什么方法(也许是一些“ bwmorph”操作)来软化二值化的线,并使它们更直,以便于hough变换更容易将其检测为单线?

Image which I aplly the Hough Transform

Resulting lines and binarized image

我现在的代码是:

F=getframe;
I = rgb2gray(frame2im(F));
BW = imbinarize(I, 'adaptive', 'Sensitivity', 0.35);
BW = bwmorph(BW,'thin', inf);

1 个答案:

答案 0 :(得分:2)

您不一定需要先进行骨架化,但是您确实需要调整霍夫变换的参数,特别是希望它如何检测峰并填补空白。这是我对您的图形(https://www.mathworks.com/help/images/ref/houghlines.html)进行的变换的示例:

bw = (imbinarize(I, graythresh(I)));
dilatedImage = imdilate(bw,strel('disk',10));
thinnedImage = bwmorph(dilatedImage,'thin',inf);

[H,theta,rho] = hough(thinnedImage);
P = houghpeaks(H,20,'threshold',0);
lines = houghlines(thinnedImage,theta,rho,P,'FillGap',400,'MinLength',300);

figure, imshow(I), hold on
max_len = 0;
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

   % Plot beginnings and ends of lines
   plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

   % Determine the endpoints of the longest line segment
   len = norm(lines(k).point1 - lines(k).point2);
   if ( len > max_len)
      max_len = len;
      xy_long = xy;
   end
end

输出看起来像这样: enter image description here