2011-10-10 101 views
4

在做了一些关于OpenCV对象检测的研究和阅读信息之后,我仍然不确定如何检测视频帧中的某个棒。什么是最好的方式,所以我可以检测到即使用户移动它。我会用这根棍子作为剑,并将光剑作为一把剑。我可以从哪里开始?谢谢!OpenCV特定对象检测

回答

7

这个问题的答案通常是霍夫线变换。 Hough变换旨在查找场景中的直线(或其他轮廓),并且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; 
} 

有点冗长,但你的想法!

+0

我应该使用blob跟踪器来获取我的棍子的长度吗?还是有其他方法来获得我棍子的起点和终点?谢谢! – Caloyskie

+0

你可以'查找你的二进制图像的Contours'。用'contourArea'找到最大的'contour',然后用'minAreaRect'获得'RotatedRect',一个围绕'contour'的边界形状。 “RotatedRect”的高度将是你的长度。 –

+0

我已经使用了findContours,我认为创建的矩形不是那么稳定,尤其是当对象正在移动时。然后我使用了Hough变换CV_HOUGH_PROBABILISTIC,它完成了这项工作。非常感谢。 – Caloyskie

2

您可以按照为OpenCV编写的面部识别(培训&检测)技术开始。

如果您正在寻找特定步骤,请告诉我。

+1

嗯...不知道多少适用于跟踪一根棍子。 –

+0

嗨马特,想像:一根棍子,通常被想象成一个圆柱形的物体。一个圆柱体,可以在3D空间中的任何位置。有些情况下,我们可以检测和跟踪类似圆柱形物体,如笔!同样,这些物体上的独特特征总是有助于改善检测/跟踪。所以,你可能必须对这个“棒子”上的显着特征进行分类。这样的对象的示例图像,无论是消极的还是积极的,都会帮助我改进对您的建议。 –

+0

有人,我猜你加了Hough变换代码来检测棒子!但是必须考虑任何物体的取向。闭塞是另一方面。 Lowe的论文可以在这方面提供帮助。 –

2

我的老教授总是说,计算机视觉的第一条定律是尽一切可能的形象来让你的工作更轻松。

如果你有控制棒的外观,那么你可能有最好的运气绘制棒的一个非常具体的颜色---霓虹粉红色或不可能出现在背景中的东西---然后使用颜色分割与连接组件标记相结合。这将是非常快的。