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的效果会好很多。

相关推荐
hundaxxx9 分钟前
自演化大语言模型的技术背景
人工智能
数智顾问34 分钟前
【73页PPT】美的简单高效的管理逻辑(附下载方式)
大数据·人工智能·产品运营
love530love36 分钟前
【保姆级教程】阿里 Wan2.1-T2V-14B 模型本地部署全流程:从环境配置到视频生成(附避坑指南)
人工智能·windows·python·开源·大模型·github·音视频
木头左38 分钟前
结合机器学习的Backtrader跨市场交易策略研究
人工智能·机器学习·kotlin
Coovally AI模型快速验证44 分钟前
3D目标跟踪重磅突破!TrackAny3D实现「类别无关」统一建模,多项SOTA达成!
人工智能·yolo·机器学习·3d·目标跟踪·无人机·cocos2d
研梦非凡1 小时前
CVPR 2025|基于粗略边界框监督的3D实例分割
人工智能·计算机网络·计算机视觉·3d
MiaoChuAI1 小时前
秒出PPT vs 豆包AI PPT:实测哪款更好用?
人工智能·powerpoint
fsnine1 小时前
深度学习——残差神经网路
人工智能·深度学习
和鲸社区2 小时前
《斯坦福CS336》作业1开源,从0手搓大模型|代码复现+免环境配置
人工智能·python·深度学习·计算机视觉·语言模型·自然语言处理·nlp
fanstuck2 小时前
2025 年高教社杯全国大学生数学建模竞赛C 题 NIPT 的时点选择与胎儿的异常判定详解(一)
人工智能·目标检测·数学建模·数据挖掘·aigc