Opencv + python:在cvtColor转换后,相同的图像文件在HSV中给出了不同的平均值

时间:2017-07-21 10:48:17

标签: python opencv image-processing colors hsv

尝试打印HSV中“值”的平均值时,我遇到以下错误。图像文件采用RBG颜色格式和uint8类型

Image1

 RESTART: C:\Users\310293649\AppData\Local\Programs\Python\Python36\HSVPLAYING.py

Image 1 V channel average= 138.582907232
Image 2 V channel average= 255.0

对于下面的代码,我认为这个方法中有两个会得到相同的平均V结果,因为它的相同图像?以下图像是转换后img i打印出来的结果。

Image1 Image2

有人向我解释为什么?如果我想获得HSV中V通道的平均值,哪种写法是正确的?

注意:我使用BGR2HSV转换虽然我的文件是RGB,因为我认为OpenCV中imread的默认模式会读取BGR格式的所有图像文件?

#Image 1
img1 = cv2.imread(r'C:\Users\310293649\Desktop\17aP5.png', cv2.COLOR_BGR2HSV)
cv2.imshow('img1', img1)
hue, sat, val = img1[:,:,0], img1[:,:,1], img1[:,:,2]
a = np.mean(img1[:,:,2])
print('Image 1 V channel average=', a)

#Image 2
img2 = cv2.imread(r'C:\Users\310293649\Desktop\17aP5.png')
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
cv2.imshow('img2', img2)
hue, sat, val = img2[:,:,0], img2[:,:,1], img2[:,:,2]
b = np.mean(img2[:,:,2])
print('Image 2 V channel average=', b)

我正面临另一个错误,在尝试仅从图像中的红色区域打印出平均V值时,我也不明白。我在图像4中出错了,我想再次知道哪种写法是正确的?

  

图像3 V通道平均值= 255.0

     
    

>     Traceback(最近一次调用最后一次):       文件“C:\ Users \ 310293649 \ AppData \ Local \ Programs \ Python \ Python36 \ HSVPLAYING.py”,第64行,         cv2.imshow('img4',img4)     cv2.error:C:\ projects \ opencv-python \ opencv \ modules \ highgui \ src \ window.cpp:304:错误:(-215)size.width> 0&&函数cv :: imshow中的size.height> 0     >

  

以下是我的代码

#Image 3
img3 = cv2.imread(r'C:\Users\310293649\Desktop\17aP5.png')
img3 = img3[100:(100+50), 100:(100+50)]
img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)
cv2.imshow('img3', img3)
hue, sat, val = img3[:,:,0], img3[:,:,1], img3[:,:,2]
c = np.mean(img3[:,:,2])
print('Image 3 V channel average=', c)

#Image 4
img4 = cv2.imread(r'C:\Users\310293649\Desktop\17aP5.png', cv2.COLOR_BGR2HSV)
img4 = img4[100:(100+50), 100:(100+50)]
cv2.imshow('img4', img4)
hue, sat, val = img4[:,:,0], img4[:,:,1], img4[:,:,2]
d = np.mean(img4[:,:,2])
print('Image 4 V channel average=', d)

0 个答案:

没有答案