Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV specific object detection

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!

like image 546
Caloyskie Avatar asked Dec 22 '22 07:12

Caloyskie


1 Answers

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!

like image 195
Matt Montag Avatar answered Jan 05 '23 06:01

Matt Montag