您的位置首页生活百科

【opencv+VS】图片中找圆

【opencv+VS】图片中找圆

的有关信息介绍如下:

【opencv+VS】图片中找圆

#include

#include

#include

int main(int argc, char** argv)

{

IplImage* img = cvCreateImage = cvLoadImage("img.jpg");

if(img)

{

IplImage* gray = cvCreateImage(

cvGetSize(img), 8, 1 );

CvMemStorage* storage =

cvCreateMemStorage(0);

cvCvtColor( img, gray, CV_BGR2GRAY

);

cvSmooth( gray, gray, CV_GAUSSIAN,

9, 9 ); // smooth it, otherwise a lot of false circles //may be detected

CvSeq* circles = cvHoughCircles(

gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 );

int i;

for( i = 0; i <

circles->total; i++ )

{

float*

p = (float*)cvGetSeqElem( circles, i );

cvCircle( img, cvPoint(cvRound(p),cvRound(p)), 3, CV_RGB(0,255,0), -1, 8,

0 );

cvCircle( img, cvPoint(cvRound(p),cvRound(p)), cvRound(p),

CV_RGB(255,0,0), 3, 8, 0 );

}

cvNamedWindow( "circles",

1 );

cvShowImage( "circles",

img );

cvWaitKey(0);

}

return 0;

}

这是未转换成yuv格式的,将输入的图像灰度化进行处理

如果想要转成yuv格式,调用cvCvtColor(img,yuv,CV_BGR2YUV);即可得到yuv格式图像,其中的y分量即为灰度图