opencv 霍夫变换检测直线
来源:互联网 发布:安卓刷机软件哪个最好 编辑:程序博客网 时间:2024/05/01 02:31
- #include <iostream>
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- using namespace std;
- int main()
- {
- IplImage *srcRGB = cvLoadImage("f:\\images\\chess1.jpg");
- IplImage *src = cvCreateImage(cvSize(srcRGB->width,srcRGB->height),IPL_DEPTH_8U,1);
- cvCvtColor(srcRGB,src,CV_RGB2GRAY);
- IplImage *dst = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,src->nChannels);
- IplImage *color_dst = cvCloneImage(srcRGB);cvSetZero(color_dst);
- CvMemStorage *storage = cvCreateMemStorage();
- CvSeq *lines = 0;
- cvCanny(src,dst,40,90);
- cvCvtColor(dst,color_dst,CV_GRAY2RGB);
- #if 1
- lines = cvHoughLines2(dst
- ,storage
- ,CV_HOUGH_STANDARD
- ,1
- ,CV_PI/180
- ,150
- ,0
- ,0);
- for(int i = 0;i<MIN(lines->total,100);i++){
- float *line = (float*)cvGetSeqElem(lines,i);
- float rho = line[0];
- float threta = line[1];
- CvPoint pt1, pt2; // 这个地方的计算公式,在文章下面介绍。
- double a = cos(threta),b = sin(threta);
- 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));
- cvLine(color_dst,pt1,pt2,CV_RGB(255,0,0),1,8);
- }
- #else
- lines = cvHoughLines2(dst,
- storage,
- CV_HOUGH_PROBABILISTIC,
- 1,
- CV_PI/180,
- 100,
- 50,
- 10);
- for(int i = 0;i<lines->total; ++i){
- CvPoint *line = (CvPoint*)cvGetSeqElem(lines,i);
- cvLine(color_dst,line[0],line[1],CV_RGB(255,0,0),1,8);
- }
- #endif
- cvShowImage("source",src);
- cvShowImage("Hough",color_dst);
- cvShowImage("dst",dst);
- cvWaitKey(0);
- }
改变#if后的常量为1时,使用上面的代码编译,为0时,使用下面的代码编译
CVAPI(CvSeq*) cvHoughLines2( CvArr* image, void* line_storage, int method, double rho, double theta, int threshold, double param1 CV_DEFAULT(0), double param2 CV_DEFAULT(0)) //
image The 8-bit, single-channel, binary source image. In the case of a probabilistic method, the
image is modified by the function //8bit 单通道二值图像
storage The storage for the lines that are detected. It can be a memory storage (in this case a
sequence of lines is created in the storage and returned by the function) or single row/single
column matrix (CvMat*) of a particular type (see below) to which the lines’ parameters are
written. The matrix header is modified by the function so its cols or rows will contain the
number of lines detected. If storage is a matrix and the actual number of lines exceeds
the matrix size, the maximum possible number of lines is returned (in the case of standard
hough transform the lines are sorted by the accumulator value)
method The Hough transform variant, one of the following:
CV HOUGH STANDARD classical or standard Hough transform. Every line is represented by
two floating-point numbers , where is a distance between (0,0) point and the line,
and is the angle between x-axis and the normal to the line. Thus, the matrix must be
(the created sequence will be) of CV 32FC2 type //返回值ro ,theta
CV HOUGH PROBABILISTIC probabilistic Hough transform (more efficient in case if picture
contains a few long linear segments). It returns line segments rather than the whole
line. Each segment is represented by starting and ending points, and the matrix must
be (the created sequence will be) of CV 32SC4 type //返回值为 直线的两个端点
CV HOUGH MULTI SCALE multi-scale variant of the classical Hough transform. The lines are
encoded the same way as CV HOUGH STANDARD
rho Distance resolution in pixel-related units //rho 距离分辨率 ,理解直线的表达式 p = xcos(theta) + ysin(theta)的意思
theta Angle resolution measured in radians //theta的分辨率
threshold Threshold parameter. A line is returned by the function if the corresponding accumu-
lator value is greater than threshold //累积值的阈值
param1 The first method-dependent parameter:
For the classical Hough transform it is not used (0). //未用
For the probabilistic Hough transform it is the minimum line length. //最短直线长度
For the multi-scale Hough transform it is the divisor for the distance resolution . (The
coarse distance resolution will be and the accurate resolution will be param1).
param2 The second method-dependent parameter:
For the classical Hough transform it is not used (0). //未用
For the probabilistic Hough transform it is the maximum gap between line segments
lying on the same line to treat them as a single line segment (i.e. to join them). //直线的最小间距
For the multi-scale Hough transform it is the divisor for the angle resolution . (The
coarse angle resolution will be and the accurate resolution will be param2).
pt1与pt2点的计算方法 。
这里已知下图中的rh0 和θ,现在只需要求图中“任意”两点, 就能用cvLine画出pt1 -> pt2的直线 。
看看下图 就明白这里1000什么的是为什么了。
这里是取了点(x0,y0)在直线上上下1000的距离,那么用cvLine画出来的线段就是从pt1 -> pt2的了。
1.这个地方也许会出现检测出来的线段长度比pt1->pt2还大,即包含了我们画的线段 , 这是肯定的。
2. 还会出现本来线段没有pt1->pt2这么长,那么我们画的就会过长了。 也是肯定会出现的情况。
因为:CV_HOUGH_STANDARD方法 只能得出rh0 和 θ的值。 这两个值只能确定直线,而不能确定线段是从哪开始到哪结束。
此方法只能确定线段对应的直线。
如果你想检测出图中的线段是从哪到哪的话,请用CV_HOUGH_PROBABILISTIC .
- opencv 霍夫变换检测直线
- opencv 霍夫变换检测直线
- opencv 霍夫变换检测直线(转)
- opencv 霍夫变换检测直线
- opencv-霍夫变换 检测直线
- opencv 霍夫变换检测直线
- OpenCV笔记11:霍夫变换检测直线
- opencv 霍夫变换检测直线 例程中的1000
- 霍夫变换直线检测及opencv实现
- 霍夫变换直线检测及opencv实现
- 霍夫变换检测直线及openCV代码
- 【OpenCV】利用霍夫变换进行直线检测
- python+opencv实现霍夫变换检测直线
- opencv检测直线方法——霍夫变换
- OpenCV的霍夫变换(Hough Transform)直线检测
- 霍夫变换直线检测
- 霍夫变换检测直线
- 霍夫变换检测直线
- 获取元素节点(DOM基础 )
- 昨天在微信里看到的一篇文章,应该不算是个新闻,但是还是值得仔细读一下。特别是对于那些在一个公司干了很多年却没有发展,或者每年跳槽两三次的同学确实应该反思看一看。原文地址 以下是正文 我曾经带过一个
- 20150225学习笔记
- adb命令、adb shell与Linux各种命令
- Canvas实战---模仿GOOGLE浮动小球效果
- opencv 霍夫变换检测直线
- linux学习基础杂记
- 2015年,除了阿里和腾讯,我们还能不能聊点别的?
- 工厂模式
- *LeetCode-Rotate Array
- 3Swift\OC\Java中字符串的比较
- Java异常处理新特性
- Android项目工程目录详解
- SpringSide session短时间失效