问题
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?
回答1:
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]
回答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.
来源:https://stackoverflow.com/questions/50449602/wrong-hsv-conversion-from-rgb