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

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

算法描述

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

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

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

函数原型

cpp 复制代码
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: 允许在同一直线上的点之间的最大间隙(以像素为单位),以将它们连接起来形成直线段。

代码示例

cpp 复制代码
#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的效果会好很多。

相关推荐
富唯智能16 分钟前
转运机器人可以绕障吗?
人工智能·智能机器人·转运机器人
视觉语言导航1 小时前
湖南大学3D场景问答最新综述!3D-SQA:3D场景问答助力具身智能场景理解
人工智能·深度学习·具身智能
AidLux1 小时前
端侧智能重构智能监控新路径 | 2025 高通边缘智能创新应用大赛第三场公开课来袭!
大数据·人工智能
引量AI1 小时前
TikTok矩阵运营干货:从0到1打造爆款矩阵
人工智能·矩阵·自动化·tiktok矩阵·海外社媒
Hi-Dison1 小时前
神经网络极简入门技术分享
人工智能·深度学习·神经网络
奋斗者1号1 小时前
机器学习之决策树模型:从基础概念到条件类型详解
人工智能·决策树·机器学习
LinkTime_Cloud2 小时前
谷歌引入 AI 反诈系统:利用语言模型分析潜在恶意网站
人工智能·语言模型·自然语言处理
张小九992 小时前
PyTorch的dataloader制作自定义数据集
人工智能·pytorch·python
Panesle2 小时前
分布式异步强化学习框架训练32B大模型:INTELLECT-2
人工智能·分布式·深度学习·算法·大模型
zstar-_2 小时前
FreeTex v0.2.0:功能升级/支持Mac
人工智能·python·macos·llm