以上是opecv reference里面的说明。
Image必须是8位单通道图(可以使灰度图、二值图、边缘图等)
Rho:距离分辨率,一般为1
Theta:角度分辨率,一般为CV_PI/180
Threshold:阈值,只返回像素和大于threshold的直线
Srn:(猜测)距离缩放
Stn:(猜测)角度缩放
重点说明
Lines:
—p: 直线到原点(左上角)的距离
—:角度
这里的角度文档上有写是vertical line~0°,horizontal line~90°但是它没说方向是顺时针啊!
这导致我不知道下面这个角度到底是怎么计算的
An example using the Hough line detector can be found at opencv_source_code/samples/cpp/houghlines.cpp
大家可以再画图里画两条直线验证
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; static void help() { cout << "\nThis program demonstrates line finding with the Hough transform.\n" "Usage:\n" "./houghlines <image_name>, Default is pic1.png\n" << endl; } int main(int argc, char** argv) { const char* filename = argc >= 2 ? argv[1] : "d:/vs/cardPic/w.png"; Mat src = imread(filename, 0); if(src.empty()) { help(); cout << "can not open " << filename << endl; return -1; } Mat dst, cdst; Canny(src, dst, 50, 200, 3); cvtColor(dst, cdst, COLOR_GRAY2BGR); vector<Vec2f> lines; HoughLines(dst, lines, 1, CV_PI/180, 80, 0, 0 ); for( size_t i = 0; i < lines.size(); i++ ) { float rho = lines[i][0], theta = lines[i][1]; cout<<i<<":"<<theta*180/CV_PI<<endl; 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), 1, CV_AA); } imshow("source", src); imshow("detected lines", cdst); waitKey(); return 0; }
时间: 2024-10-10 15:42:33