MATLAB图像处理程序40行
时间: 2023-10-09 17:10:11 浏览: 40
以下是一个简单的 MATLAB 图像处理程序,用于读取图像、转换为灰度图像、应用均值滤波器和显示结果。代码共计 40 行。
```matlab
% 读取图像
image = imread('example.jpg');
% 转换为灰度图像
gray_image = rgb2gray(image);
% 应用均值滤波器
filter_size = 3;
kernel = ones(filter_size) / filter_size^2;
blurred_image = conv2(double(gray_image), kernel, 'same');
% 显示结果
subplot(1, 2, 1);
imshow(gray_image);
title('原图像');
subplot(1, 2, 2);
imshow(uint8(blurred_image));
title('均值滤波结果');
```
这段代码首先使用 `imread` 函数读取名为 `example.jpg` 的图像。然后使用 `rgb2gray` 函数将图像转换为灰度图像。接下来,定义了一个 `filter_size` 变量,指定了均值滤波器的大小为 $3\times3$。然后创建了一个 $3\times3$ 的矩阵,其中每个元素都是 1,用于构建均值滤波器。使用 `conv2` 函数将均值滤波器应用于灰度图像,并将结果保存在 `blurred_image` 变量中。最后,使用 `imshow` 函数显示原始图像和均值滤波结果。注意,要将 `blurred_image` 转换为 `uint8` 类型,以便在图像上正确显示灰度级。