3

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...

Jota
  • 697
  • 10
  • 24
  • Have you tried to split the channels, e.g. `h, s, v = = cv2.split(img_hsv)`, to manipulate them one by one and merge them together, `img = cv2.merge((h,s,v)`, to get your desired result? – thewaywewere Apr 09 '17 at 19:14
  • What do you mean it isn't correct? If you put the normalized value of HSV and convert it to RGB using `cvtColor`, of course it will give you different colors. – Rick M. Apr 11 '17 at 12:24
  • If I do `img_hsv = cv2.cvtColor(image,cv.CV_BGR2HSV)` all values are in BGR no HSV I mean. Is there any way to get the HSV value not the BGR value? – Jota Apr 11 '17 at 13:07
  • The values in img_hsv are in the range 0-255, but are HSV in OpenCV color space. You need to use the conversion to bring them into general HSV color space. (0-360, 0-100, 0-100) – Rick M. Apr 11 '17 at 13:09
  • Like I told you, if I do that, normalize the `img_hsv` values, I get random colors because they are in BGR no HSV. I do `img_hsv = cv2.cvtColor(image,cv.CV_BGR2HSV)` and all values in `img_hsv` are in BGR, like the post edit. You can see the images. `img_hsv[50,50]` returns the BGR value, I can print it with any RGB tool and is the same color than the HSV image. If I want the HSV value from `img_save` I have to convert from BGR (`img_hsv`) to HSV and that it is works – Jota Apr 11 '17 at 13:33
  • So what you are saying is with `cv.CV_BGR2HSV` is actually not doing any conversion? – Rick M. Apr 11 '17 at 14:53

1 Answers1

2

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
Rick M.
  • 3,045
  • 1
  • 21
  • 39
  • 1
    I think H is `H(OpenCV) * 360/255.0`, do not you? – Jota Apr 11 '17 at 10:09
  • I answered you in the Edit of my post. – Jota Apr 11 '17 at 10:46
  • My bad on the H value, however I was just following the reference. – Rick M. Apr 11 '17 at 12:22
  • `hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) print(hsv[47,115])` returns `[ 98 245 202]`. Go to http://colorizer.org/ and insert `[ 98 245 202]` inverted in the RGB section. You will get the yellow color like the HSV image. So, `print(hsv[47,115])` returns the BGR value, no HSV value. Try to print `[ 98 245 202]` value like HSV, you will never get the yellow color. – Jota Apr 11 '17 at 14:41
  • What is the BGR value at 47,115? You are still not getting what I mean. I can't stress more. – Rick M. Apr 11 '17 at 14:52
  • Thanks a lot for your time Rick M. I appreciate it. If I convert a blue image to HSV I get a yellow image (the blue from the original RGB image pass to yellow after the HSV conversion). So, If I get any coordinate value from the HSV image (yellow image after conversion) (`img_hsv = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2HSV)`) I get the BGR value, in this case, I get `[ 98 245 202]` which is yellow in BGR. – Jota Apr 11 '17 at 15:00
  • The previous H*2.0 was correct. You have to understand that OpenCV loads an RGB image as BGR. I triple checked the answer and it is correct. [200, 255, 255] in RGB, converting using RGB2HSV you get [120, 255, 255] which using the formula gives 240, 100, 100 which is correct. For clarification, [200, 255, 255] in RGB space is [255, 255, 200] in BGR space. If you do BGR2HSV for the latter you get the same HSV when you do RGB2HSV with the former. – Rick M. Apr 11 '17 at 15:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141443/discussion-between-rick-m-and-jota). – Rick M. Apr 11 '17 at 15:07