Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT

Motion Heatmap

5.00/5 (1 vote)
15 Dec 2017MIT2 min read 9.2K  
This sample application is useful to see movement patterns over time.

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

This sample application is useful to see movement patterns over time. For example, it could be used to see the usage of entrances to a factory floor over time, or patterns of shoppers in a store.

What You’ll Learn

  • Background subtraction
  • Application of a threshold
  • Accumulation of changed pixels over time
  • Add a color/heat map

Gather Your materials

Setup

  1. You need the extra modules installed for the MOG background subtractor. This tutorial was tested on Windows*, and the easiest way to install it was using:
    pip install opencv-contrib-python
  2. Download the vtest.avi video from https://github.com/opencv/opencv/blob/master/samples/data/vtest.avi and put it in the same folder as the python script.
  3. Run the python script. You should see a diff-overlay.jpg when it's done.

Get the Code

Code is included in this folder of the repository in the .py file.

How It Works

The main APIs used in OpenCV are:

The application takes each frame and first applies background subtraction using the cv2.bgsegm.createBackgroundSubtractorMOG() object to create a mask. A threshold is then applied to the mask to remove small amounts of movement, and also to set the accumulation value for each iteration. The result of the threshold is added to an accumulation image (one that starts out at all zero and gets added to each iteration without removing anything), which is what records the motion. At the very end, a color map is applied to the accumulated image so it's easier to see the motion. This colored image is then combined with a copy of the first frame using cv2.addWeighted to accomplish the overlay.

IMPORTANT NOTICE: This software is sample software. It is not designed or intended for use in any medical, life-saving or life-sustaining systems, transportation systems, nuclear systems, or for any other mission-critical application in which the failure of the system could lead to critical injury or death. The software may not be fully tested and may contain bugs or errors; it may not be intended or suitable for commercial release. No regulatory approvals for the software have been obtained, and therefore software may not be certified for use in certain countries or environments.

License

This article, along with any associated source code and files, is licensed under The MIT License