我正在尝试使用霍夫变换检测图像上的白色直线。
Binary image with White = 1, Black= 0 - Lines detected in green and longest line detected in red
以下代码仅检测曲线的直线部分并忽略(无法识别)真正的直线。如何修改它以仅在白线中找到正确的直线?
load('image.mat')
%Binarize image
image(image== 0) = 1;
image= image- ones(size(image),'int8');
image= logical(image);
% imshow(BW_active,[0 1])
[H,theta,rho] = hough(image, 'RhoResolution',0.5);
P = houghpeaks(H);
lines = houghlines(image,theta,rho,P); %,'FillGap',20,'MinLength',7);
figure, imshow(image, [0 1]), hold on
max_len = 0;
% from Mathworks
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)image
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
这里有.mat文件。 https://drive.google.com/file/d/0BxCvoDCdJJYoOFZLeklCX2dzc2c/view?usp=sharing