How to get pixels mean values of a matrix
Show older comments
Hi,
I'm trying to calculate average values of group of data in a matrix. Meaning, imagine a 4x4 matrix, I want to know the average value of the four pixels in the top left of the matrix (1:2,1:2), top right, bottom left and bottom right so that in the end I have a 2x2 matrix representing the average of 2x2 groups of the previous matrix. The hard part is I want to do this using parallel computing. Originally, I was using the convn function:
convn(A,b,'valid)
%Where A is my ''image'' matrix
% b is a 0.25*ones(2,2) so that the convolution gives me an average
The issue with that is that I calculate extra values. If I take my precedent exemple, with convn(A,b,'valid), I would optain a 3x3 matrix as a result while only the values in the corner are of interest to me. Now imagine the same problem with big matrix, I would save a lot of time by skiping over these in between values.
Thanks for the help.
Accepted Answer
More Answers (1)
Image Analyst
on 8 Jun 2018
Edited: Image Analyst
on 8 Jun 2018
Then just compute the four corners if that's all you want.
upperLeftMean = mean2(A(1:2, 1:2));
upperRightMean = mean2(A(1:2, end-1:end));
lowerLeftMean = mean2(A(end-1:end, 1:2));
lowerRightMean = mean2(A(end-1:end, end-1:end));
Not sure if you want the average in a 4x4 window, or a 2x2 window - you said it both ways. But the changes to make is a 4x4 are obvious:
upperLeftMean = mean2(A(1:4, 1:4));
upperRightMean = mean2(A(1:4, end-3:end));
lowerLeftMean = mean2(A(end-3:end, 1:4));
lowerRightMean = mean2(A(end-3:end, end-3:end));
Categories
Find more on Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!