MATLAB: Calculate patch of rgb image

averagepatchrbg

Hello
I have a 320x200x3 rgb image. I'd like to calculate 4x4 pixels patch from top to bottom and left to right for finding the average value of each patch. Could you please suggest me calculating without for loop (or less in use a loop)
Thank you

Best Answer

What are you averaging? The whole 4x4x4 cube? Or just a 2D 4x4 square on a color channel by color channel basis? And more importantly, why?
You can extract the color channels and get the average like this:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
kernel = ones(4)/16;
blurredRed = conv2(double(redChannel), kernel, 'same');
blurredGreen = conv2(double(greenChannel), kernel, 'same');
blurredGreen = conv2(double(blueChannel), kernel, 'same');
blurredRGB = cat(3, blurredRed, blurredGreen, blurredBlue);
Or you can blur the colors while mixing colors by using convn
blurredRGB = convn(double(rgbImage), ones(4,4,4)/64, 'same');