Reducing intensity in an image using a function

6 次查看(过去 30 天)
So I'm trying to use mean filtering to process an image. The idea of mean filtering is simply to replace each pixel value in an image with the mean (average) value of its neighbors, including itself.

采纳的回答

Walter Roberson
Walter Roberson 2016-10-12

更多回答(1 个)

Image Analyst
Image Analyst 2016-10-12
Try conv2:
windowSize = 3;
kernel = ones(windowSize)/windowSize^2;
filteredImage = conv2(double(grayImage), kernel, 'same');
imshow(filteredImage, []);
This will give an output image where each pixel is the mean of a sliding 3-by-3 window.
  2 个评论
Walter Roberson
Walter Roberson 2016-10-12
conv2 will not give the answer the user wants at the edges, as it will zero pad, but still divide by the windowSize^2. The user wants the edges to only be divided by the number of pixels that are in-bounds.
You can run a correction afterwards -- multiply the edge values by windowSize^2 and then divide by the number of valid neighbours, which would be 6 everywhere except the corners and would be 4 there.
Image Analyst
Image Analyst 2016-10-12
Yes, there are boundary/edge effects with filters and there are different ways of handling them. imfilter() is like conv2() but offers more boundary handling options. Unfortunately none of them see to be the "shrinking window" effect like Walter mentioned. For a 3x3 filter (nearest 8 neighbors), only the outermost perimeter layer of edge pixels is affected. So if the outer 1-pixel-wide boundaries are important to you, and you want that "shrinking window" option, you'll have to do like Walter explained. It is possible to do it with nlfilter() or blockproc() but those won't be nearly as fast as conv2() or imfilter() which are highly optimized, so to just fix up one pixel, I wouldn't bother with those.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by