# -*- coding:utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt
def grayHist(img, name):
h, w = img.shape[:2]
pixelSequence = img.reshape([h * w, ])
numberBins = 256
histogram, bins, patch = plt.hist(pixelSequence, numberBins,
facecolor='black', histtype='bar')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel("像素值")
plt.ylabel("像素数量")
plt.axis([0, 255, 0, np.max(histogram)])
plt.savefig("E:/save/" + name + ".png")
plt.show()
img = cv2.imread("test.png") #导入图片,图片放在程序所在目录
cv2.namedWindow("imagshow", 2) #创建一个窗口
cv2.imshow('imagshow', img) #显示原始图片
#使用cvtColor转换为HSV图
out_img_HSV=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
hsvChannels=cv2.split(out_img_HSV)
cv2.namedWindow("Hue",2)
cv2.imshow('Hue',hsvChannels[0]) #显示Hue分量
grayHist(hsvChannels[0],"H-Histogram")
cv2.namedWindow("Saturation",2)
cv2.imshow('Saturation',hsvChannels[1]) #显示Saturation分量
grayHist(hsvChannels[1],"S-Histogram")
cv2.namedWindow("Value",2)
cv2.imshow('Value',hsvChannels[2]) #显示Value分量
grayHist(hsvChannels[2],"V-Histogarm")
cv2.waitKey(0)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)