【问题标题】:Change the color of the cv2.rectangle更改 cv2.rectangle 的颜色
【发布时间】:2019-10-03 08:23:25
【问题描述】:

我正在尝试制作一个颜色检测系统,当我点击窗口时,它会告诉我颜色。我现在正在尝试制作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(最近一次调用最后一次):文件“C:/Users/Suhail Misbah/PycharmProjects/Color_Detection/colordetection.py”,第 70 行,在 cv2.rectangle(frame, (10, 10), (100, 50), (colour2), -1) TypeError: an integer is required (got type tuple)

【问题讨论】:

  • 我真的鼓励你通过python tutorial。你不应该像你那样使用全局变量,它只会让代码变得混乱和难以阅读。

标签: python opencv colors


【解决方案1】:

cv2.rectanglecolor 参数需要一个 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。但是,cv2.rectanglecolor 参数需要 python int。您需要先转换颜色组件,例如:

cv2.rectangle(frame, (10, 10), (100, 50), (int(x) for x in colour2), -1)

【讨论】:

  • 还是一样的 Traceback 吗?如果不考虑提出其他问题,或者按照教程进行操作。
  • 我该怎么做
猜你喜欢
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2020-09-14
  • 1970-01-01
  • 2021-07-24
相关资源
最近更新 更多