本文主要是介绍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
这篇关于Verybot之OpenCV应用二:霍夫变换查找圆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!