对比度matlab代码
时间: 2023-11-25 07:10:04 浏览: 121
以下是一段简单的 MATLAB 代码,用于调整图像的对比度:
```matlab
% 读取图像
img = imread('example.jpg');
% 设置对比度调整倍数
contrast = 1.5;
% 计算图像的均值和标准差
img_mean = mean2(img);
img_std = std2(img);
% 计算新的图像像素值
new_img = (img - img_mean) * contrast * (img_std / 255) + img_mean;
% 显示原图和新图
subplot(1,2,1);
imshow(img);
title('原图');
subplot(1,2,2);
imshow(new_img);
title(['对比度调整后的图像(倍数:' num2str(contrast) ')']);
```
该代码包括以下步骤:
1. 读取图像。
2. 设置对比度调整倍数。
3. 计算图像的均值和标准差。
4. 计算新的图像像素值。
5. 显示原图和新图。
在上述代码中,`contrast` 变量控制对比度的调整倍数。值越大,对比度调整的幅度就越大。`img_mean` 和 `img_std` 变量计算图像的均值和标准差,用于标准化像素值。新的像素值计算公式为:
```
new_pixel = (old_pixel - img_mean) * contrast * (img_std / 255) + img_mean
```
其中 `old_pixel` 是原始图像中的像素值,`new_pixel` 是对比度调整后的像素值。
最后,代码通过 `imshow` 函数显示原始图像和调整后的图像,并在标题中显示对比度调整的倍数。
阅读全文
相关推荐















