gpt4实现对摄像头帧缓冲区图像的LAB阈值选择界面(python-opencv)
代码全是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, 255class App:def __init__(self, window, window_title, video_source=0):self.window = windowself.window.title(window_title)self.video_source = video_sourceself.ok = Falseself.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) # newself.btn_copy.pack()self.delay = 10self.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): # newl_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 = Trueself.window.after(self.delay, self.update)App(Tk(), "LAB Thresholding", video_source=2)
video_source参数为摄像头ID。
被取代是我的命运,我了解。。。