Repository navigation
Experiments in Optimization
We are currently building out the functionality of SimpleCV.js and we are slowly attempting to replicate all of the functionality in the python version of (SimpleCV)[http://www.simplecv.org]. This is great except for the fact that SimpleCV builds upon the already existing OpenCV library. OpenCV has been under development for over ten years and many of the core C/C++ algorithms have been optimized ad nauseum. In SimpleCV we take the raw python bindings provided by OpenCV, clean them up, and add our own secret sauce that makes them easy to use. Unfortunately, in CoffeeScript/JavaScript land we can't make use of these routines. There are no quick and easy was to cross compile C++ to java script or coffee script. To make matters worse many of the C++ routines in OpenCV are highly templated, which means it is non-trivial to take a chunk of working C++ image processing source code and modify so it looks like javascript or coffee script. To create a lot of the functionality we want in SimpleCV.js our only option is to re-invent the wheel.
Our main computer vision developers are mainly C/C++/Python hacks, and the transition to CoffeeScript has been a great learning experience. Today we decided to dive into some code optimization to see how we should best approach improving the run time of SimpleCV.js. This is not a definitive and final optimization as our depth of knowledge on CoffeeScript/JavaScript are still rather shallow. .This was primarily an instructive effort that we hope could spark discussion among the community.
For many computer vision applications you want to work with a neighborhood of pixels, however to avoid memory fragmentation and improve computational efficiency we represent an image as a one dimensional array. In SimpleCV.js we represent images as one dimensional arrays of Uint8 values, where each pixel is stored sequentially in red, green, blue, alpha order, and we work from left to right, top to bottom. So for a an image array, let's call it IMG, IMG[0] is the red value of the pixel at (0,0), IMG[1] is the green value of the pixel at (0,0), IMG[4] is the red value at (1,0), and IMG[IMG.length] and the alpha value for the pixel at (width-1,height-1). While this approach is great for storing data it makes it a pain to work with a neighborhood of pixels.
For SimpleCV.js we wanted to add a morphological dilation operation. Morphological dilation is usually used on black and white (binary) images to fill in little holes and connect regions.