本文主要是介绍cv2 获取图形平均色调(H),饱和度(S),明度(V); jupyter中显示opencv读取图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
获取hsv需要把rgb图片转到hsv色彩空间
安装库:
!pip install -i https://pypi.douban.com/simple opencv-python
注意点cv2库:(不然有错误等着你)
1、py文件命名不能是cv2
2、imread图形路径不能含有中文
import cv2
import numpy as np## 色调(H),饱和度(S),明度(V)image = cv2.imread(r"C:\Us****mg2.jpg")hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
H, S, V = cv2.split(hsv)
print(H, S, V)## 明度(V)
v = V.ravel()[np.flatnonzero(V)] #亮度非零的值
average_v = sum(v)/len(v)
print(average_v)
## 饱和度(S)
s = S.ravel()[np.flatnonzero(S)]
average_s = sum(s)/len(s)
print(average_s)
## 色调(H)
h = H.ravel()[np.flatnonzero(H)]
average_h = sum(h)/len(h)
print(average_h)
jupyter中显示opencv读取图片
import cv2
from matplotlib import pyplot as plt
%matplotlib inlineim = cv2.imread(IMG_PATH,1) # load image as bgr
im2 = im[:,:,::-1] # transform image to rgb
plt.imshow(im2)
plt.show()
这篇关于cv2 获取图形平均色调(H),饱和度(S),明度(V); jupyter中显示opencv读取图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!