DEV Community

Rahul Parajuli
Rahul Parajuli

Posted on

Motion Detection Using OpenCV for begineer.

A beginner guide to motion detection using OpenCV.

For a beginner, which was when I was studying at my university. I studied about OpenCV and its application and decided to work in a computer vision related project.

Image description
Motion Detection is magic when used in a way that is required, it can be used in vehicle counting system, anomaly detection during exam hall (which is - I don't recommend, you know what I mean). But on a serious side this sort of libraries can be used in surveillance system which can be utilized more efficiently that your neighbor's eyes.

Video is all about images that are captured continuously, also known as frames. You know FPS - its that images per second that we are talking about here.

Lets have quick look into pseudo code-

  1. First we initiate our libraries (pandas, numpy, cv2 (computer vision)).
  2. We declare initial state and motion track list.
  3. to initialize the camera module its simple than you can ever imagine (cv2.VideoCapture(0)). {bam, pretty cool, right?}
  4. We use while loop to continuously loop through every images captured.
  5. We can also use various color conversion in cv2 like, grayscale, gaussian blur etc.
  6. We now store each frames in initial state to capture initial state of the video captured.
  7. Now we create threshold frame using absolute difference between initial state and current scaled frame.
  8. We create a contours using that threshold frame and loop through it to decide a rectangle boundary to any object in motion.
  9. Jeez, you made it through all these lecture, now lets look at the code.

follow through my github, for the code - github

Happy Coding!

Top comments (0)