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

相关推荐
China_Yanhy5 小时前
动手学大模型第一篇学习总结
人工智能
空间机器人6 小时前
自动驾驶 ADAS 器件选型:算力只是门票,系统才是生死线
人工智能·机器学习·自动驾驶
C+++Python6 小时前
提示词、Agent、MCP、Skill 到底是什么?
人工智能
小松要进步6 小时前
机器学习1
人工智能·机器学习
泰恒6 小时前
openclaw近期怎么样了?
人工智能·深度学习·机器学习
KaneLogger6 小时前
从传统笔记到 LLM 驱动的结构化 Wiki
人工智能·程序员·架构
tinygone6 小时前
OpenClaw之Memory配置成本地模式,Ubuntu+CUDA+cuDNN+llama.cpp
人工智能·ubuntu·llama
正在走向自律7 小时前
第二章-AIGC入门-AIGC工具全解析:技术控的效率神器,DeepSeek国产大模型的骄傲(8/36)
人工智能·chatgpt·aigc·可灵·deepseek·即梦·阿里通义千问
轩轩分享AI7 小时前
DeepSeek、Kimi、笔灵谁最好用?5款网文作者亲测的AI写作神器横评
人工智能·ai·ai写作·小说写作·小说·小说干货
Aevget7 小时前
基于嵌入向量的智能检索!HOOPS AI 解锁 CAD 零件相似性搜索新方式
人工智能·hoops·cad·hoops ai·cad数据格式