【问题标题】:How to find vanishing points from vanishing lines?如何从消失线中找到消失点?
【发布时间】:2015-11-10 16:58:58
【问题描述】:

我试图找到消失线的消失点来估计 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

我发布链接是因为我没有足够的声誉来发布图片。

【问题讨论】:

  • 我也会尝试使用霍夫变换。根据图像的大小,使用 x 和 y 参数空间形成霍夫。您需要遍历每条线并找到它与另一条线相交的位置。然后增加离该位置最近的 bin。最后只找到票数最高的垃圾箱。如果您找到最小二乘点,它可能不像假设所有线都必须正确指向消失点那样稳健,我认为可能不会出现这种情况,因为可能存在一些不正确的线。
  • 我试图找到线的交点,但我只能找到每条线和它之后的线的交点 我刚刚在问题中编辑了我的代码以显示我是怎么做的它。我不知道如何找到所有线的交点。你知道我该怎么做吗?
  • 你可以添加你正在使用的图像吗?

标签: matlab image-processing vanishing-point


【解决方案1】:

一种简单的方法:

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

伪代码:

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 :)

【讨论】:

  • 这也是我的想法,虽然会是n^2/2的计算,但是如果OP的图片行数比较少(应该是这样)就不应该成为一个问题。我认为您可以使用上述方法和霍夫变换来选择最常见的交点。它应该是一个相对容易实现的算法。
  • 谢谢@RobAu,我用你的方式找到了所有的十字路口。但我不确定取所有交叉点的平均值如何帮助我找到消失点?
  • @jucestain 我不知道如何使用霍夫变换来找到最常见的交点,你能解释一下吗?提前致谢。
  • 你能发张照片吗?我做了一个有 3 个消失点的项目(来自现实世界中直线垂直的结构),所以你可以计算相机位置。这就是你在做的吗?
  • @user5236997 霍夫变换只是一种投票方案。制作与图像坐标相对应的二维点数组。使用上面的循环找出每条线的交点,然后在与交点对应的 bin 中加 1。最后,找到票数最高的 bin,这很可能是交点。 hough 很好,因为它将是健壮的 WRT 不正确的 bin,因为这些只会被丢弃。如果您附上您正在使用的实际图像,则提供编码示例会更容易。
猜你喜欢
  • 2015-09-23
  • 2014-05-14
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
相关资源
最近更新 更多