画图片归一化直方图的代码
时间: 2023-11-28 11:45:07 浏览: 38
根据提供的引用,以下是画图片归一化直方图的Python代码:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 读取灰度图像
img = cv2.imread('image.jpg', 0)
# 计算直方图
hist, bins = np.histogram(img.flatten(), 256, [0, 256])
# 计算累积分布函数
cdf = hist.cumsum()
cdf_normalized = cdf * hist.max() / cdf.max()
# 画直方图
plt.plot(cdf_normalized, color='b')
plt.hist(img.flatten(), 256, [0, 256], color='r')
plt.xlim([0, 256])
plt.legend(('cdf', 'histogram'), loc='upper left')
plt.show()
```
其中,`cv2.imread('image.jpg', 0)`用于读取灰度图像,`np.histogram()`用于计算直方图,`hist.cumsum()`用于计算累积分布函数,`plt.plot()`和`plt.hist()`用于画直方图。
相关问题
归一化直方图统计matlab代码
归一化直方图统计matlab代码如下:
```matlab
% 假设数据存储在变量data中
% 假设需要将数据分成20个bins
% 假设需要绘制直方图
% 计算直方图
[counts, edges] = histcounts(data, 20, 'Normalization', 'pdf');
% 绘制直方图
bar(edges(1:end-1), counts, 'hist');
```
其中,`histcounts`函数用于计算直方图,第一个参数为数据,第二个参数为bins的数量,第三个参数为归一化方式,这里选择了pdf归一化。`bar`函数用于绘制直方图,第一个参数为bins的边界,第二个参数为每个bin的计数,第三个参数为绘制方式,这里选择了直方图。
python的归一化直方图怎么画代码实现
以下是使用Python中的numpy和matplotlib库实现归一化直方图的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 生成随机数据
data = np.random.randn(1000)
# 绘制原始数据的直方图
plt.hist(data, bins=30, density=False, alpha=0.5, color='b')
# 计算归一化的直方图
counts, bins = np.histogram(data, bins=30, density=True)
bin_centers = 0.5 * (bins[1:] + bins[:-1])
pdf = counts / np.sum(counts)
# 绘制归一化直方图
plt.plot(bin_centers, pdf, 'r')
# 添加图例和标签
plt.legend(['Normalized histogram', 'Raw data'])
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
```
解释一下代码:
首先,我们使用numpy库生成了1000个随机数据。然后,使用matplotlib的hist函数绘制了原始数据的直方图。
接下来,我们使用numpy的histogram函数计算数据的直方图,并将其归一化。
最后,我们使用matplotlib的plot函数绘制了归一化直方图,并添加了图例和标签,最终展示出来。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)