Thursday, July 3, 2014

Motion detection and Tracking in Matlab

           Hi guys, This is my first blog so dnt mind if there is a mistake. I hope this helps a lot of people. Its basically about a vision system that can detect any uni-color object in any given environment and track it to some extent. So if anyone needs any help, i ll look forward to it. 



            Am trying to create this simple blog bcoz i found it hard to develop this project with my amateur level image processing skills. So basically i am gonna tell you what u guys need for your kit development and the stuff u guys need to read, understand and its links.

  • Matlab 2013b or later (torrentz)
  • Two Webcams (CCD Cameras better), (at present u can go with one camera, later i ll update this blog to work under the stereo vision system to work better)
  • Two Servo Motors (Hobby servos will be sufficient with required torque based on the weight of ur pan/tilt mechanism)
  • A Pan/Tilt Assembly (its ur choice, u can find a lot of models in internet)
  • Arduino Uno microcontroller board
  • Some single strand connecting wires for connecting servos and microcontroller
  • basic programming knowledge
  • patience to read stuff to understand 

Let me explain the process in basic steps:
  • Detection process
    1. Create a basic background model of the current field of view of the camera
    2. Find a new object entering the environment using a gaussian method.
  • Tracking process
    1. based on the image obtained from detection method, create a template of object as reference image.
    2. use the reference image to find the object in successive video frames based on histogram based tracking.
         Rather than the existing motion tracking algorithms, here i have used two different algorithms. Gaussian Mixture Model for background modelling, motion detection respectively and CAMShift algorithm for histogram based tracking. You can read in detail about it in MATLAB help files. They have explained in detail with very good examples.

         From the result of tracking we get the center of object in terms of pixels and we compare it with the center of the screen. Then the position of object with respect to center of the screen is found and give the corresponding signal to the the controller board to rotate the servo motors till it coincides with the center of the screen. But as we are using hobby servos we cannot achieve a accuracy of 1 pixel and we cannot get a perfect match. So instead we approximate it to 10 pixels. 

        All the matlab files have been attached, and i have commented it properly so that u can understand easily. (Main.m) matlab file contains the main code and all other files are subroutine files that are called in main program. 

     (background.m and motion_detection.m) matlab subroutines does the detection steps mentioned above. (motion_tracking.m) matlab subroutines does the first step in tracking process and rest of the process are carried out in main program. (Pantilt.m) matlab subroutine finds the respective pan/tilt direction and returns the character value which are to be send over serial port to arduino uno.

    Once again if u download the below links and read the commented portion u ll have a better understanding of the logic i mentioned above.

            In the above picture, (a) is the background (background can be full of different objects, the program ll still work, just make sure none of them are moving a lot). (b) is a picture of a new object entering the background (in this case, its my phone). (c) is a image of showing whats the foreground and whats the background. (d) is a image of the new object which is taken as a template. The above four pictures are the detection process.


         We use the template image we got from detection process to find the same object in subsequent frames and draw a bounding box around it as shown in above images, thus tracking it. further we calculate the angle to which the servo motor has to pan/tilt to keep the tracked object in the center of the screen and send to arduino uno over serial port communication to move the servo respectively. Below are the tracking process showed in number of stages in a number of images.  
                   

Here are the links to matlab files and arduino file.

For any problems in program, plz feel free to contact me.

P.S: The arduino program was open source program from the arduino website. I tweaked it a little to my use.