3

Using Hough transform in Matlab,detected some lines. Using the end points of these lines I have plotted them. I cant understand how I can find intersecting lines when I have all the variables.

Line7
Point1 [50,66]
Point2 [11,106]
theta,rho [45,81]


Line9
Point1 [19,83]
Point2 [53,79]
theta,rho [82,84]

Since the parametric equations are as follows

  rho = xCos(theta) + ySin(theta)

I am unsure how to solve this. With all this information there must be a quick way of finding if the lines intersect, if so, the points as well.

Any guidance much appreciated.

function FindHoughLines(I,filename)
[H,T,R] = hough(I);
rotI = imrotate(I,0,'crop');
imshow(H,[],'XData',T,'YData',R,...
            'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P  = houghpeaks(H,10,'threshold',ceil(0.1*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
 plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(I,T,R,P,'FillGap',5,'MinLength',7);
 figure, imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
    if(isField(lines,'point1') ~= 0)               
   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');
   text(xy(1,1),xy(1,2),[ num2str(k)],'HorizontalAlignment','center','BackgroundColor',[.7 .9 .7]);
   % 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
end

Lines

  • 1
    Take a look at this post, probably will help. http://stackoverflow.com/questions/2050850/matlab-find-point-of-intersection-between-two-vectors – Rob Taylor Apr 27 '12 at 19:03
  • @RobTaylor, thanks, that was the answer! –  Apr 28 '12 at 01:15

2 Answers2

2

The most straightforward way that comes to mind is to convert all detected lines to cartesian coordinates using

y = -ctg(theta) + r/sin(theta)

Then use a standart detection procedure, like http://en.wikipedia.org/wiki/Bentley–Ottmann_algorithm

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55
1

Are you wanting intersections of lines or line segments? For instance, suppose one of your line segments has endpoints (1,0) and (2,0) and the other has endpoints (0,1) and (0,2); do you want the intersection at (0,0) counted or not?

Do you need to cope efficiently with the case where there are many many lines and find all their intersections, or is it OK to consider all pairs of lines and check them one by one?

The easiest case is if it's OK just to consider pairs of lines, and if line (as opposed to line segment) intersections are good enough. Then for each pair of lines you're just solving two simultaneous linear equations: aX+bY=c, dX+eY=f. This is a very standard thing to do; it amounts to inverting a 2x2 matrix.

If you need to notice when an intersection doesn't actually lie within the given line segments, here are a couple of approaches. (1) First first the intersection as above, then check that it lies within each given segment. You can do that by picking one coordinate (say, the x-coordinate) and seeing whether it lies between the x-coordinates of the two endpoints. Of course you can't use the x-coordinate for vertical lines and shouldn't use it for nearly-vertical lines; better to pick whichever coordinate has the larger coefficient. (2) Write the lines parametrically as (x,y)=(x1,y1)+t(dx1,dx1) and (x,y)=(x2,y2)+u(dx2,dy2); now instead of simultaneous equations for x,y you have simultaneous equations for t,u; solve those and check that 0 <= t,u <= 1.

If you need to cope efficiently when you have many lines, there are algorithms for this; google "line sweep" to find a standard one that's pretty easy to understand.

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
  • Thanks for the answer. Actually I have many lines, and I want to find the intersection of lines instead of line segments. Lines that actually intersect. –  Apr 28 '12 at 01:07