MATLAB Answers

How do you average each pixel in an image with a 9x9 square?

3 views (last 30 days)
Eric Yang
Eric Yang on 18 Oct 2020
Answered: Image Analyst on 18 Oct 2020
I tried to provide a diagram for my situation. Essentialy I have a 1280 X 1024 pixel image that needs to be processed through an equation that specifically requires each individual pixel to be put through a function in a 9x9 square, ignoring the edge cases where a 9x9 square cannot be formed. I've created a nested for loop that goes through each individual pixel in the image, but don't know how to select a 9x9 matrix to work with. Based on the diagram below, the target pixel iterates from the black, to purple, to blue, then to pink box.
As the function processes each individual pixel, I do not want it to modify the original image, rather store it in a new image. For context, this is for producing a laser speckle contrast image.
I tried following the documentation from https://www.mathworks.com/matlabcentral/answers/272994-calculate-average-around-pixel ,but I don't see how I can apply it for successive pixels.
I've also tried using mat2tiles, but that doesn't meet the criteria for the problem i'm trying to solve since it creates a tile, rather than average the next pixel in the image.

  3 Comments

Rik
Rik on 18 Oct 2020
What specific function do you mean? You should really try to avoid a nested loop for image processing.
Rik
Rik on 18 Oct 2020
I would suggest calculating all terms individually. When I'm back at a computer I can write an example for you.
Eric Yang
Eric Yang on 18 Oct 2020
I followed your suggestion and obtained individual 9x9 submatrices around the pixel of interest for each term. Not sure if this is the best way to do it, but it works.
Thanks for your suggestion.

Sign in to comment.

Accepted Answer

Rik
Rik on 18 Oct 2020
Edited: Rik on 18 Oct 2020
If you want the average (or something like a gaussian blur): use conv2, as described in the answer you linked. Read the documentation for that function to see how to keep only the results from the places where you have a full surrounding field.

  0 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 18 Oct 2020
Try conv2() or imfilter():
kernel = ones(9)/81; % Define filter window to take the mean in a 9x9 square.
blurredImage = imfilter(grayImage, kernel); % blurredImage is the same class as grayImage.
% or
blurredImage = conv2(double(grayImage), kernel, 'same'); % blurredImage is a double.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!