Motion Detection with Open CV and C#

Deepak Battini
2 min readJul 13, 2018

Motion detection is an important concept and idea, which you might find lots of implementation in day to day life. A good example is the CCTV camera installed at your home or office, which can detect motion and start recording video and mak it sound smarter. This tutorial will show how simple it is to implement motion detection using Differential Images. As an example, we capture the input of a webcam and visualise the motions in it. I recorded a short video of what we want to achieve.

You must be familiar with the below piece of code which I have used a lot with my previous post and will be using in an upcoming post. This captures images in a loop and display as creating a nice video.

FrameSource cam = Cv2.CreateFrameSource_Camera(0);
Cv2.NamedWindow("Camera", WindowMode.FullScreen);
Mat frame = new Mat();
while (true)
{
cam.NextFrame(frame);
Cv2.ImShow("Camera", frame);
if (Cv2.WaitKey(1) == (int)ConsoleKey.Enter)
break;
}

How do we detect motion a motion when an object moves. We can use a differential image technique which is a result of subtracting two consecutive frames.

gdif(x,y)=g1(x,y)−g2(x,y)

So as you can see a differential image shows the difference between the two images. With those images, you can make movement visible.

--

--

Deepak Battini
Deepak Battini

Written by Deepak Battini

Programmer and founder of blazorly.com. passionate open-source contributor, loves to combine cutting-edge tech expertise.