Image Processing With Python: Image Differencing for Video Feeds

How to detect the changes and movement in video frames?

JManansala
The Startup
6 min readJan 31, 2021

--

(Original Image by GMA News TV)

In this post, we will learn how we can apply the image differencing to detect changes and movement in each frame of a video.

Let’s begin.

As usual, we import libraries such as numpy and matplotlib. We also import specific functions from the skimage library. Lastly, we will introduce the cv2 library to convert video files to images.

But first, let us define what image differencing is. Basically, it is an image processing technique used to determine changes between images by finding the difference between each pixel in each image. The generated image highlights the pixel values’ changes, while all pixels that have no changes will be zero. The critical point is that the two images must be properly aligned for this technique to work.

Let’s use a traffic surveillance feed from a major highway in Metro Manila, Philippines. (Video courtesy of GMA News TV, Philippines).

To implement image processing techniques on this video file, we will convert a short clip of this surveillance feed into images using the video frames. For this problem, the image’s capture interval will be 5 seconds — to emphasize the difference between images.

After converting the image, let’s inspect the image frame

(Original Image by GMA News TV)

Great! For ease of our analysis, we will only be using two frames in this example. Let’s use Frame #10 and #12 because this is where the train is the clearest.

(Original Image by GMA News TV)

Now, let’s apply image processing techniques to prepare our image for image differencing. Since the image is taken at an angle, we can apply the homography matrix to change the image’s perspective. I will no longer discuss how the homography matrix works. If you are not yet familiar with this technique, I suggest reading it in my previous article!

(Original Image by GMA News TV)
(Original Image by GMA News TV)

We can see that by applying the homography matrix, the objects at the backside of the image are now comparatively the same scale as the objects at the image’s front side.

Now, let’s apply image differencing! This can be computed by taking the difference between two aligned images. Note these images should be in the grayscale format for more straightforward analysis.

(Original Image by GMA News TV)

By taking the difference between the two frames of the video, we can see all the changes within that period. Notice that the image shows the train on the tracks and the vehicles on the highways. These are all of the objects that moved! Moreover, notice how the roads, buildings, and pedestrian overpass are not reflected on the differenced image — this is because no changes happened on these objects!

Notice that the objects on the right side of the highway have red specks on top of the blue specks. Meanwhile, on the left side of the highway, the red specks are below the blue specks. This is because the initial position of the object is reflected as blue specks in the resulting image. Meanwhile, the final position is reflected as red specks.

Now, let’s see how far the object went from its initial to its final position. To do this, let’s get the absolute difference of the image so that the object in its initial and final position looks the same.

(Original Image by GMA News TV)

Great! Now, let’s use the template matching algorithm to detect these objects. Again, I will no longer discuss how template matching works. If you are not yet familiar with this technique, I suggest reading it in my previous article!

Let’s use the train as the object!

(Original Image by GMA News TV)
(Original Image by GMA News TV)

By using object detection, we were able to pinpoint where the object’s initial and final positions are in the image! We can now compute the distance traveled by the train by getting the difference between the coordinates of the initial and final position!

Moreover, we know that the time interval between the initial and final position, we can also compute for the train’s speed! Amazing, right?

In summary

We have explored how to use image differencing to detect changes and movement of objects in video feeds. We have also applied image processing techniques such as homography matrix and template matching to enhance our analysis of this problem further.

I hope you were able to appreciate the potential of image differencing in solving real-world problems. Stay tuned for more posts!

Want to learn more? Check out my GitHub repository at this link!

--

--