I don't know if this post belongs in this site because I feel it might be a programming question, but also I feel it might be related to the way Color Spaces work, if it does't belong here I can change it. I would like to know how many different HSV colors are possible to generate using OpenCV.
HSV colors are defined by three values: its Hue ranging from $0^ {\circ}$ to $360^ {\circ}$, its Saturation ranging from $0$ to $1$, and its Value ranging from $0$ to $1$. In OpenCV they're defined like this: Hue ranging from $0$ to $180$, Saturation ranging from $0$ to $255$, and Value ranging from $0$ to $255$. So theoretically one should be able to create $181*256*256=11,862,016$ different colors (I used $181$ and $256$ instead of $180$ and $255$ because $0$ counts as a value).
The problem is that if I try to generate all these possible combinations using Python, the amount of different colors is less than the theoretical value.
This is my code to generate an HSV Ramp (Hue varies along the X axis, Saturation along the Y axis, and for simplicity Value is going to be left as 255):
# Importing libraries
import cv2
import numpy as np
# Creating a Blank Image
height, width = 256, 181
blank_image = np.zeros((height, width, 3), np.uint8)
# Creating a vertical HSV Ramp for Blank Image
hue = 0
for x in range(0, width):
sat = 255
for y in range(0, height):
blank_image[y, x] = hue, sat, 255
sat -= 1
hue += 1
# Transforming HSV image into BGR to display it
hsv_ramp = cv2.cvtColor(blank_image, cv2.COLOR_HSV2BGR)
# Counting unique BGR Values of all pixels
arr = np.array(hsv_ramp)
bgr_values, c = np.unique(arr.reshape(-1, arr.shape[2]), axis=0, return_counts=True)
# Printing Values
for j, i in zip(bgr_values, c):
print(i, j)
# Displaying Image
cv2.imshow("HSV Ramp", hsv_ramp)
cv2.waitKey(0)
To run the code, to sort and to save the different pixel values I use the next bash command:
python3 hsv_ramp_generator_.py | sort -n > pixels_values.txt
This is the image generated:
By examining the pixels_values.txt text file, it's possible to see that many pixels generated using the HSV color space are actually the same color in the BGR color space, just to show a few of the most repeated ones:
Repetitions BGR_Values
11 [250 250 255]
11 [250 255 250]
11 [255 250 250]
15 [251 251 255]
15 [251 255 251]
15 [253 254 255]
15 [253 255 254]
15 [254 253 255]
15 [254 255 253]
15 [255 251 251]
15 [255 253 254]
15 [255 254 253]
19 [252 255 252]
19 [255 252 252]
20 [252 252 255]
29 [253 255 253]
29 [255 253 253]
30 [253 253 255]
59 [254 255 254]
59 [255 254 254]
60 [254 254 255]
181 [255 255 255]
Let's take for example the first row (11 [250 250 255]), these are the HSV colors that are actually the same BGR color:
[H=0,S=250,V=255], [H=1,S=250,V=255], [H=2,S=250,V=255], [H=3,S=250,V=255],[H=4,S=250,V=255], [H=175,S=250,V=255], [H=176,S=250,V=255], [H=177,S=250,V=255], [H=178,S=250,V=255], [H=179,S=250,V=255], [H=180,S=250,V=255]
When considering only HSV colors that its value is equal to $255$, there should be $181*256=46,336$ different colors, but the amount actually found by my code is $41,013$.
Why does this happen? Does it have to do with how HSV and BGR color spaces work, or does it have more to do with OpenCV? Is there a way to theoretically compute the $41,013$ figure?
