Sliding window for image

39 views (last 30 days)
Ciara
Ciara on 17 Apr 2014
Commented: Marium Azhar on 12 Mar 2017
Hi can anyone show me how to use a 32x32 sliding window on an image using and extract the mean from each window?
Thanks

Accepted Answer

Image Analyst
Image Analyst on 18 Apr 2014
You can use conv2() or imfilter() to slide a 32 by 32 window across the image by one pixel at a time and get the mean. Nearly always an odd size (31 or 33) is used because then there are the same number of pixels to the left and right - the window is centered over the pixel. With an even number, the output and input images are shifted by a half pixel. Here's the code:
kernel = ones(32)/32^2; % Create averaging window.
output = conv2(grayImage, kernel, 'same'); % Get means.
If you want to move over in jumps of 32 pixels instead of 1 pixel, see my demos for blockproc (attached).
  13 Comments
Image Analyst
Image Analyst on 11 Dec 2015
Kit, you can use stdfilt():
sdImage = stdfilt(grayImage, ones(32));
Marium Azhar
Marium Azhar on 12 Mar 2017
if i want to apply some other operation instead of mean std deviation etc. i mean if i just want to run a 5x5 sliding window over the whole image? then?

Sign in to comment.

More Answers (1)

Joseph Cheng
Joseph Cheng on 17 Apr 2014
You can use blockproc().
M=magic(10);
fun =@(MEANS) mean(MEANS.data(:));
blockproc(M,[2 2],fun)

Community Treasure Hunt

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

Start Hunting!