how can i get HSV color upper and lower range?

Viewed 149

I'm currently finding the contour using hsv upper and lower ranges of red, blue, green and yellow. Its working fine but can any one help me to share all hsv upper and lower ranges of the following colors:

#61366B
#5B66AF
#E7FF00
#FF0000
#FF6700
#C21CE5
#45AFAC
#0D9910
#0033C1
1 Answers

You can do it like this:

#!/usr/bin/env python3

import cv2
import numpy as np
import matplotlib.colors

# Define our colours - presumably RGB
h = ['#61366a','#5B66AF','#E7FF00', '#FF0000','#FF6700','#C21CE5','#45AFAC','#0D9910','#0033C1']

# Convert to floats
RGB = [matplotlib.colors.to_rgb(c) for c in h]

# Make into Numpy array of floats, reshape and convert to HSV
HSV = cv2.cvtColor(np.array(RGB,np.float32).reshape(-1,1,3), cv2.COLOR_RGB2HSV)

# Print maxima and minima
print(f'Hue: min {np.min(HSV[...,0])}, max {np.max(HSV[...,0])}')
print(f'Sat: min {np.min(HSV[...,1])}, max {np.max(HSV[...,1])}')
print(f'Val: min {np.min(HSV[...,2])}, max {np.max(HSV[...,2])}')

Sample Output

Hue: min 0.0, max 289.6153564453125
Sat: min 0.47999992966651917, max 0.9999998807907104
Val: min 0.4156862795352936, max 1.0

Be very aware that OpenCV expects a different range of values depending on the image type!!!

Related