Crea sito
Giandomenico's page
home

OpenCV for Freepascal in Windows and Linux

NEW! Header OpenCV.pas and IPL.pas updated to use with Freepascal on Linux!

This is a version of example program lkdemo (slighty modified from one of the examples coming with OpenCV) adapted to Freepascal. Now the OpenCV and IPL headers have compiler defines to adapt to Linux other then Windows.
Because lkdemo has a GUI output, I have chosen to use Freepascal with MSEGUI library (MSEGUI home), because it's very light, has a very good IDE, and it's straightforward to port to every Linux distro not requiring special GUI libraries (as Qt, GTK, ...). Maybe Lazarus is equally good, but I have not tested (although in the OpenCV header there are defines also for Lazarus in the BMP to OpenCV images conversion functions).
The program was tested on Xubuntu 10.10 with OpenCV 2.1 downloaded directly from the Ubuntu repository. I found some access violation problems with cvFindCornerSubPix function, but Freepascal shines managing that without crashing the program, wherever the original C++ program, recompiled on the same Xubuntu machine, refuses to run.
The ZIP archive contains a project for the MSEIDE tool. Unpack it in a directory, and open and compile in Windows or Linux without any modifications (having previously installed Freepascal and MSEGUI+MSEIDE, of course! :-).




C style interface for GPU OpenCV functions

All the original GPU OpenCV 2.3.1 library (opencv_gpu231.dll) is wrote in C++, so it's not possible to use it from many languages that cannot interface to C++, for example Delphi. Then I wrote Opencv_cvGpu. It's a dynamic library (DLL) that aims to use a C style interface with the new GPU versions of standard OpenCV functions. For now the library exposes these GPU functions:

  • cvCvtColor
  • cvDilate
  • cvMorphologyEx
  • cvResize
  • cvThreshold
  • cvRemap
They are among the more used and the ones with best improvement in performance, also on low end Nvidia graphic cards (I have just a simple GT-220), as from the results of performance_gpu.cpp (the performance comparison program for GPU functions included in OpenCV samples). In addition there are utility functions, especially to efficiently manage transfer of images from CPU memory to GPU memory:
  • cvGPUCreateImage: associates a GPU image with a standard OpenCV image (IPLImage), and upload to it
  • cvGPUReleaseImage: destroy the GPU image
  • cvGPUReadImage: download the image from GPU to OpenCV memory
  • cvGPUDownladResult: activate or deactivate the automatic download of GPU result to OpenCV image. This is a relative slow operation, sometimes not useful if the image has to pass in a sequence of functions all on GPU. In this case only the final result from the last function in the sequence needs to download to OpenCV struct.
Here there are the C++ sources, a Visual C++ 2008 Express project, and binaries.




Programs for stereo vision

Here there are improved versions of C++ programs related to OpenCV (version 2.3.1) for stereo imaging. The original sources are from the samples of OpenCv distribution. The first program is a modification of stereo_calib.cpp that allows to capture the images straight from two video sources attached to computer, for example two webcams. The second program is an improved version of stereo_match.cpp, that generates disparity map (and depth map if requested) again from two video sources, instead that from a couple of images as in the original example. The program shows a window with cursors for main parameters of SGBM algorithm, so you can change that parameters on the fly and just see the results. The links are for C++ sources, a Visual C++ 2008 Express projects, and binaries.




 

Stereo_match from two webcams
Stereo_match from two webcams

CvBlob library as a dynamic library (DLL)

NEW! Now there is a CvBlobDLL version for OpenCV 2.31

CvBlobDLL is a dynamic library for Windows (DLL) that exposes in a C style the main functions from the very nice CvBlob static C++ library.

CvBlob is a great work by Cristobal Carnero Linan (web site: http://cvblob.googlecode.com). CvBlob finds connected components (blobs) in a binary image, using functions from OpenCV, and a very fast internal algorithm, and has many valuable features. In my opinion the best of them are the contour of the blob as sequences, and the precalculated moments.

But CvBlob is a C++ static library, so it's impossible to dynamically call it from other languages (C, Delphi, etc.). So I wrote CvBlobDLL as a DLL wrapper for CvBlob. It exposes two functions (in cvblobDLL.h): cvBlobDLabel that is the main function to search the blobs, with some options to filter the results, and to return special properties of blobs; and cvBlobDRelease that simply releases the memory block allocated by the library. The blobs themselves are returned in a standard OpenCV list (CvSeq) as special type structs (CvBlobObj). Then all standard OpenCv functions can apply to the blobs list.

The downloadable ZIP archive contains: the complete DLL project, in Visual C++ 2008 Express format, with a test program; and the binaries in 32 bit version..

Compiling CvBlobDLL requires the sources of OpenCV, and the .LIB or sources of CvBlob.

The project contains three configurations: debug using OpenCV 1.0; release using OpenCV 1.0 again; and releaseOCV2 using OpenCV 2.31. All binaries were compiled as 32 bit executables, and tested on a Windows 7 64 bit system.



Here also a Delphi example (Delphi 2006 project)   

 
CvBlob DLL- original image CvBlob DLL- resulting image


OpenCV Flow

NEW! Header OpenCV.pas updated to use OpenCV 2.3.1

OpenCV Flow is an utility program. It permits to investigate the effects of a sequential flow of OpenCV functions, executed on every image contained in a directory. It's written in Delphi.
The program window has some buttons on top; on top left the original image (resized); on bottom left the transformed image; on top right a summary of some calculated parameters of an area of image choosen by drawing a rectangle on the image; on bottom right the list of functions executed on the image. Clicking the "Options" button, open a window where to choose some files (camera calibration distortion and intrisics matrixes; OpenCV help file; template image for template match functions), and the directory containing the images (PNG, JPG, BMP files) to be processed. Clicking on "Start" starts processing the first image in the directory. The image is passed through the sequence of functions appearing in the right workflow, and the final result is displayed in the bottom left frame. It's possible to change the functions, and reprocess the same image clicking on "Process". The resulting image can be saved in the program directory, clicking on "Save". The "Stop" button interrupts the processing of the directory. In the "Functions sequence" frame there are buttons to load and save a sequence (as a CSV file). The "OpenCV help" opens the help file, either a pdf or a html file. In the functions sequence grid click on a blank function field to add a function, choosing from the list. If no param is typed, the program shows and uses default parameters, where applicable. To delete a row, click on the delete field of the row.
The sources require the FastMM4 fast memory manage library. If you don't want to use FastMM4, delete or comment the corresponding row in the Uses clause in the OpCVFlow.dpr (project).


 
OpenCV Flow

 
OpenCV Flow options


Frame recon demo

FrameRecon demo is a Delphi 6 program that track in realtime a black square frame in a webcam image.
The program finds the frame using contours, either based on Canny or threshold function. In the tree of various contours it searches four corner ones, choosing only that have another inner four corner contour. For them, a simple aligning rule is calculated to find the searched square frame. Then it calculates the perspective transformation (translation and rotation), draw the resulting computed frame in green, and undistort the image as the frame was just in front of objective without translation or rotation. The sources require the FastMM4 fast memory manage library.


 
Example of frame recognition


Shape recon demo

ShapeRecon demo is a Delphi 6 program that try to recognize an assigned color shape in an image.
The image areas, having the searched colors, (color blobs) are selected by a special library that is an extension to OpenCV: Camellia by Bruno STEUX (Mines Paris / ParisTech): http://camellia.sourceforge.net/. The original is a static C/C++ library, that I have partially recompiled as a Windows DLL, exporting only the blobs search functions. In the demo the color searched is a pink/red one.
For every color blobs extracted from Camellia functions the moments are calculated with functions from OpenCV, and the results compared to the same moments calculated on a reference black and white image, the base pattern. If the difference in results is inside some range, the blob is recognized as equal to the base pattern, and the same blob is superimposed in original image with a green area.


 
Pattern recognized rotated of 180 Pattern not recognized
LEFT: Pattern recognized rotated of 180; RIGHT: Pattern not recognized
Pattern recognized Pattern recognized on floor
LEFT: Pattern recognized; RIGHT: Pattern recognized on floor

Note from the images that the algorithm is capable to recognize the pattern rotated of 180 degrees, and in a vertical or oblique position. Indeed the success of recognize depends very much from lighting conditions local to the target shape.


A DLL library for SIFT algorithm

This ZIP archive contains a SIFT DLL library that uses routines from OpenCV to implement the algorithm. I arranged in a DLL the original code by Rob Hess: http://web.engr.oregonstate.edu/~hess/ He provides a series of static C libraries, and 3 example programs. I think that it's very useful to have a DLL version of his code as a good complement of OpenCV. The DLL can be used from C or other languages, as Delphi, Visual Basic 6, .Net, ecc. In this archive there is a directory BIN containing the precompiled DLL and example programs. The directories LIB and INCLUDE contains the .lib for the DLL and the C header siftDLL.h. The directory SOURCES contains the sources, arranged as projects for Visual C++ 6.0 . Remember that (as from Rob Hess): "The SIFT algorithm is Patented in the U.S. by the University of British Columbia. Thus, the SIFT feature detection code in this package may not be used in any commercial products without permission from UBC. All other code in this package is Licensed under the GPLv2. See the files LICENSE.ubc.txt and LICENSE.txt for more info."



OpenCV examples translated from C to Delphi

Camshift demo

The first example is the Delphi 6 version of the Camshiftdemo example program for the Open Computer Vision (OpenCV) library, originally written in C. Camshift is an algorithm capable of tracking a moving coloured blob in a videocamera field. The structures and functions header are translated from C to Delphi only in the parts strictly needed for Camshiftdemo. I have tested the program with a budget webcam (Genius). Also I have tested a wireless color camera, with the receiver connected to a tv card with capture function (Hauppage WinTv PCI). The program runs well with any of these two cameras.
The program accept these commands:

  • Mouse selection: hold the left button and select a clearly colored area in the image. The program identifies the area with a red ellipse, then will try to move the ellipse according to the movement of the area
  • C key: cancel the selection
  • H key: show a color histogram window
  • B key: toggle a filtered black and white image with the normal image from camera


 
Image of Camshiftdemo   Image of Camshiftdemo 2


LK demo

The second example is the Delphi 6 version of the Lkdemo example program for the Open Computer Vision (OpenCV) library. This program is a demo of a corner tracking algorithm, it demonstrates how to mark the initial corners in the image and how to track them during movement.
The program accept these commands:

  • Mouse click over a point: mark a corner in the image, if it exists in that point
  • R key: mark all the relevant corners in image
  • C key: cancel all corners


 
Image of Lkdemo


 
Mail contact