编程做网站,做美食下载什么网站,wordpress post_class,wordpress主题 虎嗅网操作系统#xff1a;ubuntu22.04 OpenCV版本#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言#xff1a;C11
算法描述
在二值图像中使用标准 Hough 变换查找直线。
该函数实现了用于直线检测的标准 Hough 变换或标准多尺度 Hough 变换算法。详见 http://homepages… 操作系统ubuntu22.04 OpenCV版本OpenCV4.9 IDE:Visual Studio Code 编程语言C11
算法描述
在二值图像中使用标准 Hough 变换查找直线。
该函数实现了用于直线检测的标准 Hough 变换或标准多尺度 Hough 变换算法。详见 http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm 对 Hough 变换的详细解释。
HoughLines 是 OpenCV 中用于检测图像中直线的一个函数。它利用 Hough 变换来识别图像中的直线。Hough 变换是一种将图像中的直线问题转化为参数空间中的点积累问题的技术。通过这种方式即使直线片段在原始图像中是断断续续的也可以检测出来。
函数原型 void cv::HoughLines
(InputArray image,OutputArray lines,double rho,double theta,int threshold,double srn 0,double stn 0,double min_theta 0,double max_theta CV_PI
)
参数 参数image: 8 位单通道二值源图像。该图像可能在函数执行过程中被修改。 参数lines: 输出的直线向量。每条直线由一个包含 2 或 3 个元素的向量表示ρ, θ或ρ, θ, votes其中 ρ 是从坐标原点0,0图像的左上角到直线的距离θ 是直线旋转角度以弧度为单位0垂直线π/2水平线votes 是累加器的值。 参数rho: 累加器的距离分辨率以像素为单位。 参数theta: 累加器的角度分辨率以弧度为单位。 参数threshold: 累加器的阈值参数。只有那些获得足够投票数threshold的直线才会被返回。 参数srn: 对于多尺度 Hough 变换它是距离分辨率 ρ 的除数。粗略累加器的距离分辨率为 ρ而精确累加器的分辨率为 ρ/srn。如果 srn0 和 stn0将使用经典 Hough 变换。否则这两个参数都应该为正数。 参数stn: 对于多尺度 Hough 变换它是角度分辨率 θ 的除数。 参数min_theta: 对于标准 Hough 变换和多尺度 Hough 变换检查直线的最小角度。必须介于 0 和 max_theta 之间。 参数max_theta: 对于标准 Hough 变换和多尺度 Hough 变换角度的上限。必须介于 min_theta 和 CV_PI 之间。累加器中的实际最大角度可能稍微小于 max_theta具体取决于 min_theta 和 theta 参数。
代码示例 #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 );}// Show resultsimshow( Source, src );imshow( Detected Lines (in red) - Standard Hough Line Transform, cdst );// Wait and ExitwaitKey();return 0;
}运行结果 从结果图上看这个函数的效果并不是很理想。也许能通过调参能更好一些