How to apply average filter on image ?

174 views (last 30 days)
Hanif Ullah
Hanif Ullah on 14 Mar 2019
Commented: khaikheat kheat on 19 Apr 2020
I m working on image to apply average filter on it. It gives perfect result on array of matrix but not working on real image here is my code. what I m doing wrong ?
S = imread('15.jpg');
I = rgb2gray(S);
% I = [100 255 94 30 150; 53 176 255 198 140; 200 113 118 255 250; 100 255 150 160 12; 255 255 125 152 53];
I2 = padarray(I,[1 1],'replicate','both');
[x,y] = size(I2);
for i = 2:x-1
for j = 2:y-1
sum = 0;
for ii = i-1:i+1
for jj = j-1:j+1
sum = sum + I2(ii,jj);
end
end
I3(i,j) = floor(sum/9);
end
end
subplot(1,3,1);
imshow(I)
title('Original Image');
subplot(1,3,2);
imshow(I2)
title('1 Row & Column added to Image');
subplot(1,3,3);
imshow(I3)
title('Averge Filter Image');

Answers (2)

Image Analyst
Image Analyst on 14 Mar 2019
sum is a built-in function so don't use it as one of your variable names.
Use the built-in imfilter(), or conv2().
rgbImage = imread('peppers.png');
windowWidth = 21;
kernel = ones(windowWidth) / windowWidth .^ 2;
subplot(2, 1, 1);
imshow(rgbImage);
drawnow;
blurryImage = imfilter(rgbImage, kernel, 'replicate');
subplot(2, 1, 2);
imshow(blurryImage);
0000 Screenshot.png
  4 Comments
Image Analyst
Image Analyst on 18 Apr 2020
Of course, as it should. And we can help if you explain your difficulties in a new question (not here in a comment to an Answer to Hanif's question).
khaikheat kheat
khaikheat kheat on 19 Apr 2020
OK. I will create a new question

Sign in to comment.


David Fernandes
David Fernandes on 19 Nov 2019
Edited: David Fernandes on 19 Nov 2019
Although @Image Analyst solution is correct, in fact, it does not answer to your question. And I commend you for trying to understand the algorithm and not only use some built in function that hides it from you (at least at first). :)
So, maybe a bit too late here goes my 50 cents. :)
The problem is that by default when you say: sum = 0, sum becomes a UINT8 which only accepts integers up to 255. Adding 9 UINT8 values will, almost for sure, overflow 255 and 255/9 is almost black.
So, you have to force a "bigger" type.
For instance:
for i = 2:x-1
for j = 2:y-1
sum = uint16(0); % change here
for ii = i-1:i+1
for jj = j-1:j+1
sum = sum + uint16(I(ii,jj)); % and here
end
end
I2(i,j) = floor(sum/9);
end
end
  2 Comments
Image Analyst
Image Analyst on 19 Nov 2019
Since sum is a built-in function, you should call it "theSum" so that you do not overwrite the built-in sum() function.
Also you seem to have non-standard nomenclature where you have i, the first/row index, be the x direction, and j, the second/column index, go up to "y-1". Remember, images are addressed as (row, column), not as (x,y) so you should say I2(j, i) and I(jj, ii) if you're using the usual convention of x being the horizontal/columns direction, and y being the vertical/rows direction.
But other than that, it should work for a gray scale image.
David Fernandes
David Fernandes on 19 Nov 2019
Well, you're right, thank you for your comments. Although my only concern was to try to explain why the resulting image was not as expected and to make the minimum changes to the original code in order to fix it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!