如何从消失的线条中找到消失点?

时间:2015-08-17 22:40:55

标签: matlab image-processing vanishing-point

我试图找到消失线的消失点来估计2D图像的深度图。

首先,我使用霍夫变换检测到2D图像的消失线。这是我在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

现在我需要找到能够估计深度图的消失点。 选择消失点作为其周围交叉点数量最多的交叉点。

我的问题,换句话说,我怎样才能找到Matlab中多行(消失线)的交集?我想一种方法是找到所有线的平方距离总和最小的点,但不确定如何在Matlab中做到这一点?

任何帮助都将不胜感激。

编辑:我试图找到这些线的交点,但我只能找到每条线和它后面的线的交点。我不知道如何找到所有线路的交叉点?

以下是我正在使用的图片示例: https://www.dropbox.com/s/mbdt6v60ug1nymb/landscape.bmp?dl=0

我发布了一个链接,因为我没有足够的声誉来发布图片。

1 个答案:

答案 0 :(得分:2)

一种简单的方法:

您应该能够创建一个包含行之间所有交叉点的数组。

伪代码:

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

如果你有所有的交叉点,你可以简单地取平均值。

或者,采取这里写的方法:

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

3d可以简化为2d:)