calculate patch of rgb image

3 views (last 30 days)
Pam
Pam on 24 Sep 2013
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

Accepted Answer

Image Analyst
Image Analyst on 24 Sep 2013
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');
  2 Comments
Pam
Pam on 25 Sep 2013
Thank you for your answer.
I'd like to average a 2D 4x4 on R G and B in order to reduce cost of processing.
Image Analyst
Image Analyst on 25 Sep 2013
What does that mean? Doing that will only add to processing time. Plus doing it for even number of elements is unusual because it gives you a half pixel shift in the image. Finally you haven't said if you want to average over color planes or keep them separate.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!