The answer to this question usually should be a Hough string conversion. The Hough transform is for finding straight lines (or other contours) in a scene, and OpenCV can parameterize these lines so you get the coordinates of the end points. But, wise word, if you make lightsaber effects, you donβt have to go that far - just draw an orange wand and make a key color. Standard feature of Adobe Premiere, Final Cut Pro, Sony Vegas, etc. OpenCV version is to convert your frame to HSV color mode and isolate areas of the image that lie in the desired area of ββhue and saturation.
http://opencv.itseez.com/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html?highlight=hough
Here is an old routine that 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 bit detailed, but you get the idea!