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. Wikipedia gives an overly complicated of a morphological dilation operation. The simple explanation of dilation is that for each pixel we look at the pixels above, below, left, right, and kitty corner to the pixel. In the case of a black and white binary image if any of the pixels are white, the algorithm sets the pixel to white, otherwise it sets it to black. The image below shows an input image, the thresholded binary image, and then the application of a single dilation operation.
Dilation
The SimpleCV python code to do this is as follows:
cam = Camera() img = cam.getImage() binary = img.binarize() dilate = binary.dilate() result = img.sideBySide(binary) result = result.sideBySide(dilate) result.show()
Our first pass of replicating this in SimpleCV js just tried to get the functionality right. We didn't pay much attention to the run time. Also note that this is just hacked code, not production, some features aren't implemented (e.g. working on both grayscale and color images). Our first pass naive code looked like this:
rgbxy2idx:(x,y,w,h) => bpp = 4 return (bpp*y*w)+(bpp*x) ptDilate:(x,y,img,w,h,offset=0)=> a = img[offset+@rgbxy2idx(x+1, y-1,w,h)] b = img[offset+@rgbxy2idx(x, y-1,w,h)] c = img[offset+@rgbxy2idx(x-1, y-1,w,h)] d = img[offset+@rgbxy2idx(x+1, y, w,h)] e = img[offset+@rgbxy2idx(x, y, w,h)] f = img[offset+@rgbxy2idx(x-1, y, w,h)] g = img[offset+@rgbxy2idx(x+1, y+1,w,h)] i = img[offset+@rgbxy2idx(x, y+1,w,h)] j = img[offset+@rgbxy2idx(x-1, y+1,w,h)] r = Math.max(a,b,c,d,e,f,g,i,j) return r dilate:(iterations=1,grayscale=false)=> if( iterations < 1 ) iterations = 1 border = 1 w = @width+(2*border) h = @height+(2*border) # We create a one pixel border to make our operation uniform across the image. out = @cloneWithBorder(border) sz = out.length temp = @cloneWithBorder(border) istart = border istop = border+@width-1 jstart = border jstop = border+@height-1 # now go through the image, row first for k in [1..iterations] for j in [jstart..jstop] #Y for i in [istart..istop] #X out[@rgbxy2idx(i,j,w,h)]=@ptDilate(i,j,temp,w,h) # get the dilation at the point and store it out[1+@rgbxy2idx(i,j,w,h)]=@ptDilate(i,j,temp,w,hoffset=1) # do this for every channel out[2+@rgbxy2idx(i,j,w,h)]=@ptDilate(i,j,temp,w,h,offset=2) # yes this is for the RGB case #temp = out return @cropBorderCopy(out,border) # get rid of the border and return the results
Using the chrome JavaScript profiler/debugger and our [example application] (http://demo.simplecv.org/), we noticed that this code was as slow as molasses in January. What could we do to speed it up? Do we need all the overhead of all of those function calls? The functions ptDilate and rgbxytoidx make things easier to read and maintain, but they get called an awful lot. Let's see what replacing them does for us. Here is the revised code.
dilate2:(iterations=1,grayscale=false)=> if( iterations < 1 ) iterations = 1 border = 1 w = @width+(2*border) h = @height+(2*border) out = @cloneWithBorder(border) sz = out.length temp = @cloneWithBorder(border) istart = border istop = border+@width-1 jstart = border jstop = border+@height-1 bpp = 4 for k in [1..iterations] for j in [jstart..jstop] #Y for i in [istart..istop] #X for offset in [0..2] a = temp[offset+(bpp*(j-1)*w)+(bpp*(i+1))] b = temp[offset+(bpp*(j-1)*w)+(bpp*(i ))] c = temp[offset+(bpp*(j-1)*w)+(bpp*(i-1))] d = temp[offset+(bpp*(j)*w)+ (bpp*(i+1))] e = temp[offset+(bpp*(j)*w)+ (bpp*(i ))] f = temp[offset+(bpp*(j)*w)+ (bpp*(i-1))] g = temp[offset+(bpp*(j+1)*w)+(bpp*(i+1))] h = temp[offset+(bpp*(j+1)*w)+(bpp*(i ))] l = temp[offset+(bpp*(j+1)*w)+(bpp*(i-1))] r = Math.max(a,b,c,d,e,f,g,h,l) out[offset+(bpp*j*w)+(bpp*i)] = r #temp = out return @cropBorderCopy(out,border)
Running this code versus the original showed a noticable increase in performance. We are still getting familiar with Chrome's javascript profiler but from what we can tell it looks like the dilate2 operation is almost 4 times faster! Here is our profiler output for a short run of a few different code snippets.
Profiler output
So our second try is a lot better, but we noticed that we are looking up a lot of values repeatedly. For the area to the left of the target pixel, that is the pixels NW,N,W,SW,S of our pixel, we look them up three times in rapid succession. What happens if we modify our code to avoid these look ups? We will call this the student body left approach. Basically for the first pixel on a row we look up all the values, for each successive pixel on the row we shift over the values we found on the last iteration and just look up the next three pixels on the right. Here is the code for that:
dilate3:(iterations=1,grayscale=false)=> if( iterations < 1 ) iterations = 1 border = 1 w = @width+(2*border) h = @height+(2*border) out = @cloneWithBorder(border) sz = out.length temp = @cloneWithBorder(border) istart = border istop = border+@width-1 jstart = border jstop = border+@height-1 bpp = 4 a = 0; b = 0; c = 0; d = 0; e = 0; f = 0; g = 0; h = 0; l = 0; r = 0 for k in [1..iterations] for offset in [0..2] for j in [jstart..jstop] #Y for i in [istart..istop] #X if i == istart a = temp[offset+(bpp*(j-1)*w)+(bpp*(i-1))] b = temp[offset+(bpp*(j)*w)+ (bpp*(i-1))] c = temp[offset+(bpp*(j+1)*w)+(bpp*(i-1))] d = temp[offset+(bpp*(j-1)*w)+(bpp*(i ))] e = temp[offset+(bpp*(j)*w)+ (bpp*(i ))] f = temp[offset+(bpp*(j+1)*w)+(bpp*(i ))] g = temp[offset+(bpp*(j-1)*w)+(bpp*(i+1))] h = temp[offset+(bpp*(j)*w)+ (bpp*(i+1))] l = temp[offset+(bpp*(j+1)*w)+(bpp*(i+1))] r = Math.max(a,b,c,d,e,f,g,h,l) out[offset+(bpp*j*w)+(bpp*i)] = r else # student body left a = d b = e c = f d = g e = h f = l g = temp[offset+(bpp*(j-1)*w)+(bpp*(i+1))] h = temp[offset+(bpp*(j)*w)+ (bpp*(i+1))] l = temp[offset+(bpp*(j+1)*w)+(bpp*(i+1))] r = Math.max(a,b,c,d,e,f,g,h,l) out[offset+(bpp*j*w)+(bpp*i)] = r #temp = out return @cropBorderCopy(out,border)
Hrm. Our run time for dilate2 was