11.5. Other Interfaces

There are other ways to "talk" to OpenCV beyond compiled C. We detail three main methods below: Ch, MATLAB and Lush.

11.5.1. Ch

What is Ch?

Ch is a superset of C interpreter. It is designed for cross-platform scripting, 2D/3D plotting, and numerical computing. Ch was originally designed and implemented by Harry H. Cheng [16]. It has been further developed and maintained by SoftIntegration, Inc. [15, 25]

Ch supports 1999 ISO C Standard (C99), classes in C++, POSIX, X11/Motif, OpenGL, ODBC, XML, GTK+, Win32, CGI, 2D/3D graphical plotting, socket/Winsock, C LAPACK, high-level numeric functions, and shell programming.

The extensions in Ch provide the simplest possible solution for numerical computing and visualization in the C/C++ domain. Ch can interface with C/C++ binary libraries and be embedded in other application programs and hardware.

What is Ch OpenCV package?

Ch OpenCV package is Ch binding to OpenCV. It is included in the distribution of OpenCV. With Ch OpenCV, C (or C++) programs using OpenCV, C functions can readily run in Ch without compilation. Ch Standard or Professional or Student Edition and the OpenCV runtime library are needed to run Ch OpenCV.

Contents of Ch OpenCV package

Ch OpenCV package contains the following directories

  1. OpenCV:: Ch OpenCV package

    - OpenCV/demos: OpenCV demo programs in C readily to run in Ch

    - OpenCV/bin: OpenCV dynamic library and commands

    - OpenCV/dl: dynamically loaded library

    - OpenCV/include: header files

    - OpenCV/lib: function files

  2. Devel: Files and utilities used to build Ch OpenCV package using native C compiler and Ch SDK

    - Devel/c: _chdl.c C wrappers and Makefile's

    - Devel/createchf: bare OpenCV headers

    - Devel/include: Ch OpenCV-specific include files

Where to get Ch and set up Ch OpenCV

Ch OpenCV can be installed in your computer following these instructions. Here we assume <CHHOME> is the directory where Ch is installed; but if, for example, Ch is installed in C:/Ch, then C:/Ch should be used instead of <CHHOME>. We also assume <OPENCV_HOME> is the directory where OpenCV is installed. If OpenCV is installed, for example, in C:/Program Files/OpenCV, then <OPENCV_HOME> should also be replaced by C:/Program Files/OpenCV.

1.
If Ch has not been installed in your computer, download and install Ch from http://www.softintegration.com.

2.
Move the directory OpenCV to <CHHOME>/package/opencv. <CHHOME>/package/opencv becomes the home directory of CHOPENCV_HOME for Ch OpenCV package. If you do not want to move the directory, you should add a new path to the system variable _ppath for package path by adding the following statement in the system startup file <CHHOME> /config/chrc or individual user's startup file _chrc in the user's home directory _ppath = stradd(_ppath, "<OPENCV_HOME> /interfaces/ch;"). <OPENCV_HOME> /interfaces/ch/opencv becomes the home directory CHOPENCV_HOME for Ch OpenCV package.

3.
Add the system variable _ipath in the system startup file indicated in the previous step by adding the following statement: _ipath = stradd(_ipath, "<CHOPENC V_HOME> /include;"). This step is not necessary if the following code fragment is in application programs:

#ifdef _CH_
  #pragma package <opencv>
#endif

Run demo programs in directory demos:
1.
Start Ch.

2.
Type program name such as delaunay.c.

Update Ch OpenCV package for newer versions of OpenCV

The update of Ch OpenCV package for newer versions of OpenCV can be done as follows:

1. Install Ch SDK from http://www.softintegeration.com.

2. Go to ./Devel subfolder and run update_ch.ch that does the following:
   + Copies headers from <OPENCV_HOME>/cv/include and <OPENCV_HOME>/otherlibs/highgui to 
Devel/include subfolder
   + Generates Devel/createchf/*.h files containing bare function lists
   + Processes each function list with c2chf
   + Moves *.chf files generated by c2chf to ./OpenCV/lib
     subfolder and *_chdl.c files to ./Devel/c/<libname>
   + Removes automatically generated wrappers that have
     manually created counterparts in *_chdl.c
   + Builds .dl files using Ch SDK and places them to
     ./OpenCV/dl
   + Copies OpenCV DLLs to bin subfolders
   + Copies C samples from <OPENCV_HOME>/samples/c to
     ./OpenCV/demos

3. Copy the resultant folder ./OpenCV, which contains Ch OpenCV package, to <CHHOME>
/package/OpenCV.

Programming example

Below is an example called morphology.c. In this example, functions cvErode() and cvDilate() are called. The prototypes of functions are as follows:

void cvErode(const CvArr* sourceImg, CvArr* DestImg,
             IplConvKernel*, int iterations);
void cvDilate(const CvArr* sourceImg, CvArr* DestImg,
              IplConvKernel* B, int iterations);

These two functions perform erosion and dilation respectively on a source image. Parameter B is a structuring element that determines the shape of a pixel neighborhood over which the minimum is taken. If it is NULL, a 33 rectangular structuring element is used. Parameter iterations is the number of times erosion is applied. Four callback functions, Opening(), Closing(), Erosion(), and Dilation(), were declared in this example. The function Opening() carries out erosion first, then dilation. The Closing() function does the opposite. Functions Erosion() and Dilation() perform only erosion and dilation respectively. The source code of program morphology.c is as follows:

#ifdef _CH_
#pragma package <opencv>
#endif

#include <cv.h>
#include <highgui.h>
#include <stdlib.h>

#include <stdio.h>

//declare image object
IplImage* src = 0; IplImage* image = 0; IplImage* dest = 0;

//the address of variable which receives trackbar position update int pos = 0;

//callback function for slider, implements opening void Opening(int id) {
    id;
    cvErode(src,image,NULL,pos);
    cvDilate(image,dest,NULL,pos);
    cvShowImage("Opening&Closing window",dest);
}
//callback function for slider, implements closing
void Closing(int id) {
    id;
    cvDilate(src,image,NULL,pos);
    cvErode(image,dest,NULL,pos);
    cvShowImage("Opening&Closing window",dest);
}

//callback function for slider, implements erosion
void Erosion(int id) {
    id;
    cvErode(src,dest,NULL,pos);
    cvShowImage("Erosion&Dilation window",dest);
}

//callback function for slider, implements dilation
void Dilation(int id) {
    id;
    cvDilate(src,dest,NULL,pos);
    cvShowImage("Erosion&Dilation window",dest);
}

int main( int argc, char** argv) {
    char* filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
    if( (src = cvLoadImage(filename,1)) == 0 )
        return -1;

    //makes a full copy of the image including header, ROI and data
    image = cvCloneImage(src);
    dest = cvCloneImage(src);

    //create windows for output images
    cvNamedWindow("Opening&Closing window",1);
    cvNamedWindow("Erosion&Dilation window",1);

    cvShowImage("Opening&Closing window",src);
    cvShowImage("Erosion&Dilation window",src);

    cvCreateTrackbar("Open","Opening&Closing window",&pos,10,Opening);
    cvCreateTrackbar("Close","Opening&Closing window",&pos,10,Closing);
    cvCreateTrackbar("Dilate","Erosion&Dilation window",&pos,10,Dilation);
    cvCreateTrackbar("Erode","Erosion&Dilation window",&pos,10,Erosion);
    cvWaitKey(0);
    //releases header and image data
    cvReleaseImage(&src) ;
    cvReleaseImage(&image);
    cvReleaselmage(&dest);
    //destroys windows
    cvDestroyWindow("Opening&Closing window");
    cvDestroyWindow("Erosion&Dilation window");

    return 0;
}

Results are shown in Figure 11.32. On the left in Figure 11.32 is the original image; at right is the image after dilation. The dilation iterations was set to 8.

Figure 11.32. Interface CH: Raw image and after eight dilations.


11.5.2. MATLAB

After installing OpenCV, you'll find the MATLAB interface at C:/Program Files/OpenCV/interfaces/mat lab. These are source and executables for MAT-LAB wrappers for OpenCV.

To install wrappers, set path in the MATLAB to the toolbox/opencv subfolder of this folder and, optionally, to the toolbox/opencv/demos subfolder. Wrappers for OpenCV functions can be run in two ways: using cvwrap('<FunctionName>', parameters), where FunctionName is OpenCV function without cv prefix, or just by using cvFunctionName(parameters).

To build MATLAB wrappers, you need to add the <matlab_folder>/ extern/include folder to the include path (within Developer Studio) and <matlab_folder>/extern/lib /win32; <matlab_folder>/extern/lib/win32/ microsoft/msvc60 folders to the lib path.

Contents of MATLAB OpenCV Package

As of this writing, the interface is not complete, and we welcome more mex file contributions. The interface has the following:

cvadaptivethreshold.m
cvapproxpoly.m
cvcalcopticalflowpyrlk.m
cvcanny.m
cvcontourarea.m
cvcontourboundingrect.m
cvcontourperimeter.m
cvdilate.m
cvdisttransform.m
cvdrawcontours.m
cverode.m
cvfindcontours.m
cvfloodfill.m
cvgoodfeaturestotrack.m
cvlaplace.m
cvmatchtemplate.m
cvminmaxloc.m
cvprecornerdetect.m
cvpyrdown.m
cvpyrup.m
cvsobel.m
cvthreshold.m
cvupdatemotionhistory.m
cvwrap.dll -- the mex wrapper

The demo directory contains

'Canny Demo',        'cannydemo'
'Contour Demo',      'contdemo'
'Flood Fill Demo',   'filldemo'
'Optical Flow Demo', 'flowdemo'

Programming example

MATLAB is perhaps the tersest programming language. The code below runs the Canny edge detector with (low, high) thresholds set to (120, 200) with resulting input/output shown in Figure 11.33.

I = imread('Clone.jpg'),               %Read in an image
C = cvcanny(I(: , : ,2) , 120,200,3);  %Apply canny with thresholds (Low,High)
                                       %(120,200), sobel size 3
imshow(C);                             %Show the image
imwrite(C, 'CloneCanny.bmp' , 'bmp' ); %Save image

Figure 11.33. Interface MATLAB: Left image is the input, right image is after running Canny on it with low/high thresholds 120/200.


11.5.3. Lush

For Linux and UNIX systems, Yarn LeCun and Leon Bottou developed Lush [29], an object-oriented programming language with a simplified Lisp-like syntax where C may be mixed freely with simple Lisp. Lush is intended for researchers, experimenters, and engineers interested in large-scale numerical and graphic applications.

Lush is free software (under the GPL license) and runs on GNU/Linux, Solaris, and Irix. They claim a few days learning curve or your money back.

Main features are the following:

- A very clean, simple, and easy-to-learn Lisp-like syntax.

- A compiler that produces very efficient C code and relies on the C compiler to produce efficient native code (no inefficient bytecode or virtual machine).

- An easy way to interface C functions and libraries, and a powerful dynamic linker/loader for object files or libraries (.0, .A, and .SO files) written in other compiled languages.

- The ability to freely mix Lisp and C in a single function.

- A powerful set of vector/matrix/tensor operations.

- A huge library of over 10,000 numerical routines, including full interfaces to GSL, LAPACK, and BLAS.

- A library of image- and signal-processing routines.

- An extensive set of graphic routines, including an object-oriented GUI toolkit, an interface to OpenGL/GLU/GLUT, and a soon-to-be-available interface to the OpenInventor scene-rendering engine.

- An interface to the Simple Directmedia Layer (SDL) multimedia library, including a sprite class with pixel-accurate collision detection.

- Sound and video grabbing (using ALSA and Video4Linux).

- Several libraries for machine learning, neural net, statistical estimation, HMMs (gblearning, Torch, HTK).

- Libraries for computer vision (Intel Vision Library) and 3D scene rendering (OpenInventor, available soon).

- Bindings to the JavaVM API and to the Python C API (available soon).

Lush is geared toward research and development in signal processing, image processing, machine learning, computer vision, bio-informatics, data mining, statistics, simulation, optimization, and artificial intelligence.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.252.23