BackgroundSubtractorCNT with python

the impossible code - python background subtraction with BackgroundSubtractorCNT

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.

UPDATE: This project now has it’s own site.

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.

Building and install BackgroundSubtractorCNT with python

It is best keeping such details on the GitHub project page.

There are also 2 video tutorials for building  BackgroundSubtractor with python for Windows & Pi on the video channel of the impossible code.


Using BackgroundSubtractorCNT with python

After installation, these are the 2 only things you would do differently:

  1. New import: import bgsubcnt
  2. Create your background subtractor with: bgsubcnt.createBackgroundSubtractor()

Here is an example usage:

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 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.

See you next time.

Sagi Zeevi

A software developer brain surgeon ... if software only had a brain. An electronics hobbyist heart surgeon ... if electronics only had a heart.

You may also like...

4 Responses

  1. markusand says:

    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_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
    CMake Error at python/CMakeLists.txt:115 (INSTALL):
    install DIRECTORY given no DESTINATION!

    Could you please tell me what parameter do I have to change. Thanks.

Leave a Reply

Scroll Up