Wednesday, January 26, 2011

Trials of pycam example code

I am trying to learn something about computer version, and Python is my first choice of the coding tool. After several hours of searching, I found pycam. There are several sample codes given by the pycam. What I have downloaded was the ``pycam-read-only'' folder in which are three subdirectories:

  • c-vs-py
  • openeyes
  • pycam

To install the pycam, just find the ``'' in the subdirectory named ``pycam'' and run ``sudo python install.'' Furthermore, what you need to install might include the follows:

  • python-opencv
  • python-pygame
  • libcv-dev
  • libcvaux-dev
  • libhighgui-dev

The lib*-dev are (my guess) for the cpp codes, which need to include the cv.h and highgui.h.

So far, my testing results are:

  1. pycam-read-only/pycam/examples/: Okay, but the fps are low
  2. pycam-read-only/pycam/examples/opencv: Failed. There were errors I don't know how to resolve yet. Only the ``'' can be executed, after remove the ``cv.CV_VERSION'' at line 88. (the CV_VERSION seems not in my OpenCV.)
  3. pycam-read-only/pycam/pycam/: Failed
  4. pycam-read-only/c-vs-py/c_examples: Okay
  5. pycam-read-only/c-vs-py/py_examples: Some codes showed errors. They are,, and
EDIT: New results are given in the new post:
Trials of pycam example code (2)

    Tuesday, January 25, 2011

    Face detection-- code patch and tests with some drawings

    My colleague introduced me an OpenCV site in Japanese in which many sample codes are available. Most of the sample codes are written in C++, and some are converted to Python also. I copied one sample code, which is used for face detection.

    After testing, I found the sample code has two lines which have to be modified to run properly. The first one seems to be a typo. The variable named as ``storage1'' should be ``storage'' according to other lines of code. The 25th line with variable ``starage1'' should be deleted. Another modification is to change assign the path of Haar-cascade xml files [*] explicitly. The original code gives the xml file without the complete path, and the program cannot find the so-called ``haarcascade_frontalface_default.xml.''

    The patch file:
    @@ -22,9 +22,8 @@
             src_gray = cvCreateImage(cvSize(src_img.width, src_img.height), IPL_DEPTH_8U, 1)
    -        storage1 = cvCreateMemStorage(0)
             ## (2)ブーストされた分類器のカスケードを読み込む
    -        cascade_name = "haarcascade_frontalface_default.xml"
    +        cascade_name = "/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml"
             cascade = cvLoadHaarClassifierCascade(cascade_name, cvSize(1,1))
             ## (3)メモリを確保し,読み込んだ画像のグレースケール化,ヒストグラムの均一化を行う

    Here I tested the sample code with some of my drawings, which can be viewed on my another blog. The results are interesting (at least for me).

    [*] I have just begun to learn things about computer vision, so I might use some terms wrong.

    Thursday, January 13, 2011

    [Py] Spline function test

    To interpolate data points, Scipy has several modules for this work. For what I am interested in, there are two functions seem to meet my requirement:

    According to the explanations given by the above links, to use UnivariateSpline with s=1 is equivalent to use InterpolatedUnivariateSpline.

    Here is the results of a simple test:


    from numpy import linspace,exp
    from numpy.random import randn
    from scipy.interpolate import UnivariateSpline, InterpolatedUnivariateSpline
    from pylab import plot, legend, show
    x = linspace(-3, 3, 100)
    y = exp(-x**2) + randn(100)/10
    s0 = UnivariateSpline(x, y, s=0)
    s1 = UnivariateSpline(x, y, s=1)
    s2 = InterpolatedUnivariateSpline(x, y)
    xs = linspace(-3, 3, 1000)
    ys0 = s0(xs)
    ys1 = s1(xs)
    ys2 = s2(xs)
    plot(xs,ys0,'g--',label='UnivariateSpline s=0',linewidth=4)
    plot(xs,ys1,'b',label='UnivariateSpline s=1')
    plot(x,y,'ro',label='original signal')

    Wednesday, January 05, 2011

    Producing sinusoidal signals

    Sometimes I need a signal of different frequencies for testing, so I wrote a simple Python code to produce compound sinusoidal signal by adding several cosine waves of different frequencies, amplitudes, and phases.

    The code is listed as the follows. (download)
    from scipy import *
    from pylab import *
    data_size = 10 # number of sinusoidal waves
    A = rand(data_size)
    theta = rand(data_size)
    f = [i for i in range(1,data_size+1)]
    t = linspace(0,10,1024)
    y = [0.0 for i in range(len(t))]
    for i in range(data_size):
            y += A[i]*cos(2*pi*f[i]*t + theta[i])

    Trials of EMD C code

    I found a simple module of EMD in C on google code: realtime-emd. The source code, however, has no main function to invoke the modules. So I wrote one as the follows.

    #include <stdio>
    #include "EmpiricalModeDecomposition.h"
    void imfs_output(emdData* emd){
            int i, j;
            for(j = 0; j < emd->size; j++){
                    for(i = 0; i < emd->order; i++) {
                            printf("%f ", emd->imfs[i][j]);
    int main(void){
            FILE *inputfile = fopen("sinusoid.txt","r");
            int i=0;
            float signal[1024]={0.0};
            emdData data;
            while (fscanf(inputfile, "%f", &signal[i]) != EOF){
                    //printf("%d %f\n",i,signal[i]);
            emdCreate(&data,i,5,10,0); // problems here
            return 0;

    The main problem is the arguments of the function emdCreate. I am not sure how to set proper parameters to obtain correct results, so please take this clip of code as just a reference.

    You can download the main.c and sinusoid.txt for testing. The input file sinusoid.txt was generate by adding several sinusoidal signals.