How to get HSV and LAB color space?

非 Y 不嫁゛ 提交于 2019-12-07 17:04:19

问题


I'm using OpenCV with Python. My code is:

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

When I access to a pixel value I'm getting values in RGB space, for example:

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

How can I normalize HSV and LAB color space? HSV = 360º 100% 100% and LAB = 128 100 100

Edit1. Answering to Rick M.: Your solution is not correct because when I translate the values ​​of OpenCV like you said to HSV I get random colors.

For example. Original image detection with the values of img_hsv:

If I get those values and I reverse the order, I am getting the RGB values:

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

So, when I get the values of the img_hsv, if I invert the order I am getting the RGB Value... What is OpenCV doing in img_hsv = cv2.cvtColor(image,cv.CV_BGR2HSV) then? I think OpenCV returns BGR values...


回答1:


OpenCV brings the output of all the color spaces in the range (0, 255) Note: This is Mat type dependent, assuming 8UC3 here.

So, to bring HSV to its range :

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

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

similarly for Lab color space :

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

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

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

Reference

Adding a check, real color conversion, python code:

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


来源:https://stackoverflow.com/questions/43307959/how-to-get-hsv-and-lab-color-space

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!