0
How could it be done so that when the predominant color in the video appears a message telling which is the predominant color?
Example: appears an object in the capture of red video there would appear a message saying that the predominant color is red, this code detects the colors green, red and yellow.
import cv2
import numpy as np
captura = cv2.VideoCapture(0)
while (1):
_, imagen = captura.read()
hsv = cv2.cvtColor(imagen, cv2.COLOR_BGR2HSV)
lower_green = np.array([49, 50, 50])
upper_green = np.array([107, 255, 255])
lower_red1 = np.array([0, 65, 75], dtype=np.uint8)
upper_red1 = np.array([12, 255, 255], dtype=np.uint8)
lower_red2 = np.array([240, 65, 75], dtype=np.uint8)
upper_red2 = np.array([256, 255, 255], dtype=np.uint8)
lower_yellow = np.array([16, 76, 72], dtype=np.uint8)
upper_yellow = np.array([30, 255, 210], dtype=np.uint8)
mask_green = cv2.inRange(hsv, lower_green, upper_green)
mask_red1 = cv2.inRange(hsv, lower_red1, upper_red1)
mask_red2 = cv2.inRange(hsv, lower_red2, upper_red2)
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
kernel = np.ones((6, 6), np.uint8)
mask_green = cv2.morphologyEx(mask_green, cv2.MORPH_CLOSE, kernel)
mask_green = cv2.morphologyEx(mask_green, cv2.MORPH_OPEN, kernel)
mask_yellow = cv2.morphologyEx(mask_yellow, cv2.MORPH_CLOSE, kernel)
mask_yellow = cv2.morphologyEx(mask_yellow, cv2.MORPH_OPEN, kernel)
mask_red1 = cv2.morphologyEx(mask_red1, cv2.MORPH_OPEN, kernel)
mask_red1 = cv2.morphologyEx(mask_red1, cv2.MORPH_OPEN, kernel)
mask_red2 = cv2.morphologyEx(mask_red2, cv2.MORPH_OPEN, kernel)
mask_red2 = cv2.morphologyEx(mask_red2, cv2.MORPH_OPEN, kernel)
mask1 = cv2.add(mask_red1, mask_red2)
mask2 = cv2.add(mask_green,mask_yellow)
cv2.imshow('red', mask1)
cv2.imshow('green', mask2)
cv2.imshow('Camara', imagen)
tecla = cv2.waitKey(5) & 0xFF
if tecla == 27:
break
cv2.destroyAllWindows()
As I could do in case the predominant color is red, from a print saying that the color and red, if possible just give me a way to do the research, thank you very much
– Nycolas
Search for color spaces (colorspaces), where you have 3 RGB channels, and can convert to other types of spaces. [255,0,0] is red, [0,255,0] green and [0,0,255] blue. So which channel has the highest value is the dominant channel. Or you could create other types of colors and various ranges for each one. For example that color palette
– danieltakeshi
I’m unable to do the programming, could help me?
– Nycolas