MATLAB对加入噪声图像选用最大均匀性平滑模板做运算代码
时间: 2024-02-13 20:06:30 浏览: 32
以下是MATLAB中对加入噪声图像选用最大均匀性平滑模板做运算的代码:
```matlab
% 读入图像
I = imread('noisy_image.png');
% 添加噪声
J = imnoise(I, 'salt & pepper', 0.02);
% 定义最大均匀性平滑模板
mask = ones(5, 5);
% 对图像进行卷积运算
K = conv2(J, mask, 'same');
% 显示结果
imshow(K);
```
解释一下代码:
第一行读入图像,需要将`noisy_image.png`替换成你的图片路径和名称。
第三行添加噪声,这里我选用的是椒盐噪声,噪声强度为0.02,可以根据需要更改噪声类型和强度。
第五行定义最大均匀性平滑模板,这里选用的是5*5的全1矩阵,表示对每个像素周围的5*5个像素取平均值。
第七行对图像进行卷积运算,这里选用的是`conv2`函数,第三个参数`'same'`表示输出与输入图像大小相同的图像。卷积运算的结果是对每个像素进行最大均匀性平滑。
最后一行显示结果。
希望对你有所帮助!
相关问题
用Matlab对噪声图像先进行闭运算再进行开运算的代码示例
以下是Matlab代码示例,用于对噪声图像进行闭运算再进行开运算:
```matlab
% 读取噪声图像
img = imread('noisy_image.png');
% 定义闭运算和开运算的结构元素
se1 = strel('disk', 3); % 闭运算结构元素
se2 = strel('disk', 5); % 开运算结构元素
% 进行闭运算
img_closed = imclose(img, se1);
% 进行开运算
img_opened = imopen(img_closed, se2);
% 显示原始图像、闭运算图像和开运算图像
subplot(1,3,1), imshow(img), title('Noisy Image');
subplot(1,3,2), imshow(img_closed), title('Closed Image');
subplot(1,3,3), imshow(img_opened), title('Opened Image');
```
以上代码中,我们使用了 `imclose()` 函数进行闭运算,将噪声图像中的小孔和突出部分填平。然后,我们使用 `imopen()` 函数进行开运算,进一步平滑图像并去除细小的噪点。最后,我们使用 `subplot()` 函数将原始图像、闭运算图像和开运算图像显示在同一幅图像中,以便比较它们的效果。
matlab图像空域平滑 1.读出一幅图像,给这幅图像分别加入椒盐噪声和高斯噪声后并与前一张图像显示在同一图像窗口中。 2.对加入噪声图像选用不同的平滑模板(3×3邻域模板、5×5邻域模板、超限像素平滑法、最大均匀性平滑、中值滤波等)做运算,对比不同模板所输出的效果。 图像空域锐化 1.读出一幅图像,利用sobel和prewitt算子对图像进行锐化,观察采用不同的输出形式,获得的图像锐化处理效果。 2.分别采用5×5,9×9大小的二阶Laplacian算子1中原始图像进行锐化,比较处理效果,要求在同一窗口中显示。
图像空域平滑:
1.读出一幅图像,给这幅图像分别加入椒盐噪声和高斯噪声后并与前一张图像显示在同一图像窗口中。
```matlab
img = imread('lena.png');
gray_img = rgb2gray(img);
% 添加椒盐噪声
salt_pepper_img = imnoise(gray_img, 'salt & pepper', 0.05);
% 添加高斯噪声
gaussian_img = imnoise(gray_img, 'gaussian', 0.02);
% 显示图像
figure;
subplot(1,3,1);
imshow(gray_img);
title('Original Image');
subplot(1,3,2);
imshow(salt_pepper_img);
title('Salt & Pepper Noise Image');
subplot(1,3,3);
imshow(gaussian_img);
title('Gaussian Noise Image');
```
2.对加入噪声图像选用不同的平滑模板(3×3邻域模板、5×5邻域模板、超限像素平滑法、最大均匀性平滑、中值滤波等)做运算,对比不同模板所输出的效果。
```matlab
% 3x3邻域平均滤波
avg_3x3 = imfilter(salt_pepper_img, fspecial('average', [3 3]));
% 5x5邻域平均滤波
avg_5x5 = imfilter(salt_pepper_img, fspecial('average', [5 5]));
% 超限像素平滑法
lim_3x3 = adpmedian(salt_pepper_img, 3);
% 最大均匀性平滑
max_3x3 = ordfilt2(salt_pepper_img, 5, ones(3,3));
% 中值滤波
med_3x3 = medfilt2(salt_pepper_img, [3 3]);
% 显示图像
figure;
subplot(2,3,1);
imshow(salt_pepper_img);
title('Salt & Pepper Noise Image');
subplot(2,3,2);
imshow(avg_3x3);
title('3x3 Avg Filter');
subplot(2,3,3);
imshow(avg_5x5);
title('5x5 Avg Filter');
subplot(2,3,4);
imshow(lim_3x3);
title('Adaptive Median Filter');
subplot(2,3,5);
imshow(max_3x3);
title('Max Uniformity Filter');
subplot(2,3,6);
imshow(med_3x3);
title('Median Filter');
```
图像空域锐化:
1.读出一幅图像,利用sobel和prewitt算子对图像进行锐化,观察采用不同的输出形式,获得的图像锐化处理效果。
```matlab
img = imread('lena.png');
gray_img = rgb2gray(img);
% Sobel算子
sobel_img = imfilter(gray_img, fspecial('sobel'));
sobel_img1 = imabsdiff(gray_img, sobel_img);
sobel_img2 = imadd(gray_img, sobel_img);
% Prewitt算子
prewitt_img = imfilter(gray_img, fspecial('prewitt'));
prewitt_img1 = imabsdiff(gray_img, prewitt_img);
prewitt_img2 = imadd(gray_img, prewitt_img);
% 显示图像
figure;
subplot(2,3,1);
imshow(gray_img);
title('Original Image');
subplot(2,3,2);
imshow(sobel_img);
title('Sobel Operator');
subplot(2,3,3);
imshow(prewitt_img);
title('Prewitt Operator');
subplot(2,3,4);
imshow(sobel_img1);
title('Sobel Operator (abs diff)');
subplot(2,3,5);
imshow(sobel_img2);
title('Sobel Operator (add)');
subplot(2,3,6);
imshow(prewitt_img1);
title('Prewitt Operator (abs diff)');
```
2.分别采用5×5,9×9大小的二阶Laplacian算子1中原始图像进行锐化,比较处理效果,要求在同一窗口中显示。
```matlab
% 5x5二阶Laplacian算子
laplacian_5 = fspecial('laplacian', 0.2);
laplacian_img_5 = imfilter(gray_img, laplacian_5);
% 9x9二阶Laplacian算子
laplacian_9 = fspecial('laplacian', 0.8);
laplacian_img_9 = imfilter(gray_img, laplacian_9);
% 显示图像
figure;
subplot(1,3,1);
imshow(gray_img);
title('Original Image');
subplot(1,3,2);
imshow(laplacian_img_5);
title('5x5 Laplacian Operator');
subplot(1,3,3);
imshow(laplacian_img_9);
title('9x9 Laplacian Operator');
```