【opencv+VS】图片中找圆
我采集一幅图片,jpf、png都行,然后转成YUV422格式,找图片中的圆,opencv怎么实现呢?...
我采集一幅图片,jpf、png都行,然后转成YUV422格式,找图片中的圆,opencv怎么实现呢?
展开
1个回答
展开全部
#include <cv.h>
#include <highgui.h>
#include <math.h>
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[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8,
0 );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]),
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分量即为灰度图
#include <highgui.h>
#include <math.h>
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[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8,
0 );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]),
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分量即为灰度图
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询