Finally I read this book, visualizing data. Frankly, I expected new approaches, how to visualize data, so attractive. However, this book is inclined to the introduction to the Processing Language and its internal implementation and design. I have a toolkit to visualize data, matlibplot written with python. This matlibplot is beautilful because I can embed it within other GUI framework such as gtk, and lovely wxwidget and wxpython. The Processing Language is written at CMU for students, professionals, and artists. Looks promising but not align with me. That is because it is not extensible at all. The Processing Language is just backend and hard to extend it as real processing engine.

Introduction

The OpenCV is one of the greates and widely using image library for all developers and students. However, I am not a OpenCC and Graphics guy, but I choose OpenCV for Video Recording purpose. Since I mostly live and develop within Ubuntu, I tried writing and testing a little python-opencv program, but not working. It is mainly because the problem of new ffmpeg in ubuntu 9.04 Jaunty – see, https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/311188. While searching the Internet, I found an article to use video streaming of opencv(here, http://gijs.pythonic.nl/blog/2009/may/3/getting-video-io-working-opencv-and-ubuntu-jaunty-/). I tired because I don’t want to reinvent the wheel of course, but not working like many comments on ‘cvCreateVideoWriter‘ function.

Prepare to compile

Prepare to build

1
2
sudo apt-get build-dep python-opencv
sudo apt-get install libswscale-dev swig

Prepare to configure

Link avcodec.h, avformat.h, avio.h, avutil.h and swscale.h in the /usr/include/ffmpeg (you have to create) like below.

1
2
3
4
5
6
ls /usr/include/ffmpeg
avcodec.h -> ../libavcodec/avcodec.h
avformat.h -> ../libavformat/avformat.h
avio.h -> ../libavformat/avio.h
avutil.h -> ../libavutil/avutil.h
swscale.h -> ../libswscale/swscale.h

Checkout OpenCV

1
svn co https://opencvlibrary.svn.sourceforge.net/svnroot/opencvlibrary/tags/latest_tested_snapshot

Configure & Make

1
2
cd latest_tested_snapshot/opencv
./configure --enable-shared --enable-swscale --enable-gpl --with-swig
  • –enabled-shared : as shared library
  • –enable-swscale : alternative work around for ffmpeg
  • –enable-gpl : opensource library
  • –with-swig : regenerate swig interfaces
...
    Video I/O ---------------------
    Use QuickTime / Mac OS X: no
    Use xine:                 no
    Use gstreamer:            no
    Use ffmpeg:               yes
    Use dc1394 & raw1394:     no
    Use v4l:                  yes
    Use v4l2:                 yes
    Use unicap:               no

Wrappers for other languages ========================

    SWIG                      /usr/bin/swig -c++
    Python                    yes
    Octave                    no
...

The highlighted configuration items are important, ffmpeg / swig / python. If everything is find, do make. The interface files in the checkout directory are not properly generated, so you have to generate yourself.

1
2
make
sudo make install

Test and Correct OpenCV Python files

1
2
3
4
5
6
7
8
9
10
11
$ cd /usr/local/lib/python2.6/site-packages/opencv
$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for ...
>>> import cv.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cv.py", line 8473, in <module>
    CV_SEQ_CONTOUR=CV_SEQ_POLYGON
NameError: name 'CV_SEQ_POLYGON' is not defined

Open cv.py and Edit

1
CV_SEQ_CONTOUR=CV_SEQ_POLYGON

(line 8473) to

1
CV_SEQ_CONTOUR=CV_SEQ_POLYGON_TREE

Everything is done.

Example of OpenCV and PyGame

Displaying two Video Cameras as one concatenated video and record it to a file.

screenshot_0551

screenshot_056

To make my program independent, I just copied the opencv python binding to the local directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
import pygame
import Image
import opencv

from PIL import Image
from pygame.locals import *

from opencv import highgui
from opencv import cv

def get_image( camera1, camera2 ) :
    im1 = highgui.cvQueryFrame( camera1 )
    im2 = highgui.cvQueryFrame( camera2 )
   
    pil1 = opencv.adaptors.Ipl2PIL( im1 )
    pil2 = opencv.adaptors.Ipl2PIL( im2 )

    img = Image.new( "RGB", (pil1.size[0] + pil2.size[0], pil1.size[1]) )
   
    img.paste( pil1, (0,0,pil1.size[0],pil1.size[1]) )
    img.paste( pil2, (pil1.size[0],0,pil1.size[0]+pil2.size[0], pil2.size[1] ) )

    return img

def CV_FOURCC( c1, c2, c3, c4 ) :
    return (((ord(c1))&255)             \
                + (((ord(c2))&255)<<8)  \
                + (((ord(c3))&255)<<16) \
                + (((ord(c4))&255)<<24))

if __name__ == "__main__" :

    camera1 = highgui.cvCreateCameraCapture( 0 )
    camera2 = highgui.cvCreateCameraCapture( 1 )

    ( width, height ) = ( 640, 480 )
    fps = 30.0
    pygame.init()
    window = pygame.display.set_mode( ( width * 2, height ) )
    pygame.display.set_caption( "WebCam Demo" )
    screen = pygame.display.get_surface()

    writer = highgui.cvCreateVideoWriter( "captured.avi",
                                          CV_FOURCC('M','J','P','G'),
                                          fps,  
                                          cv.cvSize(width * 2, height) )

    if not writer :
        print "Error on writer"
        exit(1)

    on_exit = False

    while not on_exit :
        events = pygame.event.get()
        for event in events:
            if event.type == QUIT or event.type == KEYDOWN:
                on_exit = True

        im = get_image( camera1, camera2 )

        highgui.cvWriteFrame( writer, opencv.adaptors.PIL2Ipl(im) )
       
        pg_img = pygame.image.frombuffer( im.tostring(), im.size, im.mode )
       
        screen.blit( pg_img, (0,0) )
        pygame.display.flip()
        pygame.time.delay( int( 1000 * 1.0 / fps ) )
1
2
3
python webcam.py
Output #0, avi, to 'captured.avi':
Stream #0.0: Video: mjpeg, yuvj420p, 1280x480, q=2-31, 78643 kb/s, 90k tbn, 30 tbc

Reference

  1. https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/311188\
  2. http://ubuntuforums.org/showthread.php?p=7315433
  3. http://code.google.com/p/ctypes-opencv/source/checkout
  4. http://gijs.pythonic.nl/blog/2009/may/3/getting-video-io-working-opencv-and-ubuntu-jaunty-/
  5. http://www.pythonware.com/products/pil/

Disclamer

I copied all of ideas from the Internet and just do it myself for my purpose. However, if you want, feel free to use above example under GPL v.2. Thanks.

Not much special and fun tips and know-how anymore. I think most of readers of this book better to study recent tools not like Vi, external diffing tools and independent GDB. Studying Emacs, Bash, and Python should be the first step to jump from novice to professional.

I love this C++ in-depth series, but it seems that in-depth series will not be published anymore. It is great for introduction to metaprogramming language especially C++ programmers. Even it requires prerequisites of deep understanding of C++ language itself, of course template framework, if you finished reading this book, you would be enough to write C++ library with extensibility and optimization for various users. With this template framework of C++, C++ could be equipped with speed and extensibility. Not only that, for naive users not understanding computer deeply, C++ could be the best language ever in terms of speed. All library or framwork programmers must read this book for their clients.