# Speeding Up Image Processing Functions

I am working with stacks of 1024x1024 timelapse microscopy images that I am performing a series of image processing functions on to help segment each image. These functions include Sharpen, Erosion, Dilation, RidgeFilter, FillingTransform and WatershedComponents (with hand-selected markers). After some trial and error it seems I need to at least use the following functions to arrive at a generally acceptable segmentation:

watershedLabelMatrix = WatershedComponents[RidgeFilter[image],markersForWatershed]


I would like to know if there is anyway to speed up these image processing steps (particularly the bit of code above). I have not had success with use of Compile or Parallelize. I am not sure if it is possible to Compile a function that takes an image as its argument. Here's one example of what failed for me:

watershedCompile1 = Compile[{{imageIn, _Real}, {markersIn, _Real}},
WatershedComponents[imageIn, markersIn]]


I get this message after evaluating:

Compile::extscalar: "WatershedComponents[imageIn,markersIn] cannot be compiled and will be evaluated externally. The result is assumed to be of type Real."

Similarly, when I try to Parallelize I am told WatershedComponents cannot be parallelized.

When I try to use WatershedComponents "on the fly" within a little DynamicModule interactive notebook I have written that iterates through a stack of images and allows me to save acceptable WatershedComponent masks, I get a long (maybe 5 to 10 seconds) pause while I wait for the WatershedComponents result and usually see the dreaded Progress: Formatting Notebook Contents dialog window popup.

Any suggestions on improving the performance of these image processing functions or suggestions for how to improve performance within a DynamicModule (or alternatives to DynamicModule) would be greatly appreciated.

• Parallelize has a somewhat misleading name. It won't magically speed up anything. Compile also supports only certain functions, and compilation likely won't help at all with these functions, it might even hurt performance a tiny bit. This is, of course, not an answer to your question about how to speed up the operation. – Szabolcs Jul 29 '14 at 20:24
• Many Image Processing Functions can be accelerated by using CUDALink and OpenCLLink. Erosion and Dilation have build in CUDA functions (CUDAErosion, CUDADilation). – paw Jul 30 '14 at 9:00
• Have you tried to scale down the image? Of course, it depends on your application if that works. I saw in one of your previous questions that you have a lot of nothing around the area of interest, it would probably be faster if you would ImageCrop to just use WatershedComponents on the area of interest. – C. E. Jul 30 '14 at 10:50
• I don't think there is much you can do to speed up the built-in WatershedComponents. It is implemented using a separate program ITK.exe which I assume is an implementation of the ITK toolkit. I suspect paw's suggestion to use GPU processing might be the best approach to getting performance fast enough for a dynamic interface. A Google search for watershed components gpu gets some interesting looking results. – Simon Woods Jul 30 '14 at 11:45
• @Pickett I haven't tried cropping the images but resizing them from 1024x1024 to 512x512 does give roughly a 4-fold improvement in time for WatershedComponents to evaluate. – user13999 Aug 1 '14 at 1:38