2012-10-24

OpenCV Background Subtraction in Python

Recently, I tried  finding an example of Background Subtraction being done in OpenCV and Python without success. The code is not complicated or special in any way. However, I guess, the example might be helpful, if you want to get started with Background Subtraction quickly.

The script reads a video file and writes out a mask for every frame.
#!/usr/bin/env python
#-*- encoding: utf-8 -*-

import cv2
import numpy

bgs = cv2.BackgroundSubtractorMOG(24*60, 1, 0.9, 0.01)

capture = cv2.VideoCapture("00185.MTS.mp4")
cv2.namedWindow("input")

a = 0
while(True):
    
    f, img = capture.read()
    fgmask = bgs.apply(img)
    
    #cv2.imshow("input", fgmask)
    #cv2.waitKey(1)
    cv2.imwrite("./pngs/image-"+str(a).zfill(5)+".png", fgmask)

    a = a + 1
    print(a)

Unfortunately there seem to be no Python bindings for BackgroundSubtractorMOG2 at the time of this writing.

2012-10-06

Buggy projectM Music Visualizations

Today I tried visualizing some music using projectM and qmmp. Using a qmmp plugin provided in the Ubuntu repositories, music visualization was especially easy to set up and use.

Unfortunately I found that some of the visualizations are quite buggy and can easily end your music listening experience with a nasty crash of the music player.
The faulty visualizations are part of the Ubuntu package projectm-data (version 2.0.1+dfsg-12).

Here is a script that removes all visualizations that are faulty in my opinion. I do not claim this list to be exhaustive, but in my case it ended disturbing crashes.