python - 更改cv2.rectangle的颜色

标签 python opencv colors

我试图建立一个颜色检测系统,当我单击窗口时它会告诉颜色。我现在正在尝试将cv2.rectangle的颜色更改为我单击的颜色。

这是我到目前为止所做的

import cv2
import webcolors
from tkinter import *
from PIL import Image
from PIL import ImageTk
from tkinter import filedialog
import cv2
# --- functions ---

def closest_colour(requested_colour):
    global colorcode
    colorcode = requested_colour
    min_colours = {}
    for key, name in webcolors.css3_hex_to_names.items():
        r_c, g_c, b_c = webcolors.hex_to_rgb(key)
        rd = (r_c - requested_colour[0]) ** 2
        gd = (g_c - requested_colour[1]) ** 2
        bd = (b_c - requested_colour[2]) ** 2
        min_colours[(rd + gd + bd)] = name
    return min_colours[min(min_colours.keys())]

def get_colour_name(requested_colour):

    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = ""
    return actual_name, closest_name

def click_event(event, x, y, flags, param):
    global closest_name, colour2 # inform function to assign to global/external variable instead of creating local one

    if event == cv2.EVENT_LBUTTONDOWN:
        B, G, R = frame[x, y]
        colour2 = (R, G, B)  # reverse values
        colour = frame[y,x][::-1] # reverse values
        actual_name, closest_name = get_colour_name(colour)


font = cv2.FONT_HERSHEY_DUPLEX

closest_name = '' # create global variable at start
requested_colour = ''
cap = cv2.VideoCapture(0);

cv2.namedWindow('frame')
cv2.setMouseCallback('frame', click_event)

while True:
    ret, frame = cap.read()

    if closest_name:
        cv2.rectangle(frame, (600, 60), (0, 0), (224, 224, 224),cv2.FILLED)
        cv2.rectangle(frame, (10, 10), (100, 50), (colour2), -1)
        cv2.rectangle(frame, (10, 10), (100, 50), (0, 0, 0), 2)

        cv2.putText(frame, closest_name, (110, 40), font, 1, (0, 0, 0), 1)


    cv2.imshow('frame', frame)
    cv2.resizeWindow('frame', 500, 450)

    if cv2.waitKey(40) == 27:
        break


这是我得到的错误

Traceback (most recent call last): File "C:/Users/Suhail Misbah/PycharmProjects/Color_Detection/colordetection.py", line 70, in cv2.rectangle(frame, (10, 10), (100, 50), (colour2), -1) TypeError: an integer is required (got type tuple)

最佳答案

colorcv2.rectangle参数需要一个3整数的元组,表示三个颜色分量RGB。通过以下方式调用该函数:

colour2 = (R,G,B) 
cv2.rectangle(frame, (10, 10), (100, 50), (colour2), -1)

您将以下元组传递给函数:((R,G,B)),这是一个包含一个元素的元组:元组。使用
cv2.rectangle(frame, (10, 10), (100, 50), colour2, -1)

代替。

更新:看来frame[x,y]返回的颜色分量是numpy.uint8。但是,colorcv2.rectangle参数需要一个python int。您需要首先转换颜色分量,例如:
cv2.rectangle(frame, (10, 10), (100, 50), (int(x) for x in colour2), -1)

关于python - 更改cv2.rectangle的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58215094/

相关文章:

c++ - 使用 SIFT 描述符的 OpenCV (C++) 增加了检测到的特征的数量?

css - 在相同颜色的 CSS 背景上对齐透明 PNG

python - 如何区分两种颜色?

html - 悬停时字体颜色不会改变

python - 如何扩展模型用户?

python - 忽略工具提示文本并仅获取当前使用 Beautiful Soup 显示的文本

python - session 未创建异常 : Message: session not created: This version of ChromeDriver only supports Chrome version 76 with Selenium ChromeDriver

Python - 获取堆中小于 n 的最大数

c++ - 了解 OpenCV fitEllipse 方法

matlab - 训练新的 LatentSVMDetector 模型