代码全是GPT4写的,我就提出Prompt和要改的地方而已。

图形界面效果

可复制阈值:(xxx, xxx, xxx, xxx, xxx, xxx)

代码

import cv2
import numpy as np
import time
from tkinter import *
from PIL import Image, ImageTk
import pyperclip  # new

# Global variables for storing the LAB color space threshold values
l_min_g, a_min_g, b_min_g, l_max_g, a_max_g, b_max_g = 0, 0, 0, 255, 255, 255

class App:
    def __init__(self, window, window_title, video_source=0):
        self.window = window
        self.window.title(window_title)
        self.video_source = video_source
        self.ok = False

        self.vid = cv2.VideoCapture(self.video_source)
        if not self.vid.isOpened():
            raise ValueError("Unable to open video source", video_source)

        self.canvas1 = Canvas(window, width=480, height=360)
        self.canvas1.pack(side=LEFT)
        self.canvas2 = Canvas(window, width=480, height=360)
        self.canvas2.pack(side=LEFT)

        self.btn_snapshot = Button(window, text="Snapshot", width=50, command=self.snapshot)
        self.btn_snapshot.pack(anchor=CENTER, expand=True)

        self.l_min_slider = Scale(window, from_=0, to=255, orient=HORIZONTAL, label="L_min", length=480)
        self.l_min_slider.pack()

        self.a_min_slider = Scale(window, from_=0, to=255, orient=HORIZONTAL, label="A_min", length=480)
        self.a_min_slider.pack()

        self.b_min_slider = Scale(window, from_=0, to=255, orient=HORIZONTAL, label="B_min", length=480)
        self.b_min_slider.pack()

        self.l_max_slider = Scale(window, from_=0, to=255, orient=HORIZONTAL, label="L_max", length=480)
        self.l_max_slider.pack()

        self.a_max_slider = Scale(window, from_=0, to=255, orient=HORIZONTAL, label="A_max", length=480)
        self.a_max_slider.pack()

        self.b_max_slider = Scale(window, from_=0, to=255, orient=HORIZONTAL, label="B_max", length=480)
        self.b_max_slider.pack()

        self.btn_copy = Button(window, text="Copy LAB Values", command=self.copy_values)  # new
        self.btn_copy.pack()

        self.delay = 10
        self.update()

        self.window.mainloop()

    def snapshot(self):
        if self.ok:
            cv2.imwrite("frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg", cv2.cvtColor(self.frame, cv2.COLOR_RGB2BGR))

    def copy_values(self):  # new
        l_min = self.l_min_slider.get()
        a_min = self.a_min_slider.get()
        b_min = self.b_min_slider.get()
        l_max = self.l_max_slider.get()
        a_max = self.a_max_slider.get()
        b_max = self.b_max_slider.get()

        values_str = f"({l_min}, {a_min}, {b_min}, {l_max}, {a_max}, {b_max})"
        pyperclip.copy(values_str)

    def update(self):
        ret, self.frame = self.vid.read()
        if ret:
            self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
            self.photo1 = ImageTk.PhotoImage(image=Image.fromarray(self.frame).resize((480, 360)))
            self.canvas1.create_image(0, 0, image=self.photo1, anchor=NW)

            l_min = self.l_min_slider.get()
            a_min = self.a_min_slider.get()
            b_min = self.b_min_slider.get()
            l_max = self.l_max_slider.get()
            a_max = self.a_max_slider.get()
            b_max = self.b_max_slider.get()

            lower_bound = np.array([l_min, a_min, b_min])
            upper_bound = np.array([l_max, a_max, b_max])
            mask = cv2.inRange(cv2.cvtColor(self.frame, cv2.COLOR_RGB2Lab), lower_bound, upper_bound)

            self.photo2 = ImageTk.PhotoImage(image=Image.fromarray(mask).resize((480, 360)))
            self.canvas2.create_image(0, 0, image=self.photo2, anchor=NW)

            self.ok = True

        self.window.after(self.delay, self.update)

App(Tk(), "LAB Thresholding", video_source=2)

video_source参数为摄像头ID。

被取代是我的命运,我了解。。。

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐