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.

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

Leave a Reply

Scroll Up