如何获得HSV和LAB色彩空间?

时间:2017-04-09 14:35:22

标签: python opencv hsv lab

我正在使用OpenCV和Python。我的代码是:

img_hsv = cv2.cvtColor(image,cv.CV_BGR2HSV)
img_lab = cv2.cvtColor(image,cv.CV_BGR2Lab)

当我访问像素值时,我在RGB空间中获取值,例如:

img_hsv[x][y] = [255,255,255]

如何规范化HSV和LAB色彩空间? HSV = 360°100%100%且LAB = 128 100 100

EDIT1。回答Rick M。: 您的解决方案不正确,因为当我翻译OpenCV的值时,就像你对HSV说的那样,我会得到随机颜色。

例如。原始图像检测的值为img_hsvHSV Values by OpenCV

如果我得到这些值并且我反转顺序,我得到RGB值: enter image description here

HSV Value = 16, 25, 230 -> Invert -> 230, 25, 16 = RGB Value
HSV Value = 97, 237, 199 -> Invert -> 199, 237, 97 = RGB Value

所以,当我得到img_hsv的值时,如果我反转顺序,我得到RGB值......那么OpenCV在img_hsv = cv2.cvtColor(image,cv.CV_BGR2HSV)做什么呢?我认为OpenCV返回BGR值......

1 个答案:

答案 0 :(得分:2)

OpenCV带来范围内的所有颜色空间的输出(0,255)注意:这是依赖于Mat类型的,假设8UC3在这里

因此,要将HSV带入其范围:

H(HSV original) = H(OpenCV) * 2.0
S(HSV original) = S(OpenCV) * 100/255.0

V(HSV original) = V(OpenCV) * 100/255.0

类似于Lab颜色空间:

L(Lab original) = L(OpenCV) * 100/255.0

a(Lab original) = a(OpenCV) - 128

b(Lab original) = b(OpenCV) - 128

Reference

添加支票real color conversion, python代码:

image_rgb = np.zeros((300, 300, 3), np.uint8)
image[:] = (255, 255, 255)

img_hsv = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2HSV)
h = img_hsv[100, 100, 0]
s = img_hsv[100, 100, 1]
v = img_hsv[100, 100, 2]
print h , s , v
>>> 0 0 255