我编写了使用3×3平均滤波器来平滑图像的代码,但输出很奇怪,它几乎都是黑色的.这是我的代码.
function [filtered_img] = average_filter(noisy_img)
[m,n] = size(noisy_img);
filtered_img = zeros(m,n);
for i = 1:m-2
for j = 1:n-2
sum = 0;
for k = i:i+2
for l = j:j+2
sum = sum+noisy_img(k,l);
end
end
filtered_img(i+1,j+1) = sum/9.0;
end
end
end
我把这个函数调用如下:
img=imread('img.bmp');
filtered = average_filter(img);
imshow(uint8(filtered));
到目前为止,我在代码逻辑中看不到任何错误,如果有人能够发现问题,我会很感激.