欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > OpenCV特征检测(10)检测图像中直线的函数HoughLinesP()的使用

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

2024/10/24 11:11:57 来源:https://blog.csdn.net/jndingxin/article/details/142442416  浏览:    关键词:OpenCV特征检测(10)检测图像中直线的函数HoughLinesP()的使用
  • 操作系统: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 variablesMat dst, cdst, cdstP;const char* default_file = "sudoku.png";// Loads an imageMat src = imread( "/media/dingxin/data/study/OpenCV/sources/images/line.jpg", IMREAD_GRAYSCALE );// Check if image is loaded fineif ( src.empty() ){printf( " Error opening image\n" );printf( " Program Arguments: [image_name -- default %s] \n", default_file );return -1;}// Edge detectionCanny( src, dst, 50, 200, 3 );// Copy edges to the images that will display the results in BGRcvtColor( dst, cdst, COLOR_GRAY2BGR );cdstP = cdst.clone();// Standard Hough Line Transformvector< Vec2f > lines;                                // will hold the results of the detectionHoughLines( dst, lines, 1, CV_PI / 180, 150, 0, 0 );  // runs the actual detection// Draw the linesfor ( 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 Transformvector< Vec4i > linesP;                                  // will hold the results of the detectionHoughLinesP( dst, linesP, 1, CV_PI / 180, 50, 50, 10 );  // runs the actual detection// Draw the linesfor ( 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 resultsimshow( "Source", src );imshow( "Detected Lines (in red) - Standard Hough Line Transform", cdst );imshow( "Detected Lines (in red) - Probabilistic Line Transform", cdstP );// Wait and ExitwaitKey();return 0;
}

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

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

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com