代码全是GPT4写的,我就提出Prompt和要改的地方而已。
图形界面效果
代码
import cv2
import numpy as np
import time
from tkinter import *
from PIL import Image, ImageTk
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.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 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)
被取代是我的命运,我了解。。。