Consider using imfilter
from the Image Processing Toolbox. imfilter
is essentially an optimized convolution and correlation pipeline that takes advantage of the Intel Integrated Performance Primitives. Just as a simple test, let's create a random double
precision 200 x 200 x 150 3D matrix and we want to find the average of 9 x 9 x 9 pixel neighbourhoods as you said:
A = rand(200,200,150);
kernel = (1/9^3)*ones(9,9,9);
B = imfilter(A, kernel);
This actually runs quite fast on my machine. My specs are a MacBook Pro with 16GB of RAM running a 2.3 GHz Intel Core i7 processor.
Just to satisfy curiosity, I used timeit
to time how long this operation takes once you allocate the kernel and the matrix:
A = rand(200,200,150);
kernel = (1/9^3)*ones(9,9,9);
B = imfilter(A, kernel);
t = timeit(@() imfilter(A, kernel));
On average, the above code runs for about 1.1393 seconds:
>> t
t =
1.1393
Unless you migrate over to GPUs, this to me is the fastest way you're ever going to get it... especially because you have 150 slices of 200 x 200 2D data you have to process with each point collecting a 9 x 9 x 9 volume of elements.