OpenCV 特定物体检测

发布于 2024-12-09 08:52:18 字数 113 浏览 0 评论 0原文

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

After doing some research and reading information about OpenCV object detection, I am still not sure on how can I detect a stick in a video frame. What would be the best way so i can detect even if the user moves it around. I'll be using the stick as a sword and make a lightsaber out of it. Any points on where I can start? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-12-16 08:52:18

对此的首选答案通常是霍夫线变换。霍夫变换旨在查找场景中的直线(或其他轮廓),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;
}

有点冗长,但你明白了!

The go-to answer for this would usually be the Hough line transform. The Hough transform is designed to find straight lines (or other contours) in the scene, and OpenCV can parameterize these lines so you get the endpoints coordinates. But, word to the wise, if you are doing lightsaber effects, you don't need to go that far - just paint the stick orange and do a chroma key. Standard feature of Adobe Premiere, Final Cut Pro, Sony Vegas, etc. The OpenCV version of this is to convert your frame to HSV color mode, and isolate regions of the picture that lie in your desired hue and saturation region.

http://opencv.itseez.com/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html?highlight=hough

Here is an old routine I wrote as an example:

//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;
}

A little verbose, but you get the idea!

生寂 2024-12-16 08:52:18

我的老教授总是说,计算机视觉的第一定律是对图像尽一切努力,让你的工作变得更轻松。

如果您可以控制棍子的外观,那么您可能会很幸运地为棍子涂上非常特定的颜色(霓虹粉红色或不太可能出现在背景中的颜色),然后使用颜色分割与连接组件标记。那会非常快。

My old professor always said that the first law of computer vision is to do whatever you can to the image to make your job easier.

If you have control over the stick's appearance, then you might have the best luck painting the stick a very specific color --- neon pink or something that isn't likely to appear in the background --- and then using color segmentation combined with connected component labeling. That would be very fast.

笑咖 2024-12-16 08:52:18

您可以首先遵循为 OpenCV 编写的人脸识别(训练和检测)技术。

如果您正在寻找具体步骤,请告诉我。

You can start by following the face-recognition (training & detection) techniques written for OpenCV.

If you are looking for specific steps, let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文