Reputation: 111
I have worked some time with OpenCV to pop the colors on the screen. At the end, I succeeded to mask the colors in different windows as in the photo below:
Was wondering what is the most efficient and a working way to make the python detect the colors, and make it to run the functions() that were written by me. For example, if green was detected, run the function hellogreen(), which will print hello green when green is detected and so on.
Source Code if needed just in case:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Red color
low_red = np.array([161, 155, 84])
high_red = np.array([179, 255, 255])
red_mask = cv2.inRange(hsv_frame, low_red, high_red)
red = cv2.bitwise_and(frame, frame, mask=red_mask)
# Blue color
low_blue = np.array([94, 80, 2])
high_blue = np.array([126, 255, 255])
blue_mask = cv2.inRange(hsv_frame, low_blue, high_blue)
blue = cv2.bitwise_and(frame, frame, mask=blue_mask)
# Green color
low_green = np.array([25, 52, 72])
high_green = np.array([83, 255, 255])
green_mask = cv2.inRange(hsv_frame, low_green, high_green)
green = cv2.bitwise_and(frame, frame, mask=green_mask)
# Every color except white
low = np.array([0, 42, 0])
high = np.array([179, 255, 255])
mask = cv2.inRange(hsv_frame, low, high)
result = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow("Frame", frame)
cv2.imshow("Red", red)
cv2.imshow("Blue", blue)
cv2.imshow("Green", green)
key = cv2.waitKey(1)
if key == 27:
break
Upvotes: 0
Views: 844
Reputation: 1865
Put these lines at the end of your code (helloblue, hellogreen and hellored are your hypothesized functions):
b = cv2.countNonZero(blue_mask)
r = cv2.countNonZero(red_mask)
g = cv2.countNonZero(green_mask)
if b >= r and b >= g:
helloblue()
elif r >= b and r >= g:
hellred()
elif g >= b and g >= r:
hellgreen()
key = cv2.waitKey(1)
if key == 27:
break
Upvotes: 1