I am trying to get a program to detect any hue of black (and some slight gray), but am currently having difficulties trying to find a good upper hue vector (the upper_hue variable) that would allow me extract most hues of black with out extracting other colors. It seems to work well with pictures like the black shoes or umbrella, but there it has problems with photos of roads where the brightness varies.
I am trying to find a good upper hue range that would allow the program to be robust against this, however it risks getting non-black colors.
What would be a good upper hue vector of values for black and gray. If that isn't the issue, what would be a good way to solve this?
Code:
import cv2
import numpy as np
import imutils
def color_seg(choice):
if choice == 'blue':
lower_hue = np.array([100,30,30])
upper_hue = np.array([150,148,255])
elif choice == 'white':
lower_hue = np.array([0,0,0])
upper_hue = np.array([0,0,255])
elif choice == 'black':
lower_hue = np.array([0,0,0])
upper_hue = np.array([50,50,100])
return lower_hue, upper_hue
# Take each frame
frame = cv2.imread('images/black0.jpg')
#frame = cv2.imread('images/road_1.jpg')
frame = imutils.resize(frame, height = 300)
chosen_color = 'black'
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of a color in HSV
lower_hue, upper_hue = color_seg(chosen_color)
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_hue, upper_hue)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.waitKey(0)