WxOpenCv is a simple
demo application to show you how to integrate wxWidgets with OpenCv library. The application
take the video stream from a camera and displays it a window. Also, it adds a green
rectangle to every frame and shows you how many frames per second you receive.
Aditionaly,

I did a small hack to provide access to the system/driver control
parameters of the camera.Note, that
this might not be the best optimization of the speed or resources as this is
not a scope of this project. The image
bellow represents a snapshot of the application running.

Get started

Download the source
zip file from here
and unzip the file. Inside you will have the source code, the msvc++ 6.0
project files and also the executable (release version) compiled already for
the widows. Then you can open the project in have a look at the code.

Project files explained

There are six cpp files
aith as many header in the project and their functionality is briefly described
bellow.

wxopencv.cpp  is
main application file where the camera, gui and worker thread are
initialized

worker.cpp 
contains a class derived from wxThread which is meant to handle the
frame flow with the eventual extra processing

gui/ frame.cpp
 is responsible for the GUI the frame, menus and the
camera view

gui/camview.cpp
 contains the class used to display the frames from
the camera and is also where you can draw extra elements in the view

gui/about.cpp  is
the about dialog you get from menu File->About

camera/camera.cpp
 this has CCamera class which is used to read the
video stream frame by frame and convert it to iplImage format

How does it work

The way I implemented
this is fairly simple. I use a thread (worker) to pull frames from the camera
object and feeds this into the camera view. The main challenge I encounter initially
was how to convert from the IPL image format used by opencv to something wxwidtgets
can work with  which is bitmap. I will start first with some details on this conversion,
see bellow:

Where pDstImg is a
copy of the ipl image received from the camera +/- extra drawing, and m_pBitmap
is the bitmap used by the CcamView

class to draw onto the window.

- camera.cpp  class CCamera, method GetNextFrame,
is where I receive the first frame from the camera covert it to RGB and feed it
into the camera view. See the code sample bellow (actually simplified).

The GetNextFrame
method is called by the Run method of the camera object which also does
some checking on either the camera was paused or parameters or changed. The camera
Run method is called from within the worker thread main loop.

- worker.cpp  class CwxopencvWorker, method
Entry, is where I periodically call for a new frame the camera object +/-
a given delay by default set at 5ms.

The camera control panel hack

I found a way to access
the camera control panel using the a VFW dummy structure. It was a bit of a
pain since vfw is more like a black box. You will find this implemented in file
frame.cpp, class CGUIFrame, methods OnVideoFormat and OnVideoSource
as per the following snapshot:

Given that both
libraries are portable, there should be possible to be able to compile this
code, at least on Linux. You will find that in the camera.cpp file I differentiate
the code by platform using the WIN32_LARRY define. I did manage to get this
working on Linux as well, some three years ago or so, but ever since then I neglected
the portability issue, however I left the code in place. I remember at the time
that I had some problems to get the Logitech
webcam driver to work under Linux  which should be ok by now. If you do manage
to get this ported over and tested let me know.

Feedback

Well, once again
this is just one way to demonstrate how its possible to integrate these to
libraries. If you find any bugs or better ways to implement or optimize this
demo please let me know at: i@larryo.org.