在做了一些研究并阅读了有关 OpenCV 对象检测的信息之后,我仍然不确定如何检测视频帧中的棍子。最好的方法是什么,这样即使用户移动它我也能检测到。我会用这根棍子当剑,然后用它做一把光剑。关于我可以从哪里开始的任何要点?谢谢!
最佳答案
对此的首选答案通常是霍夫线变换。霍夫变换旨在寻找场景中的直线(或其他轮廓),OpenCV 可以参数化这些直线,以便您获得端点坐标。但是,明智的话,如果你正在做光剑效果,你不需要走那么远 - 只需将棍子涂成橙色并做一个色度键。 Adobe Premiere、Final Cut Pro、Sony Vegas 等的标准功能。此功能的 OpenCV 版本是将您的帧转换为 HSV 颜色模式,并隔离位于所需色调和饱和度区域的图片区域。
http://opencv.itseez.com/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html?highlight=hough
下面是我写的一个老例程作为例子:
//Photoshop-style color range selection with hue and saturation parameters.
//Expects input image to be in Hue-Lightness-Saturation colorspace.
//Returns a binary mask image. Hue and saturation bounds expect values from 0 to 255.
IplImage* selectColorRange(IplImage *image, double lowerHueBound, double upperHueBound,
double lowerSaturationBound, double upperSaturationBound) {
cvSetImageCOI(image, 1); //select hue channel
IplImage* hue1 = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
cvCopy(image, hue1); //copy hue channel to hue1
cvFlip(hue1, hue1); //vertical-flip
IplImage* hue2 = cvCloneImage(hue1); //clone hue image
cvThreshold(hue1, hue1, lowerHueBound, 255, CV_THRESH_BINARY); //threshold lower bound
cvThreshold(hue2, hue2, upperHueBound, 255, CV_THRESH_BINARY_INV); //threshold inverse upper bound
cvAnd(hue1, hue2, hue1); //intersect the threshold pair, save into hue1
cvSetImageCOI(image, 3); //select saturation channel
IplImage* saturation1 = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
cvCopy(image, saturation1); //copy saturation channel to saturation1
cvFlip(saturation1, saturation1); //vertical-flip
IplImage* saturation2 = cvCloneImage(saturation1); //clone saturation image
cvThreshold(saturation1, saturation1, lowerSaturationBound, 255, CV_THRESH_BINARY); //threshold lower bound
cvThreshold(saturation2, saturation2, upperSaturationBound, 255, CV_THRESH_BINARY_INV); //threshold inverse upper bound
cvAnd(saturation1, saturation2, saturation1); //intersect the threshold pair, save into saturation1
cvAnd(saturation1, hue1, hue1); //intersect the matched hue and matched saturation regions
cvReleaseImage(&saturation1);
cvReleaseImage(&saturation2);
cvReleaseImage(&hue2);
return hue1;
}
有点冗长,但你明白了!
关于visual-c++ - OpenCV 特定物体检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7708179/