统计RGB、灰度图各像素分布,直接替换 读取的图片
fig,ax1= plt.subplots(1, 1)
ax1.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
img = cv2.imread('train_00000031.jpg') #读取的图片
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(hist,color="gray")
ax1.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
plt.xlim([0,256])
ax1.set_xlabel("range of pixel values")
ax1.set_ylabel("nums of pixel")
ax1.legend(["Gray"],loc="upper right")
plt.savefig(r'numberOfpixels.svg',dpi=300,format='svg')
plt.show()
print("finish!")
img=cv2.imread('VOCdevkit/VOC2007/JPEGImages/1001.jpg') #读取图片,若图片就是灰度图就不用第二行
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #将BGR转为灰度图
fig,ax1= plt.subplots(1, 1)
ax1.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(hist,color="gray")
ax1.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
plt.xlim([0,256])
ax1.set_xlabel("range of pixel values")
ax1.set_ylabel("nums of pixel")
ax1.legend(["Gray"],loc="upper right")
plt.savefig(r'numberOfpixels.svg',dpi=300,format='svg')
plt.show()
print("finish!")