Sliding window for image

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

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

Thank you. But whenever I do imshow(output) I get the whole image, not a window of it. Is there more code I need to add?
Of course you get the whole image. The window scans the whole image. What do you want? The window visits hundreds of thousands of locations as it moves over the original image. What subimage out of hundreds of thousands do you want to display? All of them? That would take hours or days to finish.
Ciara
Ciara on 18 Apr 2014
Edited: Ciara on 18 Apr 2014
Ah ok!
so If I use
kernel = ones(32)/32^2; % Create averaging window.
output = conv2(grayImage, kernel, 'same'); % Get means
mean=mean(mean(output);
display (mean);
should this return the mean value of every window?
Uh, yeah, sort of. But why do that when
meanGL = mean2(grayImage);
will get you the same thing without sliding a window along? The mean of the means is the same as the mean of the whole image.
Also, don't call mean and then set the answer to a variable called mean. You'll never be able to call mean again in that program because you just blew away the built-in function by redefining it to be a variable of the same name in your program.
because I need to find the mean intensity of the pixels in each window.
here is my algorithm:
split image into 32x32 windows
if average intensity within 32x32 window>210
split the 32x32 window into an 8x8 window
end
output is a collection of all your means. No need to take the mean after that.
I don't know what you're doing with this further breakdown into 4 8 by 8 quadrants within the 32 by 32 window. What's the purpose of that? That would require 4 means for every pixel location instead of just one. Why do that???
because I am using the following algorithm from this paper to find an ROI in breast profile image. http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3097782/figure/F7/
Can you help me with this? I am really struggling
It first does 32 wide blocks. Then 8 wide blocks. It does not do the 8 on the output of the 32. It does it on the original image. But I'm not sure because I don't know what "Combine image" means. You'll have to take more time to understand what the paper is doing. http://matlab.wikia.com/wiki/FAQ#Can_you_program_up_the_algorithm_in_this_article_for_me_and_explain_it_to_me.3F
How do I contact a consultant?
The Mathworks has consultants that they can connect you with. You can call them.
Hi, Image Analyst. For this case. Could I ask you if I'm not just calculating the mean of each 32x32 window but also the standard deviation?. How can I do that?
Kit, you can use stdfilt():
sdImage = stdfilt(grayImage, ones(32));
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)

You can use blockproc().
M=magic(10);
fun =@(MEANS) mean(MEANS.data(:));
blockproc(M,[2 2],fun)

Asked:

on 17 Apr 2014

Commented:

on 12 Mar 2017

Community Treasure Hunt

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

Start Hunting!