Averaging Matrix and store it in a new matrix
조회 수: 5 (최근 30일)
이전 댓글 표시
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?
답변 (4개)
Daniel Shub
2012년 5월 18일
Assuming your data x is N x M and not a color image
y = conv2(x, ones(3, 3) / 9, 'same');
댓글 수: 6
Daniel Shub
2012년 5월 18일
@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.
Image Analyst
2012년 5월 18일
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.
Daniel Shub
2012년 5월 18일
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
Sean de Wolski
2012년 5월 18일
If you have to use loops:
for ii = 1:1
y = conv2(x, ones(3, 3) / 9, 'same');
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!