简体   繁体   中英

Why are morphological operations on 3D images so slow in Matlab?

I am running Matlab 2016a and use imopen on a 301x301x271 logical image. Example code:

A = randi([0 1], 301, 301, 271);
A = logical(A);
se = strel('sphere',12);
tic;
A = imopen(A, se);
toc;

Elapsed time is 294.313918 seconds.

Using all 4 CPU cores. Calling the same code from within a function uses only one core for some reason (600s).

function CreateExample()
...code...
end

Doing the same thing using MITK (which basically just uses itkBinaryMorphologicalOpeningImageFilter) takes less than 10 seconds.

Any ideas for optimizations? Gpuarray is not possible because it's 3D.

I tried to run both the situations you mentioned (in a script and within a function), and in both cases, I noticed that MATLAB uses multiple cores (4 in my case too), and so I cannot reproduce this behavior.

However, I believe one of the major reasons that MATLAB is slower than ITK for a sphere is that a 3D sphere is not decomposed into smaller simpler shapes.

SE = strel('disk', 12);
sum(SE.Neighborhood(:))

is 697, which is the number of 'on' pixels in the 3D sphere.

The cube on the other hand is decomposed, meaning:

SE = strel('cube', 25);
% The decompose method replaces one cube with three 3D lines, 
% applied repeatedly over the volume.
seq = SE.decompose() 
sum(seq(1).Neighborhood)
sum(seq(2).Neighborhood)
sum(seq(3).Neighborhood)

This reduces the number of comparisons to be made from 25 cubed to 3 times 25, which is probably what ITK does to optimize this.

I would be curious to know if you use a cube, like 'se = strel('cube',25);' instead. I'm fairly certain it is much faster. If that is true, you could investigate decomposing the sphere and using it instead, for example:

Vaz, MS, Kiraly, AP, & Mersereau, RM (2007). Multi-level decomposition of Euclidean spheres. In Proc. Int. Symp. Math. Morphology (ISMM) (pp. 461-472).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM