CamShift is a motion tracking algorithm called "Continuously Apative Mean-Shift. It is used to track the color information of moving objects in video images. I divide this algorithm into three parts for ease of understanding:
1) Back Projection Calculation
2) Mean Shift Algorithm
3) CamShift Algorithm
Here we will mainly discuss Back Projection, and continue to discuss the next two algorithms in subsequent articles.
Back Projection
The steps for calculating Back Projection are as follows:
1. Calculate the color histogram of the tracked target. In various color spaces, only the H component in the HSI space (or a color space similar to the HSI space) can represent color information. Therefore, in the specific calculation process, the values of other color spaces are first converted to the HSI space, and then the H component is used for 1D histogram calculation.
2. Convert the original image into a color Probability Distribution Image Based on the obtained color histogram. This process is called "Back Projection ".
The histogram function in OpenCV contains the Back Projection function. The function prototype is:
Void cvCalcBackProject (IplImage ** img, CvArr ** backproject, const CvHistogram * hist );
There are three parameters passed to this function:
1. IplImage ** img: stores the original image and inputs it.
2. CvArr ** backproject: stores the Back Projection result and outputs it.
3. CvHistogram * hist: stores the histogram and inputs
The following provides the OpenCV code for calculating Back Projection.
1. Prepare an image that only contains the target to be tracked, convert the color space to the HSV space, and obtain the H component:
IplImage * target1_cvloadimage(%target.bmp ",-1); // load the image
IplImage * target_hsv = cvCreateImage (cvGetSize (target), IPL_DEPTH_8U, 3 );
IplImage * target_hue = cvCreateImage (cvGetSize (target), IPL_DEPTH_8U, 3 );
CvCvtColor (target, target_hsv, CV_BGR2HSV); // convert to HSV space
CvSplit (target_hsv, target_hue, NULL); // obtain the H component
2. Calculate the histogram of H component, I .e. 1D histogram:
IplImage * h_plane = cvCreateImage (cvGetSize (target_hsv), IPL_DEPTH_8U, 1 );
Int hist_size [] = {255}; // quantize the value of H to [0,255]
Float * ranges [] ={ {0,360}; // The value range of the H component is [0,360)
CvHistogram * hist = cvCreateHist (1, hist_size, ranges, 1 );
CvCalcHist (& target_hue, hist, 0, NULL );
Here we need to consider the value range of the H component. The value range of the H component is [0,360). The value of this value range cannot be expressed by a byte. In order to be expressed by a byte, the H value needs to be quantified appropriately. Here we can quantify the range of H to [0,255].
4. Calculate Back Projection:
IplImage * rawImage;
//----------------
// Get from video frame, unsigned byte, one channel
//----------------
IplImage * result = cvCreateImage (cvGetSize (rawImage), IPL_DEPTH_8U, 1 );
CvCalcBackProject (& rawImage, result, hist );
5. result: The result is what we need.
Original article:Http://blog.csdn.net/houdy/archive/2004/11/10/175739.aspx