Tuesday, September 2, 2008

Computer Vision C++ vs Java review

In 2007 I created an open source computer vision project, ShapeLogic, built in Java to work with ImageJ. This setup has been very easy to work with and very productive. Bjarne Stroustrup the creator of C++ gave an interview about the new features in the C++0x standard and TR1. C++ now has a lot of innovating programming constructs e.g. template meta programming, lambda functions, concepts and traits. When I found out that "axiom" is going to be a keyword in C++ my inner mathematician demanded that I take a second look at C++ in connection with computer vision.

This post is a review of my personal past experience with computer vision in C++ and Java. I did my masters thesis in computer vision in the early 90ies, but I ended up working in other fields: video games, Internet and finance, which only left a little time to do vision in my free time. While both C++ and Java were good choices for professional vision programmers, several of the approaches I chose caused me to run out of steam. I also tried to do computer vision with functional, declarative and hybrid languages e.g. Oz, Scheme and Scala but will not cover that here.

Borland C++ early 90ies

C++ did not have STL or any other standard library so I used Borland's OWL library for images and for the application. I used C++ templates, classes with multiple inheritance, RTTI just to set up basic container functionality. There were a few books that has some free C or C++ source code for image processing and vision, but they did not spawn a user community. I did not really get to do anything interesting.

JAI, Java Advanced Imaging late 90ies

I was very excited when Java came around, this was the language to cure all programming ailments. Now they had added a library that could be used for vision and a lot of big companies were sponsoring JAI. It turned out to be a very complex framework with a deep class hierarchy, I spent a lot of time reading the manual trying to find out how to get access to image pixels. I gave up using it and the framework never gained much popularity.

VXL, C++, STL, Boost, Python, GCC, Linux around 2000

Open source software, OSS had started to become prominent. There were 2 OSS libraries:
  • OpenCV (Open Computer Vision), wich was still in alpha.
  • VXL (Vision X Library) wich was a merge of 2 big non OSS libs TargetJR and IUE.
VXL finally got into beta and I tried to combine it with Python for more high level processing.

Tools needed for build and GUI
  • VXL does builds using CMake to create Make files
  • Boost uses BJam to do builds
  • Python bindings using Pyste from Boost
  • VXL used FLTK and OpenGL as a GUI
Problems encountered
  • It was hard to get the different build systems, CMake, Bjam and Make, to work together
  • GCC 3.1 and 3.2 core dumped when compiling certain Boost classes
  • Python bindings worked for simple C++ classes, but not for the nested template classes in VXL
  • It was hard to debug the template programs
  • Emacs was not really as easy to use as Visual Studio
  • Bad drivers for OpenGL on Linux
I actually got some examples set up, but spent more time fighting with the tool stack than doing vision work.

ImageJ in Java around 2004

A colleague showed me a visualization tool he had worked on and said that he did it in around 1 month. I barely believed him, but tried the underlying framework, ImageJ. To my big surprise I was up and running and doing real work in a few hours. ImageJ just got things right. It was built using pure Java by one man, Wayne Rasband. It is very easy to work with and very modular, so a lot of people have made plugins and there is a vibrant development community. When I started working on ShapeLogic that was the best choice.

OpenCV, GIL Generic Image Library, Boost and Eclipse in C++ 2008

In the light of advance in the C++ language and tools, I have decided to try it again.
C++ image libraries choices
I chose to start with OpenCV made by Intel and GIL made by Adobe but a part of Boost since 1.35.

C++ IDE tried
  • Eclipse 3.4
  • NetBeans 6.1
Eclipse worked better for me, it has its own build system so you do not have to mess with Make files.

C++ cross platform GUIs Not sure which one will be best for my purpose.

First attempt
I tried Boost, OpenCV and GIL and got them up and running under both Linux and Windows in a few hours. Eclipse CDT C++ IDE works great.

Porting ShapeLogic algorithms to C++ version

My plan is to port some algorithms from ShapeLogic from Java to C++. ShapeLogic is a toolkit for declarative programming, specialized for vision. In principle you should be able to make a list of rules for categorizing say the shape of a particle in a particle analyzer. You put them in a database or a flat file and the same rules should work for C++ and Java version of ShapeLogic. In practice this might not work out.

Advantages of C++ and Java

This is a loose first assessment.
Constructs used in ShapeLogic that are missing or less convenient in C++
  • Uniform cross platform GUI
  • Dynamic cross platform libraries
  • HashTable
  • Reflection
  • Garbage collection
  • Antlr for parsing logic language
Advantages of C++ over Java in vision
  • Substantially higher speed
  • Better handling of video
  • Used more frequently for computer vision programming
  • Good tracking and face recognition algorithms in OpenCV
For me, Java has been very good for doing medical image processing algorithms. I have heard conflicting evidence about whether it is feasible for doing computer vision on video using Java. Video handling in Java has been bad up to now, this is supposed to be fixed with the new JavaFX. Shadow Monsters is a computer vision based art piece taking video footage of silhouette of the viewer and adding monsters to them, I saw it on display at Museum of Modern Art. It was programmed using Processing, which is a Java based image processing tool for artists. I discussed the issue with a computer programmer / artist who said that he had tried to do a motion algorithm in Processing and had to port it to C++ based Openframeworks since Java was too slow. After being discouraged by my prior attempts to do vision in C++, I am very happy to see the dramatic developments in C++ and see if it is suitable for a simple port of ShapeLogic algorithms. The result of this C++ port will be covered in 2 postings:

-Sami Badawi
http://www.shapelogic.org

5 comments:

Henrik said...

Hello Sami,

I wish we could have met at the big reunion at Rockmaskien.

Best regards

Henrik

Charles said...

Thanks for writing down your insights into Java vs C++ computer vision. I really found it helpful.

Sankar said...

Very good comparison between c++ and java.


sankar
java training in chennai

Unknown said...

Thanks for your informative post!! It helped me to understand the difference between Java and C++ programming languages. Java Training in Chennai

Unknown said...

In my personal opinion, C++ is much more easier to learn as compared to Java. C++ in Urdu