BackgroundSubtractorCNT with python

OpenCV is very popular among python developers and we deserve being able to use BackgroundSubtractorCNT with python just like any other OpenCV python feature. This post explains how to enable, build and use BackgroundSubtractorCNT with python.

BackgroundSubtractorCNT with python

(If you missed the previous post about BackgroundSubtractorCNT and how fast it is then see here.)

As I would normally use OpenCV from python, I wanted to be able to create a BackgroundSubtractorCNT python object in my python applications. I also wanted use it interchangeably with any other background subtraction implementation. So I started working on a python branch for the project. After looking into several options and looking at what OpenCV is doing I started focusing on swig for wrapping the C++ code. On one hand BackgroundSubtractorCNT is not a part of OpenCV and cannot be part of it’s build system and code generation solutions. On the other hand swig can supply wrappers for use in many other programming languages, so it’s worth the time of learning to use it.

It turns out that swig is very friendly once you get to know it. After I started playing with it the decision became final.

The only main challenges left were these:

Having to convert numpy arrays from/to cv::Mat.

Challenge: I need to do it the same way OpenCV does it for interoperability.

Incorporate the “python module building” into a cmake build.

Challenge: I need it to work both on Linux and Windows – build & install/package.

Solving these challenges sent me seeking inspiration on the internet. I’m not going to delve into tedious details of how I solved it. Instead I’ll share the links to how other people solved similar problems at the end of this post.

Final notes

As promised here are links for further reading about facing python module building with OpenCV and cmake. I did things somewhat differently but based it on ideas from these:

Bloerg: Cmake and distutils – using setup.py from within cmake. I’m using that idea but changed some things for windows path compatibility and altered the install option for packaging support. If this is something which interest you, see the solution I came up with here.

pyboostcvconverter – manually enable conversion between cv::Mat and numpy arrays for “Boost.Python”. I’m using the same idea of “utilize by copy-paste” the original OpenCV conversion code, but I made the conversion transparent for my swig usage (like OpenCV does it). If this is something which interest you, see the solution I came up with here.

This has been a fun project and a good learning experience. I hope you’ll also find it useful.

16 Responses

Hi! I’m trying to install BackgroundSubstractorCNT for python in RPi3 to give it a try, but I’m not able to install. I’ve followed the instructions in github and youtube channel and I am stuck in the cmake step, I guess because I have my OpenCV install in a virtual environment and some path may not match. This is my error log:
OpenCV_LIB_DIR_NATIVE is:
OpenCV_INCLUDE_DIRS_NATIVE is: ‘/usr/local/include’,’/usr/local/include/opencv’
CMAKE_CURRENT_SOURCE_DIR_NATIVE is: /home/pi/BackgroundSubtractorCNT/python
BGSUBCNT_INCLUDE_DIR_NATIVE is: /home/pi/BackgroundSubtractorCNT
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘module’ object has no attribute ‘getsitepackages’
PYTHON_PREFIX is /home/pi/.virtualenvs/cv
PYTHON_SITE_DIR is
CMake Error at python/CMakeLists.txt:115 (INSTALL):
install DIRECTORY given no DESTINATION!

I’m using the ‘site’ module to establish the installation directory and there is a known issue for virtualenv and python 2.7 here – https://github.com/pypa/virtualenv/issues/355 .
However, I’ll look into this today and try to take this into account.

Hi,
It is using the default ‘python’ in your path:
1. I just opened and fixed 2 related issues thanks to your comment
– fixed a build error for python3
– fixed missing documentation about using a different python version (see also below).
2. Please take again the source (the latest sources from the master branch) there is no specific release for these fixes yet.
3. You need to use this from the build directory:
cmake -DPYTHON_EXTENSION=ON -DPYTHON=/usr/bin/python3.5 ..