python 图片直方图代码
时间: 2023-04-03 07:04:15 浏览: 75
以下是 Python 的图片直方图代码:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('image.jpg', 0)
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.hist(img.ravel(), 256, [0, 256])
plt.show()
```
其中,`img` 是读入的图片,`hist` 是计算出的直方图数据,`plt.hist()` 是用来绘制直方图的函数。
相关问题
python 生成图片直方图
图片直方图可以使用Python中的OpenCV和Matplotlib库。下面是两个不同的示例代码:
示例1:使用OpenCV和Matplotlib生成图片直方图
```python
import cv2
from matplotlib import pyplot as plt
import numpy as np
def image_hist_demo(image_path):
img = cv2.imread(image_path, 0) # 读取灰度图像
plt.hist(img.ravel(), 256) # 生成直方图
plt.show() # 显示直方图
if __name__ == "__main__":
image_path = "your_image_path"
image_hist_demo(image_path)
```
示例2:使用Matplotlib生成数据直方图
```python
import matplotlib.pyplot as plt
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] # 数据
plt.hist(data) # 生成直方图
plt.show() # 显示直方图
```
python显示灰度图片的直方图代码
以下是 Python 显示灰度图片的直方图代码:
```python
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('image.jpg', )
plt.hist(img.ravel(), 256, [, 256])
plt.show()
```
其中,`cv2.imread('image.jpg', )` 读取灰度图像,`plt.hist()` 绘制直方图。