Search code examples
imagematlablines

How to find intersection point between lines matlab


I have 2 or 3 hough lines drawn on the edges of the road these lines intersect at the horizon point where the road and the sky meet. I want to find and plot this point. How can i achieve this using hough lines? An idea rings in my mind of voting map how can i create voting map ? This is my code. Assume hough lines from the edges of the road, intersect and make a triangle with the road area inside. here is my output image

enter image description here

 I  =  imread('1.jpg');


 J = imfilter(I, fspecial('gaussian', [17 17], 5), 'symmetric');

se3 = strel('disk', 4);

%J = imdilate(J, se);

J = rgb2gray(J);

BW = edge(J, 'sobel');
BW = imdilate(BW, se3);

[H T R] = hough(BW);

P  = houghpeaks(H, 4);

lines = houghlines(J, T, R,P);
 Q = figure(5);
 imshow(I) 
 hold on;

 xy1 = [lines(3).point1; lines(3).point2];
 line1 =  plot(xy1(:,1),xy1(:,2),'LineWidth',6,'Color','blue');

  xy2 = [lines(4).point1; lines(4).point2];
 line2 =  plot(xy2(:,1),xy2(:,2),'LineWidth',6,'Color','blue');

  xy3 = [lines(2).point1; lines(2).point2];
 line3 =  plot(xy3(:,1),xy3(:,2),'LineWidth',6,'Color','blue');

Solution

  • if you have equations of the lines then you just need to solve 3 equations with two unknowns (x,y of intersection point). Use matlab matrix division operator (\) for that. For example: You have 3 lines:

    • X + 2Y = 7,
    • 3X + 4Y = 8,
    • 5X + 6Y = 9

    The code is:

    A = [1,2;3,4;5,6]
    b = [7,8,9]'
    x = A\b
    

    If you dont know the equations but only pixels in which those lines pass then drow all the lines on a black image and search for maximal pixel value. The intersection point will be 3 times brighter than the rest of lines. If you want to reduce run time than start from pixel on arbitrary line and iterate over its neighbours until you find the intersection point.