Averaging Matrix and store it in a new matrix

Assuming i have a 125x125 image. and i want to create new matrices where each point consists of the average of the surrounding 8 points and itself. but ignoring the average for the pixels on the edge for simplicity.
x = imread('image.png');
[rows,cols] = size(x);
for r = 1:rows;
for c = 1:cols;
%it does not seem working by using to inbult mean function here since its a m x n matrix i want to calculate.
end
end
so for example, i have a matrix of
x = [1 2 3 4; 5 6 7 8; 9 10 11 12;13 14 15 16]
m = x(1:3,2:4)
z = mean(x(1:3, 2:4))
---------------------------------
x =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
m =
2 3 4
6 7 8
10 11 12
z =
6 7 8
but what i really want of z is z = (2+3+4+6+7+8+10+11+12)/9 = 7 is there anyway to do this?

Respuestas (4)

Daniel Shub
Daniel Shub el 18 de Mayo de 2012
Assuming your data x is N x M and not a color image
y = conv2(x, ones(3, 3) / 9, 'same');

6 comentarios

matlab_newbie
matlab_newbie el 18 de Mayo de 2012
thank you very much for answering, but it doesnt seem like the answer i really want.
by using the mean function is there anyway to calculate the average of all element in the x(1:3, 2:4) which is a 3x3 matrix inside a larger matrix, in this example its 4x4, or even larger, maybe 125x125.
Image Analyst
Image Analyst el 18 de Mayo de 2012
I think Daniel meant to say 'valid' instead of 'same'. Do you want every 3x3 array, or you only want the one, single 3x3 array located between rows 1 and 3 and columns 2 and 4?
matlab_newbie
matlab_newbie el 18 de Mayo de 2012
ya, i want every 3x3 array in every location of the 125x125 matrix, can i use any loop somehow to achieve this?? and the mean function seems wont calculate average of m x n array, am i rite? but just either columns, or rows.
Sean de Wolski
Sean de Wolski el 18 de Mayo de 2012
use conv2() as daniel has done above (with the 'valid' option as ImageAnalyst pointed out)
Give it a try, you'll see that it works and learn something very powerful all at once.
matlab_newbie
matlab_newbie el 18 de Mayo de 2012
it works well, but can i use any loop plus mean function to achieve this?
lets say i cant use the conv2() function. thank you very much
Daniel Shub
Daniel Shub el 18 de Mayo de 2012
@IA and Sean, I meant SAME and not VALID. While newbie wanted to ignore the edges for simplicity, I wanted to show that the edges could be handled.

Iniciar sesión para comentar.

Junaid
Junaid el 18 de Mayo de 2012
try this if it works for you.
x = [1 2 3 4; 5 6 7 8; 9 10 11 12;13 14 15 16];
f = fspecial('average',3,3); % create filter of 3 x 3
b = imfilter(x,f);
and to see the different configuration to handle boundary values, you can see the documentation of imfilter.
Image Analyst
Image Analyst el 18 de Mayo de 2012
Try this:
% Get the average, avoiding any edge effects due to partial masking.
outputArray = conv2(inputArray, ones(3)/9, 'valid');
% Now get "z" which they defined as the mean
% of each column (averaging going down the rows)
z = mean(outputArray, 1);
% Get their second definition of z
% which is the mean of all the elements in their first z:
z = mean(z);
But isn't this second z just the same as
z = mean2(x(2:end-1, 2:end-1));
????? It gives a single number that is the mean of x ignoring the edge rows and edge columns.

1 comentario

matlab_newbie
matlab_newbie el 18 de Mayo de 2012
thank you very much for trying to help!! it kind of helps!! as i hv never knew the 'mean2' function.
but, that only gives one single averaged number of the entire matrix.
what i want to achieve is, have a average for every 3x3 array inside this entire 125x125 matrix and store(or output) it into a new array (or matrix i should say).
so for example,
x =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
i want [1 2 3;5 6 7;9 10 11] averaged which is = 6. and [2 3 4;6 7 8;10 11 12] = 7, and [5 6 7;9 10 11;13 14 15] = 10. and so on. then store these values into a new array ... which maybe z = [6 7 10]
it pretty confusing, so we are kinda ignoring to take the average of the edge columns and rows, but when we calculate the average inside the matrix should still include the edges. which 'taking the average of every 3x3 array in the entire matrix of 125x125' should be able to express this idea..but i have no idea how...im definitely thinking i should loop it somehow. and how can i store the output in a new variable(so create a new matrix)?
can you please help, thank you very much.

Iniciar sesión para comentar.

Daniel Shub
Daniel Shub el 18 de Mayo de 2012
Assuming you really want to do this with loops and mean, presumably for homework, you are on the correct track. Te thing you missed is the the mean of 6 7 8 is 7, the answer you want. S just replace
z = mean(x(1:3, 2:4))
with
z = mean(mean(x(1:3, 2:4)))
or even better
z = mean(m(:))

1 comentario

Sean de Wolski
Sean de Wolski el 18 de Mayo de 2012
If you have to use loops:
for ii = 1:1
y = conv2(x, ones(3, 3) / 9, 'same');
end

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 18 de Mayo de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by