matlab图像直方图显示和均衡代码是啥
时间: 2024-05-13 10:14:44 浏览: 89
以下是Matlab中图像直方图显示和均衡的示例代码:
图像直方图显示:
```matlab
img = imread('image.jpg'); % 读取图像
figure; % 新建一个窗口
imhist(img); % 显示图像直方图
```
图像直方图均衡:
```matlab
img = imread('image.jpg'); % 读取图像
img_eq = histeq(img); % 直方图均衡化
figure; % 新建一个窗口
subplot(1,2,1);imshow(img);title('原始图像'); % 显示原始图像
subplot(1,2,2);imshow(img_eq);title('均衡后的图像'); % 显示均衡后的图像
```
其中,`imread`函数用于读取图像,`imhist`函数用于显示图像直方图,`histeq`函数用于进行直方图均衡化,`imshow`函数用于显示图像,`title`函数用于添加图像标题。在直方图均衡的示例代码中,`subplot`函数用于将两幅图像显示在同一个窗口中。
相关问题
matlab图像直方图均衡化代码
### MATLAB 实现图像直方图均衡化
MATLAB 提供了内置函数 `histeq` 来执行直方图均衡化操作。下面是一个完整的代码示例,展示了如何读取一张图片并对其进行直方图均衡化处理:
```matlab
% 读取原始图像
Iin = imread('tire.tif');
% 应用直方图均衡化
Iout = histeq(Iin);
% 显示对比结果
figure;
imshowpair(Iin, Iout, 'montage');
title('左:原图 右:处理后的图像')
```
这段代码首先通过 `imread` 函数加载名为 `'tire.tif'` 的图像文件[^1]。接着调用了 `histeq` 函数来对该图像应用直方图均衡化变换[^1]。最后利用 `imshowpair` 和 `montage` 参数在一个窗口内同时展示原始图像及其经过直方图均衡化之后的效果。
为了更深入理解整个过程以及查看具体的变化情况,还可以绘制两幅图像各自的灰度分布直方图来进行比较分析:
```matlab
% 创建一个新的图形窗口用于显示直方图
figure;
subplot(2,2,[1 3]);
imhist(Iin);
title('原图的灰度直方图');
subplot(2,2,[2 4]);
imhist(Iout);
title('处理后图像的灰度直方图');
```
此部分扩展了之前的例子,在新的子图布局中分别呈现了输入和输出图像对应的灰度级频率分布曲线,有助于直观感受直方图均衡化带来的变化。
图像直方图均衡化matlab代码
图像直方图均衡化是一种用来改善图像对比度的处理方法,它通过对图像的像素值进行变换,使得像素值的分布更加均匀,从而增强图像的细节和色彩。下面是一份基于Matlab的图像直方图均衡化代码示例:
1. 读取图像
```matlab
img = imread('image.jpg'); % 读取图像
```
2. 将图像转换为灰度图
```matlab
gray_img = rgb2gray(img); % 转换为灰度图
```
3. 计算图像的直方图
```matlab
histogram = imhist(gray_img); % 计算直方图
```
4. 计算直方图累积分布
```matlab
cumulative_histogram = cumsum(histogram); % 计算累积直方图
normalized_histogram = cumulative_histogram / numel(gray_img); % 归一化
```
5. 对图像的像素值进行映射
```matlab
new_img = normalized_histogram(gray_img + 1); % 映射像素值
```
6. 显示原始图像和均衡化后的图像
```matlab
subplot(1, 2, 1); % 创建子图
imshow(gray_img); % 显示原始图像
title('原始图像');
subplot(1, 2, 2); % 创建子图
imshow(new_img); % 显示均衡化后的图像
title('均衡化后的图像');
```
通过以上代码,我们可以实现对图像的直方图均衡化处理。代码中通过imread函数读取图像,然后转换为灰度图,计算图像的直方图并计算累积直方图,最后对图像的像素值进行映射。最终通过subplot和imshow函数将原始图像和均衡化后的图像显示出来。这样,我们就可以直观地观察到均衡化处理对图像的效果改善。
阅读全文
相关推荐














