1

I'm trying to find vanishing points of vanishing lines to estimate a depth map for a 2D image.

First, I detected the vanishing lines of the 2D image using hough transform. Here is my code in Matlab:

Img =imread('landscape.bmp'); %read the 2D image
%Convert the image to Grayscale
I=rgb2gray(Img);

%Edge Detection
Ie=edge(I,'sobel');

%Hough Transform
[H,theta,rho] = hough(Ie);

% Finding the Hough peaks (number of peaks is set to 5)
P = houghpeaks(H,5,'threshold',ceil(0.2*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));

%Vanishing lines
lines = houghlines(I,theta,rho,P,'FillGap',170,'MinLength',350);
[rows, columns] = size(Ie);
figure, imshow(~Ie)
hold on
xy_1 = zeros([2,2]);
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   % Get the equation of the line
   x1 = xy(1,1);
   y1 = xy(1,2);
   x2 = xy(2,1);
   y2 = xy(2,2);
   slope = (y2-y1)/(x2-x1);
   xLeft = 1; % x is on the left edge
   yLeft = slope * (xLeft - x1) + y1;
   xRight = columns; % x is on the reight edge.
   yRight = slope * (xRight - x1) + y1;
   plot([xLeft, xRight], [yLeft, yRight], 'LineWidth',1,'Color','blue');

   %intersection of two lines (the current line and the previous one)
   slopee = @(line) (line(2,2) - line(1,2))/(line(2,1) - line(1,1));
   m1 = slopee(xy_1);
   m2 = slopee(xy);
   intercept = @(line,m) line(1,2) - m*line(1,1);
   b1 = intercept(xy_1,m1);
   b2 = intercept(xy,m2);
   xintersect = (b2-b1)/(m1-m2);
   yintersect = m1*xintersect + b1;
   plot(xintersect,yintersect,'m*','markersize',8, 'Color', 'red')
   xy_1 = xy;

   % Plot original points on the lines .
   plot(xy(1,1),xy(1,2),'x','markersize',8,'Color','yellow'); 
   plot(xy(2,1),xy(2,2),'x','markersize',8,'Color','green');    
end

Now I need to find the vanishing point to be able to estimate the depth map. The vanishing point is chosen as the intersection point with the greatest number of intersections around it.

My question, in other words, is how can I find the intersection of a number of lines (vanishing lines) in Matlab? I guess one way to do it is to find the point whose sum of squared distances from all lines is minimal, but not sure how to do that in Matlab?

Any help would be appreciated.

Edit: I tried to find the intersection of the lines, but I could only find the intersection of each a line and the line after it. I don't know how to find the intersection of all the lines?

Here is an example of a picture I am using: https://www.dropbox.com/s/mbdt6v60ug1nymb/landscape.bmp?dl=0

I am posting a link because I don't have enough reputations to post an image.

DML2014
  • 107
  • 1
  • 10
  • I'd try using a hough transform as well. Form a hough using x and y parameter space based on the size of the image. You need to cycle through every line and find where it intersects another line. Then increment the bin nearest this location. At the end just find the bin with the highest number of votes. If you find the least squares point, it might not be as robust as that would assume all the lines have to be correctly pointing to the vanishing point, which I assume probably won't be the case as there may be a few incorrect ones. – JustinBlaber Aug 17 '15 at 23:22
  • I tried to find the intersection of the lines, but I could only find the intersection of each a line and the line after it **I just edited my code in the question to show how I do it**. I don't know how to find the intersection of all the lines. Do you know how can I do so? – DML2014 Aug 17 '15 at 23:37
  • Can you add the image you are using please? – JustinBlaber Aug 18 '15 at 17:18

1 Answers1

2

A simplistic approach:

You should be able to create an array with all the intersection points between the lines.

Pseudo code:

for i = 1:length(lines)-1
  for j = i+1:length(lines)
     //add intersection of lines i and j 

If you have all the intersections, you could simply take the average.

OR, take the approach written up here:

https://math.stackexchange.com/questions/61719/finding-the-intersection-point-of-many-lines-in-3d-point-closest-to-all-lines

3d can be simplified to 2d :)

Community
  • 1
  • 1
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
  • This is what I was thinking as well, although it will be n^2/2 calculations, but if the number of lines in OP's pictures are relatively small (which it should be) it shouldn't be an issue. I think you can use the above approach with a hough transform to select the most common intersection point. It should be a relatively easy algorithm to implement. – JustinBlaber Aug 18 '15 at 17:20
  • Thank you @RobAu, I was able to find all the intersections using your way. But I'm not sure how taking the average of all the intersections would help me finding the Vanishing point? – DML2014 Aug 19 '15 at 02:25
  • @jucestain I am not sure how can I use hough transform to find the most common intersection point, can you please explain it more? Thanks in advance. – DML2014 Aug 19 '15 at 02:26
  • CAn you post a picture? I did a project one with 3 vanishing points (from structure where the lines where perpendicular in the real world), so you could calculate the camera position. Is that what you are doing? – Rob Audenaerde Aug 19 '15 at 06:42
  • @user5236997 The hough transform is just a voting scheme. Make a 2D array of points corresponding to the image coordinates. Figure out each line intersection using the looping above, then add 1 to the bin corresponding to the intersection point. At the end, find the bin with the highest number of votes and this will most likely be the intersection point. The hough is good because it will be robust WRT incorrect bins, since these will just be discarded. If you attach the actual image you are using it will be easier to provide a coding example. – JustinBlaber Aug 19 '15 at 16:38
  • @RobAu I just posted a link to a picture I am using. how can I calculate the camera position? – DML2014 Aug 19 '15 at 22:09
  • @jucestain I just posted a link to a picture I am using. – DML2014 Aug 19 '15 at 22:10
  • I wonder what your Hough Transform output is... The image you posted is too complicated imho to do anything with vanishing points. – Rob Audenaerde Aug 20 '15 at 09:47
  • @RobAu sorry I shared the wrong link. Please re-check the image now. Thanks. – DML2014 Aug 20 '15 at 21:48
  • Seems there is one major vanishing point. The average of the intersections will probably be right. Alternatively, you could plot the found hough lines into an image, accumulativly, and then find the maximum pixel value. This is the most likely vanishing point. – Rob Audenaerde Aug 21 '15 at 08:52