How to get the HSV and LAB color space?

I am using OpenCV with Python. My code:

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

      

When I access a pixel value, I get the 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 wrong because when I translate the OpenCV values ​​as you said to HSV I get random colors.

For example. Initial image detection with values img_hsv

: HSV values ​​from OpenCV

If I get these values ​​and I reverse the order, I get 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 img_hsv

, if I invert the order, I get the RGB value ... What is OpenCV in img_hsv = cv2.cvtColor(image,cv.CV_BGR2HSV)

then? I think OpenCV is returning BGR values ​​...

+3


source to share


1 answer


OpenCV displays all the color space in the range (0, 255) Note. It depends on the Mata type 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 the Lab color space:



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

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

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

      

Link

Adding validation, 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

      

+2


source







All Articles