PLEASE HELP! I need to detect lines using Hough Transform

3 views (last 30 days)
I need to detect road lanes using the Hough Transform. I'm using the image attached and I'm getting the following result. Can anybody help me detecting only the two road lines? Thanks in advance.
I = imread('espe1_bn.jpg');
img_filt = imfilter(I, [-1 0 1], 'replicate','corr');
%subplot(2,2,2); imshow(img_filtro);axis on;
%title('Imagen filtrada');
% --Deteccion de bordes
BW = edge(img_filt,'canny');
%BW(BW < 0) = 0;
%BW(BW > 1) = 1;
[H,theta,rho] = hough(BW);
imshow(H,[],'XData',theta,'YData',rho,...
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,2,'threshold',ceil(0.3*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
plot(x,y,'s','color','white');
lines = houghlines(BW,theta,rho,P,'FillGap',45,'MinLength',7);
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
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
Output:

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!