Verybot之OpenCV应用二:霍夫变换查找圆

        其实我是想通过这个程序来测试一下,OpenCV在Verybot上跑得怎么样,霍夫变换的原理就不多说了,下面是程序:

#include "cv.h"
#include "highgui.h"
#include "stdio.h"


int main(int argc, char** argv)
{
    cvNamedWindow("vedio",0);
    CvCapture* capture;
    if(1 == argc)
    {
        capture = cvCreateCameraCapture(0);
    }
    else
    {
        capture = cvCreateCameraCapture(atoi(argv[1]));
    }

    assert(NULL != capture);

	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320);

	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,240);
	
    IplImage* frame;
	
	char keyCode;

	frame = cvQueryFrame(capture);
    
	if(!frame)
    {
        return 0;
    }

    IplImage* gray = cvCreateImage( cvGetSize(frame), 8, 1 );    //创建一个Image用来存灰度的图像

	CvMemStorage* storage = cvCreateMemStorage(0);        
	
    while((keyCode = cvWaitKey(15)))
    {
        if(keyCode == 'q')
		{
			break;
		}
        frame = cvQueryFrame(capture);
        if(!frame)
        {
            break;
        }

        cvCvtColor( frame, gray, CV_BGR2GRAY );


        CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4,200,200,20 );    //霍夫变换查找圆
		
		int i=0;
		
		for( i = 0; i < circles->total; i++ )        //将找到的圆标识出来

        {

             float* p = (float*)cvGetSeqElem( circles, i );
			 
			 cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0),3);

			 printf("%d  ,%d  ,%d\n",cvRound(p[0]),cvRound(p[1]),cvRound(p[2]));
        }
		
        cvShowImage("vedio",frame);
    }

    cvReleaseImage(&frame);
    cvDestroyAllWindows();
    return 0;
}


 

        程序比较简单,cvHoughCircles就是查找圆所使用的函数,后面的这几个参数比较重要,需要根据实际情况进行调整,调整不好的话,会误检测出圆来。

        测试的效果感觉还是不错的,下面是一个检测的录像:

 

        http://v.youku.com/v_show/id_XNjYxNjkzNDMy.html

 

       

你可能感兴趣的:(opencv,Verybot)