How to get pixels mean values of a matrix

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

Use blockproc() to individually process each block. For example to get an average value of each [2 x 2] block use
averageA = blockproc(A, [2 2], @(x) mean2(x.data), 'UseParallel', 1)
'UseParallel' specified to use parallel processing.

7 Comments

It works thanks. One question. What does the 1 at the end stand for?
blockproc() covers the entire matrix while Raphael said that "only the values in the corner are of interest to me" and he want to be " skiping over these in between values". So I would not use blockproc() because it processes, not skips, the in between values.
In the question, "skipping over" was actually for conv() function, since it can do the average but its overlap the processing blocks. The OP was mentioning about skipping these extra values that conv() calculate.
@Raphaël, 1 means that you want to use parallel processing, perhaps the following syntax will make it more clear
averageA = blockproc(A, [2 2], @(x) mean2(x.data), 'UseParallel', true)
ok, thanks.
And yes I want to skip over the overlap. Strangely enough, I just tested and despite calculating overlap, the convn function is still much faster than the blockproc.
I suppose that is because convn use matrix multiplication which is implemented pretty efficiently in MATLAB, whereas blockproc() is calling an anonymous function for each block which makes it slow. The faster solution will be to delete the appropriate rows and columns of the convn() result.
conv() is very highly optimized. For example when you move over one column or down one row, all it has to do is to "sum in" that one sliver of values that is new to the window, whereas blockproc() has to read all the values. So because of that, plus calling a function like Ameer said, it may actually be faster to use conv2() or convn() than blockproc().
For what it's worth, I'm attaching some blockproc demos, in case anyone wants some more examples of how it can be used, including one where it can be used to do a variable amount of overlap rather than no overlap ("jumps").
@Image Analyst, thanks for the explanation about the efficient implementation of conv2().

Sign in to comment.

More Answers (1)

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));

1 Comment

ya, no. The first exemple was a simple one. I'm actually working with big matrix in e dimensions.
Thanks anyway.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!