clear all; close all;
I = imread('cameraman.tif');
I = im2double(I);
I = imnoise(I, 'salt & pepper', 0.01);
J = ordfilt2(I, 1, ones(4,4));
K = ordfilt2(I, 9, ones(3));
figure;
subplot(131);
imshow(I);
title('Original Image with Salt and Pepper Noise');
subplot(132);
imshow(J);
title('Image with Order Filtered (Rank 1)');
subplot(133);
imshow(K);
title('Image with Order Filtered (Rank 9)');
解释:
-
clear all; close all;
:清空工作空间中的所有变量并关闭所有图形窗口。 -
I = imread('cameraman.tif');
:读取名为cameraman.tif
的图像,并赋值给I
。 -
I = im2double(I);
:将图像I
的数据类型转换为双精度浮点数,以便进行后续处理。 -
I = imnoise(I, 'salt & pepper', 0.01);
:在图像I
上添加椒盐噪声,噪声密度为0.01。 -
J = ordfilt2(I, 1, ones(4,4));
:使用ordfilt2
函数对图像I
进行排序滤波,滤波的顺序数为1(最小值滤波),邻域窗口为4x4的均匀矩阵。 -
K = ord