我按照此页面中的说明进行操作:http ://wiki.elphel.com/index.php?title=OpenCV_Tennis_balls_recognizing_tutorial
来检测网球。此代码可以为 Python 运行,并且它的要求是 V4L/AVLD 用于形态学操作。它的使用功能cvClose()
和cvOpen()
扩张和侵蚀面具。我用 C++ 编写我的代码,所以使用cvDilate()
和cvErode()
代替,但结果不如那个网站好。
这是我的结果:output.jpg。(对不起,因为我没有足够的声誉来发布图片 T_T)这是我的完整代码:
#include "highgui.h"
#include "cv.h"
void main()
{
IplImage* img = cvLoadImage("tennis.jpg",1);
CvSize size = cvGetSize(img);
IplImage *hsv = cvCreateImage(size, IPL_DEPTH_8U, 3);
cvCvtColor(img, hsv, CV_BGR2HSV);
CvMat *mask = cvCreateMat(size.height, size.width, CV_8UC1);
cvInRangeS(hsv, cvScalar(0.11*256, 0.60*256, 0.20*256, 0),
cvScalar(0.14*256, 1.00*256, 1.00*256, 0), mask);
cvReleaseImage(&hsv);
IplConvKernel *se21 = cvCreateStructuringElementEx(21, 21, 10, 10, CV_SHAPE_RECT, NULL);
IplConvKernel *se11 = cvCreateStructuringElementEx(11, 11, 5, 5, CV_SHAPE_RECT, NULL);
cvErode(mask, mask, se21);
cvDilate(mask, mask, se11);
cvReleaseStructuringElement(&se21);
cvReleaseStructuringElement(&se11);
/* Copy mask into a grayscale image */
IplImage *hough_in = cvCreateImage(size, 8, 1);
cvCopy(mask, hough_in, NULL);
cvSmooth(hough_in, hough_in, CV_GAUSSIAN, 15, 15, 0, 0);
/* Run the Hough function */
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq *circles = cvHoughCircles(hough_in, storage,CV_HOUGH_GRADIENT, 4, size.height/10, 100, 40, 0, 0);
cvReleaseMemStorage(&storage);
int i;
for (i = 0; i < circles->total; i++) {
float *p = (float*)cvGetSeqElem(circles, i);
CvPoint center = cvPoint(cvRound(p[0]),cvRound(p[1]));
CvScalar val = cvGet2D(mask, center.y, center.x);
if (val.val[0] < 1) continue;
cvCircle(img, center, 3, CV_RGB(0,255,0), -1, CV_AA, 0);
cvCircle(img, center, cvRound(p[2]), CV_RGB(255,0,0), 3, CV_AA, 0);
cvCircle(mask, center, 3, CV_RGB(0,255,0), -1, CV_AA, 0);
cvCircle(mask, center, cvRound(p[2]), CV_RGB(255,0,0), 3, CV_AA, 0);
}
cvNamedWindow( "Output", CV_WINDOW_AUTOSIZE );
cvShowImage( "Output", img );
cvNamedWindow( "mask", CV_WINDOW_AUTOSIZE );
cvShowImage( "mask", mask );
cvWaitKey(0);
}
有人可以帮助我获得 V4L/AVLD 并改进此代码吗?非常感谢你。