# I need to create a 3 by 3 window and calculate mean within the window. This window is to move through the image. How can I do it?

5 views (last 30 days)
Sibin Prasad on 6 Apr 2017
Commented: Sibin Prasad on 9 Apr 2017
I am new to MATLAB. This is what I have done. I got this from here and there online. Is there an efficient way of doin it?
Thank you
for i = side:p
for j = side:q
nhood_ctr = [i j];
nhood = im3((nhood_ctr(1)-floor(side/2)):(nhood_ctr(1)+floor(side/2)),(nhood_ctr(2)-floor(side/2)):(nhood_ctr(2)+floor(side/2)));
%size(nhood)
avg(i,j) = mean(mean(nhood));
sum = 0;
for k = (nhood_ctr(1)-floor(side/2)):(nhood_ctr(1)+floor(side/2))%i-2:i+2
for l = (nhood_ctr(2)-floor(side/2)):(nhood_ctr(2)+floor(side/2))%j-2:j+2
x = im3(k,l)- avg(i,j);
sum = sum + x*x;
end
end
igvm(i,j) = sum;
end
end

Joseph Cheng on 6 Apr 2017
I would take a look at the conv() and conv2() function. you would use something like [1 1 1;1 1 1;1 1 1]/9 to get mean. and then probably select 'same' or valid depending on how you want to treat the edges of the image