If you want to track a blue color Object using open cv, then this tutorial will help you in understanding the step wise method use for identification and recognition a blue color Object.
// TrackColour.cpp : Defines the entry point for the console application.
//
#include “highgui.h”
#include <cv.h>
using namespace cv;
IplImage* GetThresholdedImage(IplImage* img)
{
// Convert the image into an HSV image
IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
cvCvtColor(img, imgHSV, CV_BGR2HSV);
IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);
// Values 20,100,100 to 30,255,255 working perfect for yellow at around 6pm
cvInRangeS(imgHSV, cvScalar(112, 100, 100), cvScalar(124, 255, 255), imgThreshed);//this will not recognize the yellow color
cvReleaseImage(&imgHSV);
return imgThreshed;
}
int main()
{
// Initialize capturing live feed from the camera
CvCapture* capture = 0;
capture = cvCaptureFromCAM(0);
// Couldn’t get a device? Throw an error and quit
if(!capture)
{
printf(“Could not initialize capturing…\n”);
return -1;
}
cvNamedWindow(“video”);
// the tracked positions of the ball
IplImage* imgScribble = NULL;
// An infinite loop
while(true)
{
// Will hold a frame captured from the camera
IplImage* frame = 0;
frame = cvQueryFrame(capture);
// If we couldn’t grab a frame… quit
if(!frame)
break;
// If this is the first frame, we need to initialize it
imgScribble = cvCreateImage(cvGetSize(frame), 8, 3);//we are resfreshing it agian and again so that the circle draw from previous fram should not be availble with the new one circle
// Holds the yellow thresholded image (yellow = white, rest = black)
IplImage* imgYellowThresh = GetThresholdedImage(frame);
// Calculate the moments to estimate the position of the ball
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
cvMoments(imgYellowThresh, moments, 1);
// The actual moment values
double moment10 = cvGetSpatialMoment(moments, 1, 0);
double moment01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
// Holding the last and current ball positions
static int posX = 0;
static int posY = 0;
int lastX = posX;
int lastY = posY;
posX = moment10/area;
posY = moment01/area;
//preparing the color of the circle
CvScalar blue = CV_RGB(0,0,250);
CvPoint pt2 = cvPoint(posX,posY);
cvCircle(imgScribble,pt2,30,blue,2,8);//draw a circle
// Add fram from CAM and the empty image where we are drawing the line
cvAdd(frame, imgScribble, frame);
cvShowImage(“video”, frame);//if we write “imgScribble” here then we will got an empty image screen where line will draw.
// Wait for a keypress
int c = cvWaitKey(10);
if(c==27)//if i press “escape” key
break;
// Release the thresholded image… we need no memory leaks.. please
cvReleaseImage(&imgYellowThresh);
delete moments;
}
// We’re done using the camera. Other applications can now use it
cvReleaseCapture(&capture);
return 0;
}
and the result which i got :
