Applying low pass filter to each pixel in 3D stack

2 views (last 30 days)
I have a stack of 200 images that I wish to perform low-pass filtering on to remove noise.
I have come up from a crude method, that involves analyzing each individual pixel in the third dimension, applying the low pass filter and adding the result to a new 3D matrix.
placeholder_image = zeros(size_pic(1),size_pic(2),Frames);
for n = 1:354
for k = 1:497
indvidual_pixel = gaus_filt(n,k,:);
indvidual_pixel = squeeze(indvidual_pixel);
filtered = lowpass(indvidual_pixel,0.2);
placeholder_image(n,k,:) = filtered(:,1);
end
end
Is there a better way to do this?

Answers (1)

Soumya
Soumya on 26 Jun 2025
Hi @ John Doe,
Vectorization is a programming technique where operations are applied to entire arrays or matrices simultaneously, rather than iterating element by element using explicit loops. By reshaping the 3D image stack into a 2D matrix, where each column corresponds to the time-series of a single pixel, the lowpass function can be applied simultaneously across all pixels without explicit loops. This approach not only improves speed significantly but also reduces overhead and makes better use of memory. After filtering, the data can be reshaped back into the original 3D form.
The following steps can be followed to achieve the same:
  • Reshape the 3D stack into a 2D matrix:
[rows, cols, frames] = size(gaus_filt);
reshaped_data = reshape(gaus_filt, [], frames);
Size: [rows*cols, frames]
  • Transpose the matrix to have time-series as columns:
data_T = reshaped_data.';
  • Apply the low-pass filter simultaneously to all pixel time-series:
filtered_T = lowpass(data_T, 0.2);
  • Transpose back and reshape to the original 3D size:
filtered_data = reshape(filtered_T.', rows, cols, frames);
The following shows how the outputs matches but the elapsed time is less in the vectorized method:
Please refer to the following documentation to get more information on vectorization in MATLAB:
I hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!