Reducing intensity in an image using a function

1 vue (au cours des 30 derniers jours)
PenguinForce
PenguinForce le 12 Oct 2016
Commenté : Image Analyst le 12 Oct 2016
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.

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Oct 2016
  2 commentaires
PenguinForce
PenguinForce le 12 Oct 2016
I'm trying to accomplish this without using built in functions like this

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 12 Oct 2016
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 commentaires
Walter Roberson
Walter Roberson le 12 Oct 2016
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 le 12 Oct 2016
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by