算术均值滤波matlab代码实现
时间: 2023-10-24 20:09:04 浏览: 54
以下是算术均值滤波的 Matlab 代码实现:
```matlab
% 输入待滤波的灰度图像
img = double(rgb2gray(imread('image.png')));
% 设置均值滤波的窗口大小(奇数)
win_size = 5;
% 进行算术均值滤波
filtered_img = zeros(size(img));
for i = 1:size(img, 1)
for j = 1:size(img, 2)
% 对于边缘像素,不做处理
if i < ceil(win_size/2) || i > size(img, 1) - floor(win_size/2) || ...
j < ceil(win_size/2) || j > size(img, 2) - floor(win_size/2)
filtered_img(i, j) = img(i, j);
else
window = img(i-floor(win_size/2):i+floor(win_size/2), ...
j-floor(win_size/2):j+floor(win_size/2));
filtered_img(i, j) = mean(window(:));
end
end
end
% 显示滤波前后的图像
figure, imshow(uint8(img)), title('Original image')
figure, imshow(uint8(filtered_img)), title('Filtered image')
```
该代码实现了一个大小为 $5 \times 5$ 的算术均值滤波器,可自行调整窗口大小。
阅读全文