Motion Detection using OpenCV

Akshul Goyal
5 min readMay 30, 2021


Sixth Installment of my Project Series

OpenCV Motion Detection on Raspberry Pi 3

In the 4th post of my series I explained SMTP based surveillance system. To demonstrate a trigger based system, I used a switch to trigger video recording whenever I want to do so.

But why are we talking about a trigger based system?
It doesn’t make sense to be storing video when there is no suspicious activity going on, when there is no motion at all. Making empty videos is just waste space. And searching through those unwanted recordings is just a waste of time. A redundant system.

This post will explain how to implement basic motion detection, that can be used later on for our trigger based surveillance system.

What is OpenCV? —
OpenCV is a open source Computer Vision Library, and it’s goal is to provide a simple-to-use computer vision infrastructure which can help people build sophisticated applications quickly. It contains over 500 functions that span many areas in vision, including factory product inspection, medical imaging, security, user interface, camera calibration, stereo vision, and robotics.
— Learning OpenCV, Gary Bradski and Adrian Kaehler, O’Reilly

Installing OpenCV —
I am referring to PiImageSearch’s tutorial to install OpenCV, and also this documentation.

Before start installation, just update the system — sudo apt update

1. Installing libraries and dependencies

$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev

2. Installing GTK (GUI backend)

$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install libcanberra-gtk*

3. Numerical optimization for OpenCV

$ sudo apt-get install libatlas-base-dev gfortran

4. Python3 development headers

$ sudo apt-get install python3.7-devAfter installing to check if the dev files are installed in correct directory check using this command
$ python3.7-config --includes
-I/usr/include/python3.7m -I/usr/include/python3.7mThe first path is expected and second is current path.
If they are not the same then you need to perform a copy command to change the current path
$ sudo cp /current/path/to/dev/file/pyconfig.h /usr/include/python3.7m
pyconfig.h is the dev file that you need to copy to correct directory

5. Install pip

$ sudo apt-get install python3-pipIf this won't work then use the following commands
$ wget
$ sudo python3
$ sudo apt autoremoveIf pip is already installed then make sure it's the latest version
$ pip --version
If not then -
$ pip install --upgrade pip

6. Virtual Environment

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/ ~/.cache/pip
Update .bashrc to finish installing the tools, use nano or similar text editor -
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/
save and exit
And finally create the virtual environment-
$ mkvirtualenv cv -p python3
Verify if cv environment is working or not -
$ workon cv
(cv) pi@raspberrypi:~$
(cv) indicates that we are inside a virtual environment

7. Other Libraries

Install Numpy
$ pip install numpy
Install imutils
$ pip install imutils

8. Finally OpenCV
When I was trying to install OpenCV it was taking a lot of time to get installed, and RasPi was hanging even after I tried installing repeatedly. Increasing Swap Memory let’s OpenCV compile without memory exhaustion and Pi will not hang. Even after increasing swap memory it took around 5 hrs for installation to complete on my Pi 3.

$ sudo nano /etc/dphys-swapfile

save and exit. Check swap memory using these commands —

$ free -m
$ swapon -s

Increasing the swap memory takes up space from SD card only. So after increasing the swap memory only perform OpenCV installation

Now to install OpenCV

$ pip install opencv-python
$ pip install opencv-contrib-python // Ref.

After the installation is done revert the swap memeory back to it’s original value. CONF_SWAPSIZE=100

Verify if OpenCV is installed successfully —

Verifying OpenCV installation

Implementation —
I am using piimagesearch’s tutorial to implement basic motion detection. He have provided his code as Open Source, and is downloadable. When I run this code on my Pi it gave this error

Error while running the code

When I searched for imshow function, I understood that the code needs X-server to run. Because I am doing ssh to my Pi, the code was not running. Running this command gave me access to X-server during ssh.

ssh -Y pi@<IP>

Then I ran the python code again. And I got these three windows —

Running piimagesearch’s Python code

According to the tutorial, The code needs the first frame to be empty to detect if the area is occupied or unoccupied.

The 3 windows are —
Thresh is basically created using Image Thresholding which is used to make an image easier to analyze.
Frame Delta, which is a grey-scale image.
Security Feed, is the final video feed where it shows if someone is there in the frame, whether the area is occupied/unoccupied.

This code is very basic, as you can see even if only I am present in the frame, the code is sensing other movements as well like one block is for the ceiling fan, and there are multiple blocks. So more logic needs to be build upon it.

This implementation also helps me understand if I can even implement OpenCV in a low spec system like a Raspberry Pi. Though the response was slow as you can see from the GIF at the intro.

But this is a basic demonstration of motion detection, and I will cover more use cases in later posts.

Stay Tuned!



Akshul Goyal

Hacking the Physical World | Senior Embedded Systems Engineer @ PiRhoAlpha Research (ActiveBuildings) | I write posts about AVR and Raspberry Pi.