图像上找线、圆

来源:互联网 发布:技术员软件管家 编辑:程序博客网 时间:2024/06/05 14:46
/* 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

pattern512.JPG
orginal

lines.jpg
lines.jpg (99.05 KiB) 被浏览 4777 次


cvHoughCircles 
cccc1.jpg
cccc1.jpg (12.69 KiB) 被浏览 4777 次
circles.jpg

(VS2008+open1.1,注意图片路径)

[cpp] view plaincopy
  1. <span style="background-color: rgb(255, 255, 255);">#include "stdafx.h"  
  2. #include <iostream>  
  3.   
  4. #include <cv.h>  
  5. #include <cxcore.h>  
  6. #include <highgui.h>  
  7. #include <math.h>  
  8. using namespace std;  
  9. /* 
  10. coded and updated by Huang, Haiqiao 2010-01-13 
  11. Examples of using cvHoughLines2 and cvHoughCircles 
  12. */  
  13.   
  14. int main(int argc, char** argv)  
  15. {  
  16.    int i;  
  17.    cout << "Lines and Circles OpenCV!"<<endl;  //\\cameraman.jpg  
  18.    char* filename="D:\\OpenCV_stuff\\SampleImages\\pattern512.bmp";  
  19.    IplImage* imgRGB = cvLoadImage(filename);   
  20.    IplImage* imgGrey = cvLoadImage(filename,0); //grey image  
  21.    char* filename1="D:\\OpenCV_stuff\\SampleImages\\circle3.jpg";  
  22.    IplImage* imgcircle = cvLoadImage(filename1,1); //color image  
  23.    if (imgGrey==NULL){  
  24.       cout << "No valid image input."<<endl;   
  25.       char c=getchar();  
  26.       return 1;  
  27.    }   
  28.      
  29.    IplImage* cannyImg  = cvCreateImage(cvSize(imgGrey->width,imgGrey->height),IPL_DEPTH_8U,1);  
  30.    cvCanny(imgGrey,cannyImg,170,200,3);  
  31.   
  32.    //************hough lines**probabilistic********//  
  33.    CvMemStorage* storage = cvCreateMemStorage(0);  
  34.         CvSeq* lines = 0;   
  35.    double rho=1;  
  36.    double theta=CV_PI/180;  
  37.    double threshold=30;  
  38.    double min_length=40;//CV_HOUGH_PROBABILISTIC  
  39.    double sepration_connection=20;//CV_HOUGH_PROBABILISTIC  
  40.   
  41.    //binary image is needed. 
  42. cvClearMemStorage(storage); 
  43.     lines = cvHoughLines2(  
  44.       cannyImg,   
  45.       storage,   
  46.       CV_HOUGH_PROBABILISTIC,   
  47.       rho,  
  48.       theta,   
  49.       threshold,  
  50.       min_length,   
  51.       sepration_connection);  
  52.   
  53.    //draw lines found by cvHoughLines2  
  54.     for( i = 0; i < lines->total; i++ )  
  55.     {  
  56.        CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);  
  57.        cvLine(imgRGB, line[0], line[1], CV_RGB(255,0,0),1, 8 );//cannyImgColor  
  58.     }   
  59.      
  60.   
  61.   
  62.    /* 
  63.       Circle identification 
  64.    */    
  65.    IplImage* grayimgcircle = cvCreateImage( cvGetSize(imgcircle), 8, 1 );  
  66.    CvMemStorage* storagecircle = cvCreateMemStorage(0);  
  67.     cvCvtColor( imgcircle, grayimgcircle, CV_BGR2GRAY );  
  68.    // smooth it, otherwise a lot of false circles may be detected  
  69.     cvSmooth( grayimgcircle, grayimgcircle, CV_GAUSSIAN, 9, 9 );   
  70.       
  71.    double dp=2;  
  72.    double min_dist=10;  
  73.    int min_radius=5;  
  74.    int max_radius=150;   
  75.    //only greyimage is needed. cvHoughCircles would call cvSobel() internally. 
  76. cvClearMemStorage(storage); 
  77.    CvSeq* circles = cvHoughCircles(   
  78.          grayimgcircle,   
  79.          storagecircle,   
  80.          CV_HOUGH_GRADIENT,   
  81.          dp,   
  82.          min_dist,   
  83.          min_radius,   
  84.          max_radius );  
  85.   
  86.     //draw found curves  
  87.     for( i = 0; i < circles->total; i++ )  
  88.     {  
  89.          float* p = (float*)cvGetSeqElem( circles, i );  
  90.          cvCircle( imgcircle, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );  
  91.     }  
  92.      
  93.   
  94.   
  95.     cvNamedWindow( "Circles", CV_WINDOW_AUTOSIZE );  
  96.     cvShowImage( "Circles", imgcircle );  
  97.    cvNamedWindow("Original", CV_WINDOW_AUTOSIZE );  
  98.    cvShowImage( "Original", imgRGB );  
  99.   
  100.    cvWaitKey(0);  
  101.   
  102.    cvDestroyWindow("HoughLines");  
  103.    cvReleaseImage(&imgGrey);  
  104.    cvDestroyWindow("Original");  
  105.    cvReleaseImage(&imgcircle);  
  106.    cvDestroyWindow("Circles");  
  107.      
  108.    //char c=getchar();  
  109.    return 0;  
  110. }</span>  




0 0
原创粉丝点击