Friday 27 September 2013

Motion Detection and Speed Estimation using OpenCV

Hello Reader,

                Action Detection and Speed Estimation, this is what I wanted to do. I started making this bit in my Undergraduate final year, as a part of the curriculum. At that time I had no idea of either Computer Vision or anything related to that. I researched for quite a long time (on the Internet, of course). But all the methods that I found, were very complex and computationally very expensive. What I wanted was a very minimal system which can work "Real-Time" on very less computationally powerful devices. The device that I was targeting at that time was Raspberry Pi (a recent release at that time).

               So, finally after months of research (I had sufficient knowledge of the field, and) putting everything aside I came up with a very simple solution (and very idiotic). Estimating and tracking speed of the action was always my first goal. So keeping everything aside, I divided speed of action in two parts, "Fast" and "Not Fast".  In detecting these two things I took advantage of the capturing limit of the camera (640 * 480, 25fps). When there is a motion above some level, these kind of cameras are not able to capture and digitize it successfully.

               Considering this camera limit, lets go to the steps which I took:
  1. Capture new frame and convert it to gray-scale (almost a first step everywhere)
  2. Subtract previous frame from the current frame (or we can say, calculate absolute difference between previous image and current image) (yeah, this is what we do to detect motion)
  3. Convert the difference image (which is gray-scale right now) to binary image using appropriate threshold (this can simplify the computation here onwards)
  4. Find contours of the difference image (okay, this will help us know the boundaries of things in motion)
  5. Estimate speed based on number of detected contours (you must be thinking : "HUH??")
                Okay then, let's fill in the void and try to understand the last step. Consider these sequence of images, in which the hand is moving very fast:






                   In the first image the hand is steady, and just about to start moving. Number of contours is 19. In second image, hand has started moving at a very fast speed. So, the camera is not able to capture it and digitize it properly. So number of contours we get is 9. Same thing is going to happen in next three images, number of contours found is 12, 10 and 9 respectively. Now, in the last image, hand has finished one cycle and has gone upwards again. Here, number of contours that we are detecting is 19.

                   These kind of events are the main base of the algorithm which estimates the speed of motion. Algorithm works this way:
  1. Wait for detection of contours more than a predefined maximum contour threshold
  2. Now, scan consecutive frames for number of contours
  3. If number of contours is less than a minimum contour threshold, keep scanning
  4. While scanning if the number of contours exceed the predefined maximum contour threshold, denote this event as a fast motion
                   Here is the source code for this application.

                   I hope you liked and enjoyed the post.

                   I am still researching for ways to detect Human Action Recognition. So all suggestions are most most most welcome.

                   Please leave comments.



EDIT : Here is a full term project based on this. :)

Thank You For Reading,
Hardik Madhu

6 comments: