图像的灰度变换matlab程序
时间: 2023-07-31 13:06:14 浏览: 99
以下是一个简单的图像灰度变换的 Matlab 程序:
```matlab
% 读取原始图像
img = imread('原始图像.jpg');
% 将图像转化为灰度图像
img_gray = rgb2gray(img);
% 进行灰度变换
img_transformed = imadjust(img_gray, [0.2, 0.8], []);
% 显示原始图像和变换后的图像
subplot(1, 2, 1);
imshow(img_gray);
title('原始图像');
subplot(1, 2, 2);
imshow(img_transformed);
title('灰度变换后的图像');
```
上述程序中,`imread` 函数用于读取原始图像,`rgb2gray` 函数将彩色图像转化为灰度图像,`imadjust` 函数进行灰度变换,`imshow` 函数用于显示图像。在进行灰度变换时,第二个参数 `[0.2, 0.8]` 表示将原始图像中灰度值在 0.2 到 0.8 之间的像素进行拉伸,以便更好地显示图像的细节。
注意:在运行程序前需要将程序中的文件路径 `'原始图像.jpg'` 修改为自己的图像路径。
相关问题
图像灰度变换 MATLAB实现
图像灰度变换是将图像的灰度级进行变换,从而改变图像的对比度、亮度等特性,常见的图像灰度变换包括线性变换、非线性变换等。
以下是Matlab实现图像灰度变换的示例代码:
1. 线性变换:
```matlab
% 读取图像
img = imread('lena.jpg');
% 线性变换,增强图像对比度
a = 1.5; % 增益因子
b = 50; % 亮度调整
img_new = a * img + b; % 公式:g(x,y) = a*f(x,y) + b
% 显示原始图像和变换后的图像
subplot(1,2,1);
imshow(img);
title('原始图像');
subplot(1,2,2);
imshow(img_new);
title('线性变换后的图像');
```
2. 非线性变换:
```matlab
% 读取图像
img = imread('lena.jpg');
% 对数变换,增强图像低灰度部分的细节
c = 50; % 常数
img_new = c * log(1 + double(img)); % 公式:g(x,y) = c * log(1 + f(x,y))
% 显示原始图像和变换后的图像
subplot(1,2,1);
imshow(img);
title('原始图像');
subplot(1,2,2);
imshow(uint8(img_new));
title('对数变换后的图像');
```
以上代码仅供参考,实际应用中可以根据具体需求进行参数调整和变换方法选择。
阅读全文
相关推荐











