opencv中的标准霍夫线变换HoughLines()的-学习笔记

来源:互联网 发布:本机mac地址怎么查 编辑:程序博客网 时间:2024/06/06 03:35

#include "opencv2/core/core.hpp"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/opencv.hpp"#include <iostream>using namespace cv;using namespace std;using std::cout;int main(){Mat g_srcImage,midImage, dstImage;namedWindow("[原始图]");g_srcImage = imread("1.jpg");if (!g_srcImage.data){ cout << "error read image" << endl; return 0; }imshow("[原始图]", g_srcImage);Canny(g_srcImage, midImage, 50, 200, 3);cvtColor(midImage, dstImage, CV_GRAY2BGR);vector<Vec2f> lines;HoughLines(midImage, lines, 0.5, CV_PI / 18, 150, 0, 0);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 + 2000 * (-b));  //把浮点数转化成整数pt1.y = cvRound(y0 + 2000 * (a));pt2.x = cvRound(x0 - 2000 * (-b));pt2.y = cvRound(y0 - 2000 * (a));line(dstImage, pt1, pt2, Scalar(128, 128, 0), 1, CV_AA);}imshow("边缘检测后的图", midImage);imshow("标准霍夫线变换效果图", dstImage);waitKey(0);return 0;}

一、下面首先对HoughLines函数进行讲解: 
void 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 ) 
Parameters: 
1、image – 8-bit, single-channel binary source image. The image may be modified by the function. 
输入的图像应该是8为单通道二值图像 
2、lines—Output vector of lines. Each line is repred;
sented by a two-element vector (\rho, \theta) . \rho is the distance from the coordinate origin (0,0) (top-left corner of the image). \theta is the line rotation angle in radians ( 0 \sim \textrm{vertical line}, \pi/2 \sim \textrm{horizontal line} ). 
输出的直线,lines代表的是一个容器,包含(\rho, \theta)的容器 
3、rho – Distance resolution of the accumulator in pixels. 
极径参数的距离分辨率 
4、theta – Angle resolution of the accumulator in radians. 
极角参数的角度分辨率 
5、threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes (>\texttt{threshold} ). 
设定的阈值,大于此阈值的交点,才会被认为是一条直线 
之后几个参数可以用默认值 
6、srn – For the multi-scale Hough transform, it is a divisor for the distance resolution rho . The coarse accumulator distance resolution is rho and the accurate accumulator resolution is rho/srn . If bothsrn=0 and stn=0 , the classical Hough transform is used. Otherwise, both these parameters should be positive. 
7、stn – For the multi-scale Hough transform, it is a divisor for the distance resolution theta. 
min_theta – For standard and multi-scale Hough transform, minimum angle to check for lines. Must fall between 0 and max_theta./ 
8、max_theta – For standard and multi-scale Hough transform, maximum angle to check for lines. Must fall between min_theta and CV_PI. 
之后几个参数可以用默认值

二、下面首先对HoughLinesP函数进行讲解: 
void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, doubleminLineLength=0, double maxLineGap=0 ) 
Parameters: 
1、image – 8-bit, single-channel binary source image. The image may be modified by the function. 
输入的图像应该是8为单通道二值图像 
2、lines – Output vector of lines. Each line is represented by a 4-element vector (x_1, y_1, x_2, y_2) , where(x_1,y_1) and (x_2, y_2) are the ending points of each detected line segment. 
rho – Distance resolution of the accumulator in pixels. 
输出的矢量。输出的两点是线段的两个端点 
3、rho – Distance resolution of the accumulator in pixels. 
极径参数的距离分辨率 
4、theta – Angle resolution of the accumulator in radians. 
极角参数的角度分辨率 
5、threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes (>\texttt{threshold} ). 
设定的阈值,大于此阈值的交点,才会被认为是一条直线 
6、minLineLength – Minimum line length. Line segments shorter than that are rejected. 
线段的最小长度 
7、maxLineGap – Maximum allowed gap between points on the same line to link them. 
点到直线被允许的最大距离 
三、对代码进行讲解

if 0 
  vector lines; 
  HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 ); 
  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, CV_AA); 
  } 
#else 
     vector lines; 
     HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 ); 
    for( size_t i = 0; i < lines.size(); i++ ) 
    { 
        Vec4i l = lines[i]; 
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA); 
   } 
#endif 
imshow(“source”, src); 
imshow(“detected lines”, cdst);

waitKey();

return 0; 
}

1、c++中 Vector 是一个类模板。不是一种数据类型。 Vector是一种数据类型。

2、 typedef    vec<uchar, 2>    Vec2b;    typedef    vec<uchar, 3>    Vec3b;    typedef    vec<uchar, 4>    Vec4b; typedef    vec<short, 2>    Vec2s; typedef    vec<short, 3>    Vec3s; typedef    vec<short, 4>    Vec4s; typedef    vec<int, 2>    Vec2i; typedef    vec<int, 3>    Vec3i; typedef    vec<int, 4>    Vec4i; typedef    vec<float, 2>    Vec2f; typedef    vec<float, 3>    Vec3f;  typedef    vec<float, 4>    Vec4f; typedef    vec<float, 6>    Vec6f; typedef    vec<double, 2>    Vec2d; typedef    vec<double, 3>    Vec3d;  typedef    vec<double, 4>    Vec4d; typedef    vec<double, 6>    Vec6d;

3、函数讲解:

 float rho = lines[i][0], theta = lines[i][1]; //返回极径和极角 Point pt1, pt2;   double a = cos(theta), b = sin(theta);  //确定cos和sin的值 double x0 = a*rho, y0 = b*rho;    //确定X0和Y0点 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, CV_AA); 

通过X0和Y0向上向下个找两点,确定一条直线。1000只是个数,也可以用其他的数。

4、cvRound 
int cvRound (double value) //把浮点数转化成整数

原创粉丝点击