中值滤波图像matlab
时间: 2023-10-10 18:10:26 浏览: 76
中值滤波是一种基于排序的非线性滤波方法,用于去除图像中的噪声。Matlab中可以使用medfilt2函数进行二维中值滤波。
以下是一个简单的示例代码,演示如何使用medfilt2函数进行中值滤波。
```matlab
% 读取图像
Im = imread('lena.png');
% 添加椒盐噪声
Im_noisy = imnoise(Im, 'salt & pepper', 0.02);
% 中值滤波
Im_med = medfilt2(Im_noisy);
% 显示图像
subplot(1,3,1), imshow(Im), title('原图');
subplot(1,3,2), imshow(Im_noisy), title('加噪图');
subplot(1,3,3), imshow(Im_med), title('中值滤波结果');
```
在上面的代码中,我们首先读取了一张图像,并使用imnoise函数添加了椒盐噪声。然后使用medfilt2函数对噪声图像进行中值滤波,最后将原图、加噪图和中值滤波结果显示在一个窗口中。
注意,medfilt2函数的第二个参数可以指定滤波器的大小,例如medfilt2(Im_noisy, [3 3])表示使用一个3x3的滤波器进行中值滤波。滤波器的大小越大,滤波效果越好,但计算时间也会增加。
相关问题
中值滤波matlab图像
中值滤波可以用来消除图像中的椒盐噪声。在MATLAB中,可以使用medfilt2函数来实现中值滤波。
下面是一个实例:
```Matlab
% 读取图像
img = imread('lena.png');
% 加入椒盐噪声
img_noise = imnoise(img,'salt & pepper',0.02);
% 中值滤波
img_med = medfilt2(img_noise,[3 3]);
% 显示图像
subplot(1,3,1), imshow(img), title('原图像');
subplot(1,3,2), imshow(img_noise), title('加噪声后的图像');
subplot(1,3,3), imshow(img_med), title('中值滤波后的图像');
```
上述代码中,首先读取了一张图像,然后使用imnoise函数加入了椒盐噪声,接着使用medfilt2函数进行中值滤波,最后将原图像、加噪声后的图像和中值滤波后的图像显示在同一个窗口中。
matlab中值滤波图像处理
在 MATLAB 中,可以使用 `medfilt2` 函数来对图像进行中值滤波处理。该函数的语法为:
```
B = medfilt2(A)
```
其中,`A` 表示输入的图像,`B` 表示输出的图像。该函数默认使用 3x3 的窗口进行中值滤波处理,也可以通过指定第二个参数来使用不同大小的窗口。例如,使用一个 5x5 的窗口:
```
B = medfilt2(A, [5 5])
```
下面是一个简单的例子,演示如何使用 `medfilt2` 函数对图像进行中值滤波处理:
```matlab
% 读入图像
I = imread('lena.png');
% 添加高斯噪声
J = imnoise(I, 'gaussian', 0, 0.01);
% 中值滤波处理
K = medfilt2(J, [5 5]);
% 显示结果
subplot(1, 3, 1);
imshow(I);
title('原始图像');
subplot(1, 3, 2);
imshow(J);
title('加噪图像');
subplot(1, 3, 3);
imshow(K);
title('中值滤波图像');
```
在这个例子中,我们首先读入了一张 Lena 图像,然后使用 `imnoise` 函数添加了一些高斯噪声。接着,我们使用 `medfilt2` 函数对噪声图像进行了中值滤波处理,并将结果与原始图像和噪声图像一起显示出来。
阅读全文
相关推荐













