How to get pixels mean values of a matrix
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Raphaël
el 8 de Jun. de 2018
Comentada: Ameer Hamza
el 8 de Jun. de 2018
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.
0 comentarios
Respuesta aceptada
Ameer Hamza
el 8 de Jun. de 2018
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 comentarios
Image Analyst
el 8 de Jun. de 2018
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
el 8 de Jun. de 2018
@Image Analyst, thanks for the explanation about the efficient implementation of conv2().
Más respuestas (1)
Image Analyst
el 8 de Jun. de 2018
Editada: Image Analyst
el 8 de Jun. de 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));
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!