Camshift algorithm: 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 (mean shift) Algorithm 3) camshift Algorithm (Note: The above translation is purely personal !) 1) 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 section describes the opencv for Back Projection calculation.Code. 1. Prepare an image that only contains the target to be tracked, convert the color space to the HSI space, and obtain the H component: Iplimage * target = 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 must be quantified appropriately, here we 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. 2. Mean Shift: Camshift algorithm, opencv implements the 2-Mean Shift Algorithm Here we come to the camshift algorithm, the second part of opencv implementation. This time we will focus on the Mean Shift algorithm. Before discussing the Mean Shift algorithm, we first discuss how to calculate the center of gravity (Mass Center) of a region in a 2D probability distribution image. The center of gravity can be calculated using the following formula: 1. Calculate the moment of order 0 in the region For (INT I = 0; I For (Int J = 0; j <width; j ++) M00 + = I (I, j) 2. Moment of level 1 in the region: For (INT I = 0; I For (Int J = 0; j <width; j ++) { M10 + = I * I (I, j ); M01 + = J * I (I, j ); } 3. The mass center is: XC = M10/m00; YC = M01/m00 Next, we will discuss the specific steps of the Mean Shift algorithm. The Mean Shift algorithm can be divided into the following four steps: 1. Select the window size and initial position. 2. Calculate the Mass Center in the window. 3. Adjust the center of the window to the Mass Center. 4. Repeat 2 and 3 until the window center is "converged", that is, the distance between each window movement is smaller than a certain threshold. In opencv, the mean shift algorithm function is provided. The prototype of the function is: Int cvmeanshift (iplimage * imgprob, cvrect win, Cvtermcriteria criteria, cvconnectedcomp * out ); The required parameters are: 1. iplimage * imgprob: 2D probability distribution image, passed in; 2. cvrect win: initial window, passed in; 3. cvtermcriteria criteria: Criteria for stopping iteration, passed in; 4. cvconnectedcomp * out: the query result is output. (Note: To construct a cvtermcriteria variable, three parameters are required. One is type, the other is the maximum number of iterations, and the last one represents a specific threshold. For example, you can construct criteria: Criteria = cvtermcriteria (cv_termcrit_iter | cv_termcrit_eps, 10, 0.1 ).) Returned parameters: 1. INT: number of iterations. Implementation Code: temporarily missing 3. camshift: Camshift algorithm, opencv implementation (3): camshift Algorithm 1. principle after learning about the meanshift algorithm, we extend the meanshift algorithm to a continuous image sequence (generally a video image sequence), forming the camshift algorithm. The camshift algorithm is called "continuously apapaptive mean-shift". Its basic idea is to perform the meanshift operation on all frames of a video image, the result of the previous frame (that is, the center and size of the search window) is used as the initial value of the search window of the next frame of the meanshift algorithm. In this way, the target tracking can be realized through iteration. the entire algorithm consists of five steps: Step 1: set the entire image as the search area. Step 2: the size and position of the search window. Step 3: Calculate the color probability distribution in the search window, the size of this area is slightly larger than that of the search window. Step 4: Run meanshift. Obtain the new position and size of the search window. Step 5: In the next video image, use the value obtained in step 3 to initialize the position and size of the search window. Jump to Step 3 to continue running. 2. implementation In opencv, there is a function that implements the camshift algorithm. The prototype of this function is: cvcamshift (iplimage * imgprob, cvrect win, cvtermcriteria criteria, cvconnectedcomp * Out, cvbox2d * box = 0); where: imgprob: Color probability distribution image. win: search window initial value. Criteria: a criterion used to determine whether the search is stopped. out: Save the calculation result, including the location and area of the new search window. box: contains the smallest rectangle of the tracked object. Note: 1. In the directory of opencv 3.1 beta, there is an example of camshift. Unfortunately, the target tracking in this example is semi-automated, that is, you need to manually select a target. I am trying to implement fully-automated target tracking. I hope you can communicate with us in this regard. (Automatically implemented) |