def get_color_name(hsv_val):
h, s, v = hsv_val
if s < 20 and v > 160:
return 'white'
elif s < 20 and v < 60:
return 'black'
elif s < 20 and 60 <= v <= 160:
return 'gray'
elif h < 15 or (h > 165 and h < 180):
return 'red'
elif 15 <= h < 35:
return 'orange'
elif 35 <= h < 85:
return 'yellow'
elif 85 <= h < 155:
return 'green'
elif 155 <= h < 185:
return 'cyan'
elif 185 <= h < 265:
return 'blue'
elif 265 <= h < 345:
return 'purple'
else:
return 'unknown'
# Example usage:
# The HSV value should be in 0-360 for H, 0-100 for S, and 0-100 for V.
# Here we're assuming the Hue value in the range 0-360 that some systems use,
# but if you're using a system where Hue is in the range 0-180, you should scale accordingly.
hsv_color = (120, 90, 75) # An example HSV color
color_name = get_color_name(hsv_color)
print("Detected color:", color_name)
def get_color_name(hsv_val):
h, s, v = hsv_val
if s < 20 and v > 200:
return 'white'
elif s < 20 and v < 50:
return 'black'
elif s < 20 and 50 <= v <= 200:
return 'gray'
elif h < 8 or (h > 172):
return 'red'
elif 8 <= h < 22:
return 'orange'
elif 22 <= h < 38:
return 'yellow'
elif 38 <= h < 75:
return 'green'
elif 75 <= h < 95:
return 'cyan'
elif 95 <= h < 130:
return 'blue'
elif 130 <= h < 160:
return 'purple'
else:
return 'unknown'
# Example usage with an OpenCV image:
import cv2
# Assuming `image` is your BGR image loaded using cv2.imread()
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_image) # get h, s, v channels separately
x, y = 0, 0 # Example coordinates of the pixel you want to check
# Note that OpenCV reads x and y in (col, row) format
hsv_color = (h[y, x], s[y, x], v[y, x])
color_name = get_color_name(hsv_color)
print("Detected color:", color_name)