matlab归一直方图
时间: 2023-11-02 13:03:52 浏览: 133
归一化直方图是指将直方图的纵坐标(即各个像素级上的像素出现的次数)除以总像素数量,得到当前次数占总次数的概率。在Matlab中,可以通过对直方图的纵坐标进行除法运算实现直方图的归一化。下面是一个示例代码:
```matlab
F=imread('a1.jpg');
I=rgb2gray(F);
[count,x]=imhist(I); % 获取直方图的横坐标和纵坐标,即各个像素级,以及每个像素级上的像素出现的次数
count_normalized = count / numel(I); % 归一化直方图
subplot(1,2,1),imshow(I),title('原图')
subplot(1,2,2),stem(x,count_normalized),title('归一化直方图')
```
相关问题
MATLAB中直方图均衡化函数
### MATLAB 中 `histeq` 函数实现直方图均衡化
在 MATLAB 中,`histeq` 是专门用于执行灰度图像直方图均衡化的内置函数[^1]。该函数通过调整图像的像素强度分布,使得变换后的图像具有更均匀的直方图,从而增强图像对比度。
#### 基本语法
最简单的调用方式只需要提供输入图像作为参数:
```matlab
J = histeq(I);
```
其中 `I` 表示原始灰度图像,而返回值 `J` 即为应用了直方图均衡化之后的新图像。
为了指定输出图像中的灰度级数,默认情况下会自动设置为适合大多数情况的最佳值(通常是256),也可以显式设定此参数:
```matlab
J = histeq(I, n_levels);
```
这里 `n_levels` 定义了目标直方图中期望的不同亮度级别的数量。
#### 实际案例展示
下面是一个完整的例子,展示了如何加载一张测试图片并对其实施直方图均衡化操作,最后将原图与处理过的图像并列显示以便直观比较效果:
```matlab
close all;
clear;
clc;
% 读取样本图像文件 'pout.tif'
I = imread('pout.tif');
% 对图像进行直方图均衡化处理,保持默认256个灰度等级
J = histeq(I);
% 并排显示原始图像及其经直方图均衡化后的版本
imshowpair(I, J, 'montage');
title('左图为原始图像 | 右图为直方图均衡化后图像')
```
这段代码首先关闭所有打开窗口、清除工作区变量以及清屏准备环境;接着读入名为 "pout.tif" 的标准测试图像,并对其进行直方图均衡化转换;最终采用 `imshowpair()` 方法以拼贴形式同时呈现两幅图像供用户查看差异。
MATLAB中直方图均衡化的几种办法
在MATLAB中,有几种方法可以实现直方图均衡化。以下是其中几种常用的方法:
1. 使用imhist()函数和histeq()函数进行直方图均衡化:
```matlab
% 读取图像
img = imread('image.jpg');
% 计算原始图像的直方图
hist = imhist(img);
% 使用histeq()函数进行直方图均衡化
eq_img = histeq(img);
% 显示原始图像和均衡化后的图像
subplot(1,2,1), imshow(img), title('Original Image');
subplot(1,2,2), imshow(eq_img), title('Equalized Image');
```
2. 使用adapthisteq()函数进行自适应直方图均衡化:
```matlab
% 读取图像
img = imread('image.jpg');
% 使用adapthisteq()函数进行自适应直方图均衡化
eq_img = adapthisteq(img);
% 显示原始图像和均衡化后的图像
subplot(1,2,1), imshow(img), title('Original Image');
subplot(1,2,2), imshow(eq_img), title('Equalized Image');
```
3. 使用histeq()函数和imadjust()函数进行直方图均衡化:
```matlab
% 读取图像
img = imread('image.jpg');
% 使用imadjust()函数进行直方图拉伸
adj_img = imadjust(img);
% 使用histeq()函数进行直方图均衡化
eq_img = histeq(adj_img);
% 显示原始图像、直方图拉伸后的图像和均衡化后的图像
subplot(1,3,1), imshow(img), title('Original Image');subplot(1,3,2), imshow(adj_img), title('Adjusted Image');
subplot(1,3,3), imshow(eq_img), title('Equalized Image');
```
这些方法可以根据不同的需求选择使用。直方图均衡化可以改善图像的对比度和细节,并提高图像处理算法的效果。
阅读全文
相关推荐














