How to get HSV and LAB color space?

Question:

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:
HSV Values by OpenCV

If I get those values and I reverse the order, I am getting the RGB values:
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

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…

Asked By: Jota

||

Answers:

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
Answered By: Rick M.
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.