smoothing image with moving average filter

2 views (last 30 days)
Momonka
Momonka on 23 Oct 2018
Answered: Rushil on 21 Feb 2025
Hello i have a question. how can i smoothing this image with moving average filter 5*5 for only green Thank you so much.

Answers (1)

Rushil
Rushil on 21 Feb 2025
Hi
From what I understood, the task at hand is to compute a moving average over each 5x5 window, in order to smoothen the image. This can be accomplished using the conv2 function, using a filter of size 5x5 with all values being 1/25. You can find the supporting documentation of conv2 at the link below:
Below is some implementation that may help:
img = imread("file_name.jpeg");
% isolate the green channel
green = img(:, :, 2);
filter = ones(5, 5) / 25;
% here we use convolution for faster implementation
% the result is a moving mean in a 5x5 window
paddedGreenChannel = padarray(green, [2, 2], 'replicate');
% padding to avoid wrong mean calculation for edges
smoothedGreenChannel = conv2(double(paddedGreenChannel), filter, 'valid');
smoothedGreenChannel = uint8(smoothedGreenChannel);
% replace the green channel with the update one
smoothedImg = img;
smoothedImg(:, :, 2) = smoothedGreenChannel;
% to plot and compare both the images
figure;
subplot(1, 2, 1);
imshow(img);
title('Original Image');
subplot(1, 2, 2);
imshow(smoothedImg);
title('Smoothed Image');
Hope it's helpful

Community Treasure Hunt

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

Start Hunting!