1

I have these lists containing blue values in RGB format.

low = [
    [0, 0, 128],
    [65, 105, 225],
    [70, 130, 180],
    [72, 61, 139],
    [83, 104, 120]
]

What I want to make is this: convert all the values from, for example, first list from RGB to HSV.

I made this code:

import cv2
import numpy as np

for v in low:
    rgb = np.uint8([[v]])
    print("RGB: ", rgb)

    hsv = cv2.cvtColor(rgb, cv2.COLOR_RGB2HSV)
    print("HSV: ", hsv)
    print("\n")

The problem is when I go to check if the colors (RGB-HSV) are the same. Here I discovered that it's not.

Let's take the last value from low list.

RGB:  [[[ 83 104 120]]]
HSV:  [[[103  79 120]]]

RGB is RGB input value and HSV is output. But this last one it's not the same color as RGB. First is a shade of blue and last is green. Why?

I used this tool to check values. It also says that the right HSV for this RGB should be 205, 30, 47.

Where is my mistake?

sniperd
  • 5,124
  • 6
  • 28
  • 44
lucians
  • 2,239
  • 5
  • 36
  • 64
  • 1
    the chances of getting answers relate to the quality of code provided. See [minimal verifyable complete example](https://stackoverflow.com/help/mcve). - we do not need 500 colors to see the behavior. Also : you are lacking your imports (f.e. cv2)? Please edit and shrink your code, thanks. – Patrick Artner May 21 '18 at 13:11
  • 2
    [Found this](http://shervinemami.info/colorConversion.html)...Hope will help – lucians May 21 '18 at 13:38
  • The tool you used for verification has range [0,359] for Hue, [0,100] for Saturation and Value. OpenCV's HSV range are [0,179] for Hue, [0,255] for Saturation and Value. – FBergo May 21 '18 at 13:55
  • Possible duplicate: https://stackoverflow.com/questions/43307959/how-to-get-hsv-and-lab-color-space/43319977#43319977 – Rick M. May 22 '18 at 07:23

2 Answers2

9

The tool you used for verification has range [0,359] for Hue, and range [0,100] for Saturation and Value. OpenCV's HSV ranges are [0,179] for Hue, [0,255] for Saturation and Value.

Multiply by 2, 1/2.55, 1/2.55 and you will get the expected values, off by minor integer truncation errors: [103 79 120] * [2 1/2.55 1/2.55] = [206 31 47]

FBergo
  • 1,010
  • 8
  • 11
2

Although @FBergo's answer is correct, I'd like to add that these conversions (multiply by...) are type-dependent and care must be taken when using conversions for 8UC3, 16SC3, 32SC3, 32FC3 and so on.

Rick M.
  • 3,045
  • 1
  • 21
  • 39