Why are imageJ functions like dilate so fast?

Firstly, I’m quite new to coding so I apologise for any oversight on my part. I always try to find the answers myself, but I’m a bit stumped with this question.

I’ve been using MATLAB to create a function to ‘dilate’ a binary region in a matrix, but the process takes a little while to complete (about 5 minutes for a 5GB image (7500,7500,12 matrix dimensions)). Originally I designed the function to iteratively go through each pixel and ask ‘if img(y,x,z) ==1’ then apply function. The function being overlaying a predefined matrix containing a binary disk on the img coord. I sped this up with the ind2sub function, but the FIJI version is still much much faster.

I assume the FIJI function only applies a similar function to the outer pixels of a defined ‘region of interest’ found through some sort of connectivity function?

I’ve checked: ImageJ (Java2HTML), but can’t seem to find the code for the process ‘dilate’ or the filter ‘maximum…’ in FIJI, and was wondering if anyone could help me understand just why FIJI functions are so efficient? I’m not from a computer science background so I would appreciate a detailed and clear explanation or some references to sites which can provide the info.

Thanks in advance,

Hi Tom,
why you do not use imdilate function? It is quite well optimized.

1 Like

If you want to find the source code of an ImageJ-command, go to:
“Plugins >> Utilities >> Find Commands…” or type “Cmd L”.
Then search for the Command and click “Source”.
(You need an open internet connection.)

1 Like

Thanks for a great answer Ilya, I will try this when I get home tonight. Me over complicating things as per usual! I will also check the imdilate code to see how it is layed out.


Thanks notQRT, exactly what I was looking for to be able to check the FIJI codes! I’ll have a look see when I’m back home.

Hi Tom,

One reason the ImageJ version runs fast is that is uses parallelization. Hence, if your computer has many cores you can easily divide total computation time.

However, as Ilya pointed out, the imdilate function (and many others in Image Processing Toolbox) is highly optimized , and is much faster than script implementation. Moreover, some functions, including imdilate, have been enabled to perform image processing operations on a GPU. I would be surprised that native ImageJ runs faster than Matlab.

The algorithm may also come into play. For example, using dilation with cube structuring elements allow to replace by three dilations with lines (in the three main directions), resulting in a much lower computational complexity, and (usually) time. This is implemented in Matlab, not in pure ImageJ AFAIK.

There are also faster implementations for ImageJ, in particular CLIJ that benefits from GPU power.


Much apprechiated dlegland. This provides me with a good grasp of some of the underpinning mechanics that were foreign to me, and a good platform fom which to learn more. I’m rather looking forward to reading up on it.

Thanks all for answering a rather ambiguous and broad question.

1 Like