1
votes

I am trying to implement a 3D Gaussian Filter in Matlab - WITHOUT using inbuilt Matlab filtering functions like imfilter, imgaussfilt etc...


I have a 3D data

RAW(K,K,K)

Say, K = 100, and filter width delta = 5 for this example.

Currently, I have :

Ggrid = -floor(delta/2):floor(delta/2);
[X Y Z] = meshgrid(Ggrid, Ggrid, Ggrid);

% Create Gaussian Mask
GaussM = exp(-(X.^2 + Y.^2 + Z.^2) / (2*delta^2));

% Normalize so that total area (sum of all weights) is 1
GaussM = GaussM/sum(GaussM(:));

Which gives me a 3D Gaussian Kernel of dimensions K+1 * K+1 * K+1. Now, to get the filtered data, I want to do a convolution like :

FilteredData = conv(RAW,GaussM);

but there is a dimensional mismatch. Can someone point out where I am going wrong? I assume I am making some mistake in the Gaussian mask GaussM.

1

1 Answers

1
votes

Don't use conv as this is for 1D signals. Use convn for N-dimensional filtering where you can specify kernels of any dimension, including 3D.

As such, it's just a matter of:

FilteredData = convn(RAW, GaussM);

Also take note that the default operation is to provide an output that performs full convolution, meaning that the size of the output will be larger than the original size of RAW. To ensure that the output of convn is the same size as RAW, specify the 'same' flag as the third parameter into convn:

FilteredData = convn(RAW, GaussM, 'same');