We have been directed to test a camshift detector for object tracking, which is even able to deal with changing object scale and orientation. After getting a light insight into it, it was obvious that we will only need a part of the camshift algorithm in our application, which is the meanshift algorithm. This simplification can be made due to the character of our scene. We need to track a ball, whose distance from the camera can be considered constant after some simplification (the camera is far enough from the surface of the magnetic platform and when the ball moves from the center of the platform to the border, the difference of distances from the camera in both cases is negligible with respect to the absolute value of distances).
MEANshift implementation
The algorithm was implemented in a Simulink model 'detect_s' using a Level-2 MATLAB S-function. In the beginning, the algorithm has to be initialized by histograms of HSV intensities of pixels that depict the object that should be tracked. In this phase, a user input is required. Single frames are displayed in a loop and the user selects the rectangular area in the image that contains the object. The histograms of HSV intensities are then updated using the pixels from the selected area. We used a square colored object for testing purposes, so that the rectangular area was not a problem. For a ball, however, it would be necessary to add masking with a circular binary mask that would remove the background pixels from the selected rectangular area.
The HSV histograms computed from the areas selected by the user are normalized after the initialization phase and they are used as discrete probability densities from that point. The processed frame is transformed into probability image using the discrete HSV probability densities as lookup tables for HSV intensity values of the pixels. (This is a backprojection of probability into the original image.) Afterwards, we enter the meanshift algorithm, which iteratively computes center of mass of the region of interest in the sense of probability and updates the center of the region of interest. The update is based on computation of zero- and first-order spatial moments of the probability image denoted as M00, M10 and M01.
$M_{00} = \displaystyle\sum_x\sum_y I(x,y), \\ M_{10} = \displaystyle\sum_x\sum_y x\times I(x,y), \\ M_{01} = \displaystyle\sum_x\sum_y y\times I(x,y),$
where I(x,y) is probability that HSV intensities at position (x,y) belong to the tracked object. The coordinates of the center of mass of the region of interest (and therefore also the updated coordinates of the center of the region of interest) are computed as
$x_c = \frac{M_{10}}{M_{00}}; y_c = \frac{M_{01}}{M_{00}}$
After the iterative process has converged, the estimate of position of the tracked object is the same as the center of the region of interest.
First, the algorithm was implemented as a MATLAB script, getting single snapshots from the Creative Live! webcam one by one. (The script itself in fact only adjusts the exposure settings of the camera and then calls the function 'MEANshift_m_function', which implements the meanshift algorithm. The main advantage and purpose of doing this was the simplicity of implementing a MATLAB script which made debugging quite comfortable. The main disadvantage was the communication with the webcam, which fell asleep after each frame acquisition, so it took nearly a second until it was possible to get another frame. The script also shows how to adjust exposure settings of the webcam through the interface of Image Acquisition Toolbox interface.
We had slight problem with illumination of the scene, because the lab is illuminated by fluorescent tubes that flash at 50Hz, which is the frequency of electric network. However, the camera acquires images asynchronously with respect to this frequency, which causes that the frames differ in exposition (some of them are underexposed). The meanshift algorithm is robust enough to deal with these changes, but it has a negative impact on the precision of tracking.MATLAB script
/Dropbox/MATLAB/detect_meanshift/MEANshift_test_script.mFirst, the algorithm was implemented as a MATLAB script, getting single snapshots from the Creative Live! webcam one by one. (The script itself in fact only adjusts the exposure settings of the camera and then calls the function 'MEANshift_m_function', which implements the meanshift algorithm. The main advantage and purpose of doing this was the simplicity of implementing a MATLAB script which made debugging quite comfortable. The main disadvantage was the communication with the webcam, which fell asleep after each frame acquisition, so it took nearly a second until it was possible to get another frame. The script also shows how to adjust exposure settings of the webcam through the interface of Image Acquisition Toolbox interface.
![]() |
Probability image created by backprojection of HSV histograms into original image |
Simulink implementation with Level-2 MATLAB S-function block
/Dropbox/MATLAB/detect_meanshift/detect_s.slx
We used the Level-2 MATLAB S-function block in Simulink because unlike the MATLAB function block, it has the capability to store its internal state between iterations. Therefore the whole implementation including the initialization of the histograms can be integrated in the S-function block. The S-function is implemented in file 'MEANshift_s_function'.
![]() |
Simulink model with Level-2 MATLAB S-function integrates all functionality into the S-function block |
Simulink implementation with MATLAB function block
/Dropbox/MATLAB/detect_meanshift/detect.slx
The main disadvantage of the Level-2 MATLAB S-function block is that it does not support automatic code generation for execution of the model in external mode (e.g. using RealTime Toolbox or xPC Target). To use such block in a model executed in external mode, one has to manually create a TLC file for the S-function. The TLC file tells the compiler how to convert model into a *.c file. It would be too complicated to create the TLC file, so we transformed the model to use a MATLAB function block, which supports automatic code generation. The initialization of the histograms was moved into InitFcn callback function of the model and we also had to add memory that stores the actual state of the algorithm into the model.
![]() |
Simulink model with MATLAB function, memory of the algorithm is realized by unit delays in feedback loop. |