MATLAB: Take average of the nearest n pixels

arraymaskingmatrixmean

I have a 512x512x100 matrix of an image. I want to replace every pixel in a certaint section of that matrix (i have a seperate 512x512x100 mask of that section to know the location) to become the average of the nearest n pixels which are not in that section. The mask of the section is different in each of the 100 images.
Thanks

Best Answer

If you have the Statistics Toolbox, you could use knnsearch,