Numpy直方圖統計,利用histogram()和histogram2d()對影象的顏色分佈統計
import matplotlib.pyplot as plt
import numpy as np
filename=r'C:\Users\Sun\Desktop\lina.jpg'
img=cv2.imread(filename)
fig,ax=plt.subplots(1,2,figsize=(12,5))
colors=['blue','green','red']
for i in range(3):
hist,x=np.histogram(img[:,:,i].ravel(),bins=256,range=(0,256))
ax[0].plot(0.5*(x[:-1]+x[1:]),hist,label=colors[i],color=colors[i])
ax[0].legend(loc='upper left')
ax[0].set_xlim(0,256)
hist2,x2,y2=np.histogram2d(
img[:,:,0].ravel(),img[:,:,2].ravel(),
bins=(100,100),range=[(0,256),(0,256)])
ax[1].imshow(hist2,extent=(0,256,0,256),origin='lower',cmap='gray')
ax[1].set_ylabel('blue')
ax[1].set_xlabel('red')
plt.show()