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

算法描述

在二值图像中使用概率霍夫变换查找线段。

该函数实现了用于直线检测的概率霍夫变换算法,该算法在文献 181中有所描述。

HoughLinesP(概率霍夫变换)是 OpenCV 中用于检测图像中直线的一种方法,特别适合检测短直线段。相比于标准的 Hough 变换,HoughLinesP 更加高效,因为它只需要检测累积器中的少数几个投票即可确定直线的存在。

函数原型

void cv::HoughLinesP
(
	InputArray 	image,
	OutputArray 	lines,
	double 	rho,
	double 	theta,
	int 	threshold,
	double 	minLineLength = 0,
	double 	maxLineGap = 0 
)		

参数

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

  • 参数lines: 输出的直线段向量。每条直线段由一个包含 4 个元素的向量表示(x1, y1, x2, y2),其中(x1, y1)和(x2, y2)分别是检测到的每条直线段的两个端点。

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

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

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

  • 参数minLineLength: 最小直线长度。长度小于该值的直线段将被拒绝。

  • 参数maxLineGap: 允许在同一直线上的点之间的最大间隙(以像素为单位),以将它们连接起来形成直线段。

代码示例


#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 );
    }
    // Probabilistic Line Transform
    vector< Vec4i > linesP;                                  // will hold the results of the detection
    HoughLinesP( dst, linesP, 1, CV_PI / 180, 50, 50, 10 );  // runs the actual detection
    // Draw the lines
    for ( size_t i = 0; i < linesP.size(); i++ )
    {
        Vec4i l = linesP[ i ];
        line( cdstP, Point( l[ 0 ], l[ 1 ] ), Point( l[ 2 ], l[ 3 ] ), Scalar( 0, 0, 255 ), 3, LINE_AA );
    }
    // Show results
    imshow( "Source", src );
    imshow( "Detected Lines (in red) - Standard Hough Line Transform", cdst );
    imshow( "Detected Lines (in red) - Probabilistic Line Transform", cdstP );
    // Wait and Exit
    waitKey();
    return 0;
}

这是一张针对函数参数已进行调优的示例图片:
在这里插入图片描述

这是在使用霍夫变换HoughLines时输出结果:

在这里插入图片描述
这是使用概率霍夫变换HoughLinesP时输出结果:在这里插入图片描述
从效果图上看,使用概率霍夫变换HoughLinesP的效果会好很多。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部