I am currently working on a program that can detect if a red object is kept in my room or is is there a blue object. The rest of my surroundings is either white or black. I have tried to minimize the variation of light in my room.
I have successfully created a mask around the object given a certain hue range. I want my program to print for me :
1) "Red"- If there is a red object
2) "Blue"- If there is a blue object
I don't know how to proceed. Following is my program that cretes the mask around the object that is blue. I have given the hue range of a few other colours also. So that you can try it.
The program:
import cv2
import numpy as np
cam = cv2.VideoCapture(1)
while True:
_, frame = cam.read()
denoised = cv2.GaussianBlur(frame, (31, 31), 35)
hsv = cv2.cvtColor(denoised, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([160, 255, 255])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow('frame', frame)
#cv2.imshow('mask', mask)
cv2.imshow('res', res)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
Hue of different colours (I am not sure about the hue of red because it doesn't work for some colours- I have tried a few solutions from Stackoverflow):
lower_red = np.array([0, 100, 100])
upper_red = np.array([0, 255, 255])
lower_yellow = np.array([15, 210, 20])
upper_yellow = np.array([35, 255, 255])
lower_green = np.array([29, 86, 6])
upper_green = np.array([64, 255, 2555])
lower_orange = np.array([10, 100, 20])
upper_orange = np.array([20,255,255])
Following are some sample images that you can experiment with:


