How to apply average filter on image ?
이전 댓글 표시
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');
답변 (2개)
Image Analyst
2019년 3월 14일
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);

댓글 수: 4
khaikheat kheat
2020년 4월 18일
windowWidth = 21;
kernel = ones(windowWidth) / windowWidth .^ 2;
May you explain these 2 lines? Im new in matalb. Thanks
khaikheat kheat
2020년 4월 18일

When I change the value in the windowWidth, 23,25 adn 45 respectively. It coming out different filtered image. Can you help me with it?
Image Analyst
2020년 4월 18일
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
2020년 4월 19일
OK. I will create a new question
David Fernandes
2019년 11월 19일
편집: David Fernandes
2019년 11월 19일
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
Image Analyst
2019년 11월 19일
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
2019년 11월 19일
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.
카테고리
도움말 센터 및 File Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!