I have a few pixels in an image and I want to calculate the average values of a 3×3 window centred on those pixels in Matlab. It should be noted that not all over the image, just a few desired pixels.
I tried this code and it works but I prefer to avoid using for loops. Is there another way for doing that?
clcclearclose allimg = imread('cameraman.tif');x= [10,15,20];y= [15,25,35];m=-1;n=-1;window = zeros(3,3);Avg = zeros(max(size(x)),1)for k=1:max(size(x)) for i = 1:3 for j=1:3 window(i,j) = img(x(k)+m,y(k)+n); n=n+1; end n=-1; m=m+1; end Avg(k) = mean(window,'all')end
Best Answer