Python代码库OpenCV之04读取图片绘制直方图(含代码)
展开成直方图
代码
import numpy as np
import cv2
from matplotlib import pyplot as plt
color = ['b','g','r']
image = cv2.imread("D:\\pythondev\\dev\\opencv\\img\\tu.png")
histogram_image = cv2.calcHist([image], [0], None, [256], [0,256])
plt.hist(histogram_image.ravel(), 256, [0,256])
plt.show()
效果
![](https://img.haomeiwen.com/i41085/71c9f3329f982193.png)
展开成直方图
分离颜色通道
代码
import numpy as np
import cv2
from matplotlib import pyplot as plt
image = cv2.imread("D:\\pythondev\\dev\\opencv\\img\\tu.png")
color = ['b','g','r']
#seperate the colors and plot the histogram
for i, col in enumerate(color):
hist = cv2.calcHist([image], [i], None, [256], [0,256])
plt.plot(hist, color = col)
plt.xlim([0,256])
plt.show()
运行效果
![](https://img.haomeiwen.com/i41085/bc1bf5c3d78000c1.png)
分离颜色通道
更多精彩代码请关注我的专栏
网友评论