1
Example: if the predominant color is a shade of red or red it appears a print saying it is red.
the code works and returns a value in RGB, what I’m not getting and tell which is the predominant color, Example: when the code returns the value in example :RGB (228,47,53), instead of appearing the code in rgb appear the color itself (red, green, blue)
an example of code https://stackoverflow.com/questions/43216772/how-to-check-rgb-colors-against-a-color-range , I just can’t seem to apply my code
import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def grab_frame(cap):
ret,frame = cap.read()
return cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
def atualizar(i):
img = grab_frame(captura)
im1.set_data(img)
im2.set_data(retangulo(img))
def close(event):
if event.key == 'q':
plt.close(event.canvas.figure)
def unique_count_app(a):
colors, count = np.unique(a.reshape(-1,a.shape[-1]), axis=0, return_counts=True)
return colors[count.argmax()]
def retangulo(img):
r, g, b = contar_kmeans(img)
h, w, c = img.shape
rect = np.zeros((h, w, 3), np.uint8)
rect[0:h, 0:w] = (r,g,b)
return rect
def contar_kmeans(img):
data = np.reshape(img, (-1,3))
data = np.float32(data)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
flags = cv2.KMEANS_RANDOM_CENTERS
compactness, labels, centers = cv2.kmeans(data, 1, None, criteria, 10, flags)
return centers[0]
#Inicialização
captura = cv2.VideoCapture(0)
imagem = grab_frame(captura)
#Cria os dois subplots
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
#Cria duas imagens nos subplots
im1 = ax1.imshow(imagem)
im2 = ax2.imshow(retangulo(imagem))
#Animação e atualização
ani = FuncAnimation(plt.gcf(), atualizar, interval=200)
print (imagem)
#Fechar
cid = plt.gcf().canvas.mpl_connect("key_press_event", close)
#Mostrar o gráfico
plt.show()
What is the question? Is there a mistake happening? Are you giving an answer that is not correct? What is happening?
– fernandosavio
Opa Excuse I ended up not explaining so right, the code works and returns a value in RGB, what I’m not getting and tell which is the predominant color, Example: when the code returns the value in example :RGB (228,47,53), instead of appearing the code in rgb appear the color itself (red, green, blue)
– Nycolas
Edit your question with this information. That way people who read it will already have more tools to help you
– fernandosavio
If you have
RGB(228,47,53)
, would not just verify which is the highest value to know the prevailing color?– Woss
this part I’m not getting, I can’t check, example if the code in rgb(204, 195, 253), the predominant color would be correct blue.
– Nycolas
Yes, but note the fact that the Opencv library works on the BGR color space by default, so (204, 195, 253) actually has red as the predominant. Unless a conversion is made:
cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
– danieltakeshi
Yes correct I’m trying to create the Code to make this check but I’m not making much progress, but thank you already gave me an idea of how to proceed
– Nycolas
solved in this way
def retangulo(img):
 r, g, b = contar_kmeans(img)
 h, w, c = img.shape
 rect = np.zeros((h, w, 3), np.uint8)
 rect[0:h, 0:w] = (r,g,b)
 if r > g and r > b:
 print("Red")
 elif g > r and g > b:
 print("Green")
 else:
 print("blue")
 return rect
– Nycolas
only that I end up falling into another problem, that it always stays talking infinitely some color, I wanted to speak only when I put some object in front or something like that
– Nycolas