opencv学习笔记--霍夫线变换

  1. 霍夫线变换是一种用来寻找直线的方法.
  2. 是用霍夫线变换之前, 首先要对图像进行边缘检测的处理,也即霍夫线变换的直接输入只能是边缘二值图像.

它是如何实现的?

  1. 众所周知, 一条直线在图像二维空间可由两个变量表示. 例如:

    1. 在 笛卡尔坐标系: 可由参数: (m,b) 斜率和截距表示.
    2. 在 极坐标系: 可由参数: (r,\theta) 极径和极角表示
    Line variables

    对于霍夫变换, 我们将用 极坐标系 来表示直线. 因此, 直线的表达式可为:

    y = \left ( -\dfrac{\cos \theta}{\sin \theta} \right ) x + \left ( \dfrac{r}{\sin \theta} \right )

    化简得: r = x \cos \theta + y \sin \theta

  2. 一般来说对于点 (x_{0}, y_{0}), 我们可以将通过这个点的一族直线统一定义为:

    r_{\theta} = x_{0} \cdot \cos \theta  + y_{0} \cdot \sin \theta

    这就意味着每一对 (r_{\theta},\theta) 代表一条通过点 (x_{0}, y_{0}) 的直线.

  3. 如果对于一个给定点 (x_{0}, y_{0}) 我们在极坐标对极径极角平面绘出所有通过它的直线, 将得到一条正弦曲线. 例如, 对于给定点 x_{0} = 8 and y_{0} = 6 我们可以绘出下图 (在平面 \theta - r):

    Polar plot of a the family of lines of a point

    只绘出满足下列条件的点 r > 0 and 0< \theta < 2 \pi.

  4. 我们可以对图像中所有的点进行上述操作. 如果两个不同点进行上述操作后得到的曲线在平面 \theta - r 相交, 这就意味着它们通过同一条直线. 例如, 接上面的例子我们继续对点: x_{1} = 9y_{1} = 4 和点 x_{2} = 12y_{2} = 3 绘图, 得到下图:

    Polar plot of the family of lines for three points

    这三条曲线在 \theta - r 平面相交于点 (0.925, 9.6), 坐标表示的是参数对 (\theta, r) 或者是说点 (x_{0}, y_{0}), 点 (x_{1}, y_{1}) 和点 (x_{2}, y_{2}) 组成的平面内的的直线.

  5. 那么以上的材料要说明什么呢? 这意味着一般来说, 一条直线能够通过在平面 \theta - r 寻找交于一点的曲线数量来 检测. 越多曲线交于一点也就意味着这个交点表示的直线由更多的点组成. 一般来说我们可以通过设置直线上点的 阈值 来定义多少条曲线交于一点我们才认为 检测 到了一条直线.

  6. 这就是霍夫线变换要做的. 它追踪图像中每个点对应曲线间的交点. 如果交于一点的曲线的数量超过了 阈值, 那么可以认为这个交点所代表的参数对 (\theta, r_{\theta}) 在原图像中为一条直线.

标准霍夫线变换和统计概率霍夫线变换

OpenCV实现了以下两种霍夫线变换:

  1. 标准霍夫线变换
  • 原理在上面的部分已经说明了. 它能给我们提供一组参数对 (\theta, r_{\theta}) 的集合来表示检测到的直线
  • 在OpenCV 中通过函数 HoughLines 来实现
  1. 统计概率霍夫线变换
  • 这是执行起来效率更高的霍夫线变换. 它输出检测到的直线的端点 (x_{0}, y_{0}, x_{1}, y_{1})


//! finds lines in the black-n-white image using the standard or pyramid Hough transform
CV_EXPORTS_W void HoughLines( InputArray image, OutputArray lines,
                              double rho, double theta, int threshold,
                              double srn=0, double stn=0 );

  • image: 边缘检测的输出图像. 它应该是个灰度图 (但事实上是个二值化图)
  • lines: 储存着检测到的直线的参数对 (r,\theta) 的容器 
  • rho : 参数极径 r 以像素值为单位的分辨率. 我们使用 1 像素.
  • theta: 参数极角 \theta 以弧度为单位的分辨率. 我们使用 1度 (即CV_PI/180)
  • threshold: 要”检测” 一条直线所需最少的的曲线交点
  • srn and stn: 参数默认为0. 查缺OpenCV参考文献来获取更多信息.

//! finds line segments in the black-n-white image using probabilistic Hough transform
CV_EXPORTS_W void HoughLinesP( InputArray image, OutputArray lines,
                               double rho, double theta, int threshold,
                               double minLineLength=0, double maxLineGap=0 );

  • image: 边缘检测的输出图像. 它应该是个灰度图 (但事实上是个二值化图) 
  • lines: 储存着检测到的直线的参数对 (x_{start}, y_{start}, x_{end}, y_{end}) 的容器
  • rho : 参数极径 r 以像素值为单位的分辨率. 我们使用 1 像素.
  • theta: 参数极角 \theta 以弧度为单位的分辨率. 我们使用 1度 (即CV_PI/180)
  • threshold: 要”检测” 一条直线所需最少的的曲线交点 
  • minLinLength: 能组成一条直线的最少点的数量. 点数量不足的直线将被抛弃.
  • maxLineGap: 能被认为在一条直线上的亮点的最大距离.
//! draws the line segment (pt1, pt2) in the image
CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color,
                     int thickness=1, int lineType=8, int shift=0);

Parameters
img Image.
pt1 First point of the line segment.
pt2 Second point of the line segment.
color Line color.
thickness Line thickness.
lineType Type of the line, see cv::LineTypes.
shift Number of fractional bits in the point coordinates.



猜你喜欢

转载自blog.csdn.net/x670127565/article/details/74931867