用matlab一页写出可以运行的用谐波均值滤波消除lena图片高斯噪声的代码并且每行写上注释
时间: 2023-11-22 20:51:51 浏览: 76
以下是可以运行的用谐波均值滤波消除lena图片高斯噪声的Matlab代码:
```matlab
% 读入lena图片并显示
lena = imread('lena.jpg');
figure, imshow(lena);
% 添加高斯噪声
lena_gaussian = imnoise(lena, 'gaussian', 0, 0.01);
figure, imshow(lena_gaussian);
% 定义谐波均值滤波器的半径r和q值
r = 3;
q = 1.5;
% 对lena图片进行谐波均值滤波
lena_harmonic = zeros(size(lena));
for i = 1:size(lena, 1)
for j = 1:size(lena, 2)
% 计算当前像素点的谐波均值
win = lena_gaussian(max(i-r,1):min(i+r,size(lena,1)), max(j-r,1):min(j+r,size(lena,2)));
win_size = numel(win);
if win_size > 0
win_inv = 1 ./ double(win);
win_sum = sum(win_inv(:));
harmonic_mean = win_size / win_sum;
lena_harmonic(i,j) = harmonic_mean ^ q;
end
end
end
% 显示谐波均值滤波后的lena图片
figure, imshow(uint8(lena_harmonic));
```
首先,我们读入lena图片并展示:
```matlab
lena = imread('lena.jpg');
figure, imshow(lena);
```
然后,我们添加高斯噪声并展示:
```matlab
lena_gaussian = imnoise(lena, 'gaussian', 0, 0.01);
figure, imshow(lena_gaussian);
```
接下来,我们定义谐波均值滤波器的半径r和q值:
```matlab
r = 3;
q = 1.5;
```
然后,我们对lena图片进行谐波均值滤波:
```matlab
lena_harmonic = zeros(size(lena));
for i = 1:size(lena, 1)
for j = 1:size(lena, 2)
% 计算当前像素点的谐波均值
win = lena_gaussian(max(i-r,1):min(i+r,size(lena,1)), max(j-r,1):min(j+r,size(lena,2)));
win_size = numel(win);
if win_size > 0
win_inv = 1 ./ double(win);
win_sum = sum(win_inv(:));
harmonic_mean = win_size / win_sum;
lena_harmonic(i,j) = harmonic_mean ^ q;
end
end
end
```
在循环中,我们首先计算当前像素点的谐波均值:我们提取以当前像素点为中心的一个大小为(2r+1)x(2r+1)的窗口,计算窗口内像素值的倒数之和win_sum,然后用窗口内像素点的总数除以win_sum,即可得到谐波均值harmonic_mean。最后,我们将谐波均值的q次方作为当前像素点的值。
最后,我们展示谐波均值滤波后的lena图片:
```matlab
figure, imshow(uint8(lena_harmonic));
```
阅读全文