8x8 window and finding mean

Say I have a matrix of an image, and I want to do the following:
- Slide an `8x8` window over the matrix - Calculate the mean for each pixel in the matrix
How can I do that in `matlab`, provided that I'm kind of new to coding in matlab.
Thanks.

답변 (2개)

Walter Roberson
Walter Roberson 2013년 1월 22일
편집: Walter Roberson 2013년 1월 22일

0 개 추천

See blockproc() or nlfilter()

댓글 수: 2

med-sweng
med-sweng 2013년 1월 22일
Thanks for your reply. I'm tinking of sliding a window over the pixels. Will the functions you suggested do the work?
Walter Roberson
Walter Roberson 2013년 1월 22일
You would probably find nlfilter() easier to use for this task, but it does have the restriction that the image must be grayscale (well, really, that the array must be 2D rather than RGB which is 3D). It is, though, not immediately clear what "mean" of pixels means for RGB.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2013년 1월 23일

0 개 추천

You can do
meanImage = conv2(grayImage, ones(8)/64, 'same');
Do you realize though that if the window size is not odd, your window does not have the same number of pixels on each side of the "center" pixel. So one pixel will be aligned and you'll have three to one side, and four on the other size. So you have a half pixel shift. For that reason, window sizes are almost always odd, say, 9, so you'd have 4 pixels, then the central one, then 4 more on the other side - perfectly symmetrical. This will get the mean in the window for every pixel, like you said. If you use blockproc(), that moves in "jumps" of the window size and doesn't slide over a pixel at a time.

댓글 수: 2

Walter Roberson
Walter Roberson 2013년 1월 23일
blockproc() has a Margins option that effectively allows sliding windows.
Image Analyst
Image Analyst 2013년 1월 23일
I thought it did but didn't see it immediately. But upon further inspection, it looks like you're right, but it seems to be called 'BorderSize' (at least in R2012b) and it looks like if you set a block size of 1 and a "borderSize" of half the window width that you want, you can get the window of whatever size you want to move by just one pixel.

댓글을 달려면 로그인하십시오.

질문:

2013년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by