# Average filter in Matlab

Page 1 of 1

## 1 Replies - 29406 Views - Last Post: 12 August 2012 - 10:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288517&amp;s=34f04d570b1dea1a616fafaf6337b1ce&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bakalaka

Reputation: 0
• Posts: 1
• Joined: 10-August 12

# Average filter in Matlab

Posted 10 August 2012 - 12:08 PM

Hi everyone.
I'm trying to split a greyscale image (that has 'Salt and pepper noise') into 3x3 blocks, then take the average of each block
and scale the original image with its respective block. The result should remove most of the noise and smoothen the image.

For now this is just testing a single block on the image. The average is too large, so all pixels turn white.
I do not know how to apply this average in the correct way. Any ideas would be greatly appreciated.

[r,c] = size(A);

n = 3;
M = zeros(n,n);
m = [];

for i = 1:n
for j = 1:n
m = [m;A(i,j)];
end
end
m = sum(m)/n*n;
M(:,:)/> = m;

for i = 1:n
for j = 1:n
A(i,j) = M(i,j)*A(i,j);
end
end

imshow(A)

Is This A Good Question/Topic? 0

## Replies To: Average filter in Matlab

### #2 mufasa

Reputation: 2
• Posts: 44
• Joined: 12-February 10

## Re: Average filter in Matlab

Posted 12 August 2012 - 10:07 AM

If you want to use a filter to remove some noise form an image Matlab already has a lot of these functions built in. For a mean filter (what it sounds like you're looking for) you can just do the following:

meanFilter = fspecial('average', [3 3]);
toShow = imfilter(A, meanFilter);
imshow(toShow);

Is that somewhat what you were looking for? You can check out this link for other filters: filters.

Hope that helps!