How to get pixels mean values of a matrix

조회 수: 2 (최근 30일)
Raphaël
Raphaël 2018년 6월 8일
댓글: Ameer Hamza 2018년 6월 8일
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.

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 6월 8일
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
Image Analyst
Image Analyst 2018년 6월 8일
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").
Ameer Hamza
Ameer Hamza 2018년 6월 8일
@Image Analyst, thanks for the explanation about the efficient implementation of conv2().

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 6월 8일
편집: Image Analyst 2018년 6월 8일
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
Raphaël
Raphaël 2018년 6월 8일
ya, no. The first exemple was a simple one. I'm actually working with big matrix in e dimensions.
Thanks anyway.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by