Tell what is the predominant color

Asked

Viewed 601 times

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()
  • 3

    What is the question? Is there a mistake happening? Are you giving an answer that is not correct? What is happening?

  • 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)

  • Edit your question with this information. That way people who read it will already have more tools to help you

  • 1

    If you have RGB(228,47,53), would not just verify which is the highest value to know the prevailing color?

  • 1

    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.

  • 1

    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)

  • 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

  • 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

  • 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

Show 4 more comments

1 answer

0

If you want to return the color as (red, yellow, green), first recommend you convert from RGB to HSV, and then make a range of each color, as for example,

lower_blue = np.array([90,109,50],np.uint8)
upper_blue = np.array([120,255,255],np.uint8)

After your program returns the predominant color you compare if it is within this range. I imagine this works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.