MATLAB3x3算术均值滤波器
时间: 2023-06-10 21:04:45 浏览: 829
MATLAB中3x3算术均值滤波器的实现代码如下:
```
% 读取图像,添加高斯噪声
img = imread('lena.jpg');
noisy_img = imnoise(img,'gaussian',0,0.02);
% 均值滤波器
h = fspecial('average', [3 3]);
mean_filtered = imfilter(noisy_img, h);
% 显示结果
figure;
subplot(1,3,1);
imshow(img);
title('原图');
subplot(1,3,2);
imshow(noisy_img);
title('加噪声的图像');
subplot(1,3,3);
imshow(mean_filtered);
title('均值滤波器去噪后的图像');
```
这段代码中,首先读取了一张图像,并添加了高斯噪声。然后利用`fspecial`函数创建了一个大小为3x3的算术均值滤波器`h`。最后用`imfilter`函数将噪声图像和滤波器作为参数输入,得到了经过均值滤波器去噪后的图像。
相关问题
用imfilter函数构造算术均值滤波器(3*3)尝试复原图像
在MATLAB中,要使用`imfilter`函数构建一个3x3的算术平均滤波器来复原图像,首先要理解算术平均滤波器是一种平滑操作,通过每个像素周围邻域像素的简单相加然后除以邻居数量来进行平均。以下是一个简单的步骤:
1. **初始化滤波器矩阵**:对于3x3算术平均滤波器,你需要创建一个权重矩阵,所有元素都是1,因为算术平均就是每个像素值的一次简单求和,然后除以9(3x3的总像素数)。这个矩阵通常被称为`weights`:
```matlab
weights = ones(3) / 9;
```
2. **准备图像数据**:假设你的图像存储在变量`input_image`中,先复制一份以防止原图被修改:
```matlab
input_copy = input_image;
```
3. **应用滤波器**:使用`imfilter`函数,传入`input_copy`作为输入图像,`weights`作为滤波系数:
```matlab
smoothed_img = imfilter(input_copy, weights, 'replicate');
```
`'replicate'`选项用于边界条件处理,这里使用了镜像填充(边缘像素重复)。
4. **检查复原效果**:
```matlab
figure;
subplot(1,2,1), imshow(input_image); % 显示原图
title('Original Image');
subplot(1,2,2), imshow(smoothed_img); % 显示处理后的图像
title('Filtered (Mean) Image');
```
5. **保存结果**:
要保存复原后的图像,可以参照之前的保存过程:
```matlab
save_path = uiputfile('*.jpg', 'Save Smoothed Image As');
imwrite(smoothed_img, save_path);
```
注意:算术平均滤波可能会导致锐利度降低,适用于去除噪声,如果想要更复杂的复原效果,可能需要考虑其他滤波器(如高斯滤波)或者更高级的技术(如去噪算法)。
高斯噪声的消除用imread函数读取图像 用imnoise函数给图像加上高斯噪声并显示加噪图像 用imfilter函数构造算术均值滤波器(3*3)、几何均值滤波器(3*3)、逆谐波滤波器(Q=3 Q=-3)分别复原图像的源代码
在Matlab中,为了消除高斯噪声,你可以按照以下步骤操作:
首先,你需要使用`imread`函数读取图像文件:
```matlab
img = imread('your_image_file.jpg'); % 替换为你要处理的图片文件名
```
然后,使用`imnoise`函数添加高斯噪声到图像上,并显示加噪后的图像:
```matlab
noisy_img = imnoise(img, 'gaussian', 0); % 0表示标准差,可以调整噪声程度
figure;
imshow(noisy_img);
title('Noisy Image');
```
接着,利用`imfilter`函数构造不同类型的滤波器对图像进行平滑处理,去除噪声:
1. 算术均值滤波器(3x3):
```matlab
kernel = ones(3); % 创建一个3x3的平均滤波核
filtered_img_arithmetic = imfilter(noisy_img, kernel, 'replicate'); % 使用replicate边界处理
```
2. 几何均值滤波器(3x3):
```matlab
kernel = sqrt(ones(3)); % 创建一个3x3的几何平均滤波核,注意sqrt是元素级别的运算
filtered_img_geometric = imfilter(noisy_img, kernel, 'replicate');
```
3. 逆谐波滤波器(Q=3, Q=-3):
```matlab
Q = [3 -3]; % 生成一个3x3的Q矩阵,用于构建逆谐波滤波器
kernel = conv2(Q.^2, eye(3), 'same'); % 计算滤波核
filtered_img_harmonic = imfilter(noisy_img, kernel, 'replicate');
```
最后,展示去噪后的结果:
```matlab
subplot(2, 2, 1);
imshow(filtered_img_arithmetic);
title('Arithmetic Mean Filtered');
subplot(2, 2, 2);
imshow(filtered_img_geometric);
title('Geometric Mean Filtered');
subplot(2, 2, 3);
imshow(filtered_img_harmonic);
title('Harmonic Inverse Filtered');
% 可以对比原始图像和去噪后的图像
subplot(2, 2, 4);
imshow(img);
title('Original Image');
```
请注意替换`'your_image_file.jpg'`为你实际的图片文件路径。
阅读全文
相关推荐














