Monday, May 31, 2010

OpenCV and Matlab (and Visual Studio C++)

Matlab and OpenCV both have advantages for image processing work. I wanted to implement some code written in OpenCV (in MSVSC++) but run from a Matlab command window. After digging around on the web for help, I managed to get this to work. What makes this challenging is the OpenCV library linking. Simple C/C++/Fortran code can be implemented as Matlab functions by creating MEX files. To gain access to the OpenCV library, a MEX file can be written that has another file linked in at compile time. This second file can use the OpenCV library as usual. Without getting into the details, here are the steps I took. Use your head and change the directory paths and what not to match your setup.

Setting up Matlab
  • From a Matlab command window, run the command mex -setup. Follow the dialog and change the compiler to use the MSVSC++ compiler.
  • Find mexopts.bat (see below for locating this file) and open for editing. 
  • Add set OPENCVDIR=C:\Program Files\OpenCV2.1
  • Add %OPENCVDIR%\include\opencv to the set INCLUDE statement. 
  • Add OPENCVDIR%\lib;%OPENCVDIR%\bin to the set LIB statement. 
  • Add cv210.lib highgui210.lib cvaux210.lib cxcore210.lib to the end of the set LINKFLAGS statement. 
The location for mexopts.bat on my system is C:\Documents and Settings\Default\Application Data\MathWorks\MATLAB\R2008b. You can find where Matlab is looking for this file by trying to compile a MEX file with the verbose option enabled (e.g. mex -v program.cpp).

Setting up Visual Studio
Edit project properties as follows to allow compilation of MEX files.
  • Add the following folders to the C/C++ additional include directories:
    • [MATLAB_DIR]\extern\include
    • [MATLAB_DIR]\simulink\include
  • Add the following folder to the Linker additional directories:
    • [MATLAB_DIR]\extern\lib\win32\microsoft
  • Add the following Linker input additional dependencies (one per line):
    • libmx.lib
    • libmex.lib
    • libmat.lib
  • Add #include "mex.h" to your source code to allow the use and compilation of mx* functions.
Note: For the instructions above, replace [MATLAB_DIR] with the actual path on your system.

Creating Files

  • OpenCV File
    • Assuming you already have a working project that uses OpenCV, modify the source file to contain only a set of functions you would like to call from a MEX file.
  • MEX File
    • The MEX file needs to be written. See MathWorks' online documentation for how to do this. I used an example from here
    • Include extern references to functions that you want to use from your OpenCV file.
    • Use the functions in your MEX file.
Compiling

  • Compile the source that uses OpenCV to create an object file (e.g. opencv_file.obj)
  • Run the following command in a Matlab command window (with correct paths to your files):
    • mex mex_file.cpp opencv_file.obj
  • Fix any errors to get this to compile.
You will now have a file similar to mex_file.mexw32. This is the MEX file that acts as a normal Matlab function does (e.g. you can call mex_file()).

Tuesday, May 18, 2010

Point Grey Bumblebee2

Received a Point Grey Research Bumblebee2 stereo camera this week. Package comes with a proprietary SDK to be used in a Windows environment. I need to have the camera running in Ubuntu, so for now, I won't be using their SDK. Since the camera uses the IEEE 1394 (FireWire) standard, I've installed libdc1394 so I can attempt to communicate with the camera. To install in Ubuntu, extract the downloaded package and from a command window:
  1. ./configure
  2. make
  3. sudo make install
Also need firewire capability (such as from a PCI card like the one supplied in my camera kit) for the computer. To display a list of connected PCI cards type "lspci" in a command window, and make sure the interface is initialized at boot time by looking for firewire in the kernel log (e.g. "grep 1394 /var/log/kern.log").

Now to figure out how to obtain some images from the camera...

Thursday, April 29, 2010

Robot Operating System from Willow Garage

A project I'm working on (more details later) will likely be using Robot Operating System (ROS) by Willow Garage. This is essentially a (primarily) Unix-based operating system that has been created to provide a common, open source environment for the development of robotic applications. Willow Garage is closely tied to OpenCV development, and ROS should make it simple for integrating the most bleeding edge OpenCV functions along with digital camera drivers necessary for my project. I have set up an Ubuntu virtual machine using Sun's VirtualBox to familiarize myself with ROS. The documentation available at these websites is superb, so I will not go into more detail about setting this up. In the near future I will set up a dedicated PC. For now, I am going through the many tutorials available on the ROS website.

[UPDATE]
I have a dedicated PC set up running Ubuntu. To install OpenCV (after downloading appropriate tarballs file):

   1. tar -xjf OpenCV-2.1.0.tar.bz2
   2. mkdir opencv.build
   3. cd opencv.build
   4. cmake [] ../OpenCV-2.1.0 # CMake-way
   5. make -j 2
   6. sudo make install
   7. sudo ldconfig # linux only


Short-term goal is to create a basic image publisher/subscriber in ROS using OpenCV IplImages.

Tuesday, April 6, 2010

OpenCV and Microsoft Visual Studio 2008

I'll be playing with some image processing. OpenCV is a great option and is open source. For starters I'll be playing with OpenCV in a Visual Studio environment. Instructions for getting everything set up can be found here.


In addition to the initial setup, new Win32 Console projects should have the following additional dependencies added to the Linker > Input configuration properties:


  • cv210.lib
  • cxcore210.lib
  • highgui210.lib
Other tips for running the OpenCV samples that come with the installation as Win32 Console projects:
  • #include "stdafx.h" must be included 
  • Files referenced in the code must be in the Projects/ProjectName/ProjectName directory (and hardcoded file paths will likely need to be modified to reflect their new location)