自学内容网 自学内容网

OpenCV特征检测(9)检测图像中直线的函数HoughLines()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

在二值图像中使用标准 Hough 变换查找直线。

该函数实现了用于直线检测的标准 Hough 变换或标准多尺度 Hough 变换算法。详见 http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm 对 Hough 变换的详细解释。

HoughLines 是 OpenCV 中用于检测图像中直线的一个函数。它利用 Hough 变换来识别图像中的直线。Hough 变换是一种将图像中的直线问题转化为参数空间中的点积累问题的技术。通过这种方式,即使直线片段在原始图像中是断断续续的,也可以检测出来。

函数原型


void cv::HoughLines
(
InputArray image,
OutputArray lines,
double rho,
double theta,
int threshold,
double srn = 0,
double stn = 0,
double min_theta = 0,
double max_theta = CV_PI 
)

参数

  • 参数image: 8 位单通道二值源图像。该图像可能在函数执行过程中被修改。

  • 参数lines: 输出的直线向量。每条直线由一个包含 2 或 3 个元素的向量表示(ρ, θ)或(ρ, θ, votes),其中 ρ 是从坐标原点(0,0)(图像的左上角)到直线的距离,θ 是直线旋转角度(以弧度为单位,0~垂直线,π/2~水平线),votes 是累加器的值。

  • 参数rho: 累加器的距离分辨率(以像素为单位)。

  • 参数theta: 累加器的角度分辨率(以弧度为单位)。

  • 参数threshold: 累加器的阈值参数。只有那些获得足够投票数(>threshold)的直线才会被返回。

  • 参数srn: 对于多尺度 Hough 变换,它是距离分辨率 ρ 的除数。粗略累加器的距离分辨率为 ρ,而精确累加器的分辨率为 ρ/srn。如果 srn=0 和 stn=0,将使用经典 Hough 变换。否则,这两个参数都应该为正数。

  • 参数stn: 对于多尺度 Hough 变换,它是角度分辨率 θ 的除数。

  • 参数min_theta: 对于标准 Hough 变换和多尺度 Hough 变换,检查直线的最小角度。必须介于 0 和 max_theta 之间。

  • 参数max_theta: 对于标准 Hough 变换和多尺度 Hough 变换,角度的上限。必须介于 min_theta 和 CV_PI 之间。累加器中的实际最大角度可能稍微小于 max_theta,具体取决于 min_theta 和 theta 参数。

代码示例


#include "opencv2/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
    // Declare the output variables
    Mat dst, cdst, cdstP;
    const char* default_file = "sudoku.png";
   
    // Loads an image
    Mat src = imread( "/media/dingxin/data/study/OpenCV/sources/images/line.jpg", IMREAD_GRAYSCALE );
    // Check if image is loaded fine
    if ( src.empty() )
    {
        printf( " Error opening image\n" );
        printf( " Program Arguments: [image_name -- default %s] \n", default_file );
        return -1;
    }
    // Edge detection
    Canny( src, dst, 50, 200, 3 );
    // Copy edges to the images that will display the results in BGR
    cvtColor( dst, cdst, COLOR_GRAY2BGR );
    cdstP = cdst.clone();
    // Standard Hough Line Transform
    vector< Vec2f > lines;                                // will hold the results of the detection
    HoughLines( dst, lines, 1, CV_PI / 180, 150, 0, 0 );  // runs the actual detection
    // Draw the lines
    for ( size_t i = 0; i < lines.size(); i++ )
    {
        float rho = lines[ i ][ 0 ], theta = lines[ i ][ 1 ];
        Point pt1, pt2;
        double a = cos( theta ), b = sin( theta );
        double x0 = a * rho, y0 = b * rho;
        pt1.x = cvRound( x0 + 1000 * ( -b ) );
        pt1.y = cvRound( y0 + 1000 * ( a ) );
        pt2.x = cvRound( x0 - 1000 * ( -b ) );
        pt2.y = cvRound( y0 - 1000 * ( a ) );
        line( cdst, pt1, pt2, Scalar( 0, 0, 255 ), 3, LINE_AA );
    }
           
    // Show results
    imshow( "Source", src );
    imshow( "Detected Lines (in red) - Standard Hough Line Transform", cdst );
   
    // Wait and Exit
    waitKey();
    return 0;
}

运行结果

在这里插入图片描述
从结果图上看,这个函数的效果并不是很理想。也许能通过调参能更好一些


原文地址:https://blog.csdn.net/jndingxin/article/details/142441787

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!