/* Finds lines on binary image using one of several methods.
line_storage is either memory storage or 1 x <max number of lines> CvMat, its
number of columns is changed by the function.
method is one of CV_HOUGH_*;
rho, theta and threshold are used for each of those methods;
param1 ~ line length, param2 ~ line gap - for probabilistic,
param1 ~ srn, param2 ~ stn - for multi-scale */
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));
找线实例:
CvMemStorage* _pStorage = cvCreateMemStorage(0);
CvSeq* _lines=cvHoughLines2(_grayImg,_pStorage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,50,5,10);
/* Finds circles in the image */
CVAPI(CvSeq*) cvHoughCircles( CvArr* image, void* circle_storage,
int method, double dp, double min_dist,
double param1 CV_DEFAULT(100),
double param2 CV_DEFAULT(100),
int min_radius CV_DEFAULT(0),
int max_radius CV_DEFAULT(0));
切记:在循环使用中,每次检测完后要cvClearMemStorage(storage);否则的话一直检测,则存储的线或圆的内存会一直保存在storage中,就会出现内存泄露。
cvHoughLines2和cvHoughCircles使用范例。有图,有程序。
cvHoughLines2需要的是Binary image。 cvHoughCircles 需要的是灰度图,而且它会自动调用cvSobel函数。
关于cvHoughCircles多说一句,之前最好用cvSmooth一下,否则会有很多错误的圆被识别出来。
cvHoughLines2
-
-
lines.jpg (99.05 KiB) 被浏览 4777 次
cvHoughCircles
-
-
cccc1.jpg (12.69 KiB) 被浏览 4777 次
(VS2008+open1.1,注意图片路径)
- <span style="background-color: rgb(255, 255, 255);">#include "stdafx.h"
- #include <iostream>
-
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- #include <math.h>
- using namespace std;
-
-
-
-
-
- int main(int argc, char** argv)
- {
- int i;
- cout << "Lines and Circles OpenCV!"<<endl;
- char* filename="D:\\OpenCV_stuff\\SampleImages\\pattern512.bmp";
- IplImage* imgRGB = cvLoadImage(filename);
- IplImage* imgGrey = cvLoadImage(filename,0);
- char* filename1="D:\\OpenCV_stuff\\SampleImages\\circle3.jpg";
- IplImage* imgcircle = cvLoadImage(filename1,1);
- if (imgGrey==NULL){
- cout << "No valid image input."<<endl;
- char c=getchar();
- return 1;
- }
-
- IplImage* cannyImg = cvCreateImage(cvSize(imgGrey->width,imgGrey->height),IPL_DEPTH_8U,1);
- cvCanny(imgGrey,cannyImg,170,200,3);
-
-
- CvMemStorage* storage = cvCreateMemStorage(0);
- CvSeq* lines = 0;
- double rho=1;
- double theta=CV_PI/180;
- double threshold=30;
- double min_length=40;
- double sepration_connection=20;
-
-
- cvClearMemStorage(storage);
- lines = cvHoughLines2(
- cannyImg,
- storage,
- CV_HOUGH_PROBABILISTIC,
- rho,
- theta,
- threshold,
- min_length,
- sepration_connection);
-
-
- for( i = 0; i < lines->total; i++ )
- {
- CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
- cvLine(imgRGB, line[0], line[1], CV_RGB(255,0,0),1, 8 );
- }
-
-
-
-
-
-
- IplImage* grayimgcircle = cvCreateImage( cvGetSize(imgcircle), 8, 1 );
- CvMemStorage* storagecircle = cvCreateMemStorage(0);
- cvCvtColor( imgcircle, grayimgcircle, CV_BGR2GRAY );
-
- cvSmooth( grayimgcircle, grayimgcircle, CV_GAUSSIAN, 9, 9 );
-
- double dp=2;
- double min_dist=10;
- int min_radius=5;
- int max_radius=150;
-
- cvClearMemStorage(storage);
- CvSeq* circles = cvHoughCircles(
- grayimgcircle,
- storagecircle,
- CV_HOUGH_GRADIENT,
- dp,
- min_dist,
- min_radius,
- max_radius );
-
-
- for( i = 0; i < circles->total; i++ )
- {
- float* p = (float*)cvGetSeqElem( circles, i );
- cvCircle( imgcircle, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );
- }
-
-
-
- cvNamedWindow( "Circles", CV_WINDOW_AUTOSIZE );
- cvShowImage( "Circles", imgcircle );
- cvNamedWindow("Original", CV_WINDOW_AUTOSIZE );
- cvShowImage( "Original", imgRGB );
-
- cvWaitKey(0);
-
- cvDestroyWindow("HoughLines");
- cvReleaseImage(&imgGrey);
- cvDestroyWindow("Original");
- cvReleaseImage(&imgcircle);
- cvDestroyWindow("Circles");
-
-
- return 0;
- }</span>