|
|
|
@ -6,14 +6,12 @@ import numpy as np
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
from tkinter import ttk
|
|
|
|
|
from PIL import ImageTk, Image
|
|
|
|
|
from tkinter import filedialog
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# plt显示彩色图片
|
|
|
|
|
def plt_show0(img):
|
|
|
|
|
# cv2与plt的图像通道不同:cv2为[b,g,r];plt为[r, g, b]
|
|
|
|
|
b, g, r = cv2.split(img)
|
|
|
|
|
img = cv2.merge([r, g, b])
|
|
|
|
|
plt.imshow(img)
|
|
|
|
|
plt.show()
|
|
|
|
|
def eaowej(file_path):
|
|
|
|
|
global history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# plt显示灰度图片
|
|
|
|
@ -29,7 +27,7 @@ def gray_guss(image):
|
|
|
|
|
return gray_image
|
|
|
|
|
|
|
|
|
|
# 读取待检测图片
|
|
|
|
|
origin_image = cv2.imread('../xiangA.jpg')
|
|
|
|
|
origin_image = cv2.imread(file_path)
|
|
|
|
|
|
|
|
|
|
# 复制一张图片,在复制图上进行图像操作,保留原图
|
|
|
|
|
image = origin_image.copy()
|
|
|
|
@ -264,6 +262,7 @@ result = template_matching(word_images_)
|
|
|
|
|
#print(result)
|
|
|
|
|
# "".join(result)函数将列表转换为拼接好的字符串,方便结果显示
|
|
|
|
|
#print( "".join(result))
|
|
|
|
|
history.append("".join(result))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from PIL import ImageFont, ImageDraw, Image
|
|
|
|
@ -285,25 +284,64 @@ draw = ImageDraw.Draw(img_pil)
|
|
|
|
|
draw.text((int(0.2*weight)+25, int(0.75*height)), "".join(result), font = font, fill = (255, 255, 0))
|
|
|
|
|
bk_img = np.array(img_pil)
|
|
|
|
|
#print(result)
|
|
|
|
|
print( "".join(result))
|
|
|
|
|
plt_show0(bk_img)
|
|
|
|
|
|
|
|
|
|
# 使用PIL将numpy数组转换为Image对象
|
|
|
|
|
image_tk = Image.fromarray(cv2.cvtColor(bk_img, cv2.COLOR_BGR2RGB))
|
|
|
|
|
return bk_img
|
|
|
|
|
|
|
|
|
|
# 初始化Tkinter窗口
|
|
|
|
|
root = tk.Tk()
|
|
|
|
|
root.title("车牌识别结果")
|
|
|
|
|
|
|
|
|
|
# 将Image对象转换为Tkinter支持的PhotoImage对象
|
|
|
|
|
photo_image = ImageTk.PhotoImage(image_tk)
|
|
|
|
|
root = tk.Tk()
|
|
|
|
|
root.title("图片导入器")
|
|
|
|
|
root.geometry("1000x1000")
|
|
|
|
|
|
|
|
|
|
# 用于存储历史记录的列表
|
|
|
|
|
history = []
|
|
|
|
|
current_image_label = None
|
|
|
|
|
# 全局变量,用于存储加载的图像
|
|
|
|
|
# 显示图像的函数
|
|
|
|
|
def show_image(image):
|
|
|
|
|
global current_image_label # 引用全局变量
|
|
|
|
|
if current_image_label is not None: # 如果存在先前的Label,先删除它
|
|
|
|
|
current_image_label.pack_forget() # 使用pack_forget()移除组件
|
|
|
|
|
|
|
|
|
|
pil_image = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)))
|
|
|
|
|
lbl_image = tk.Label(root, image=pil_image)
|
|
|
|
|
lbl_image.image = pil_image # 保持对图像的引用,防止垃圾回收
|
|
|
|
|
lbl_image.pack()
|
|
|
|
|
current_image_label = lbl_image # 更新全局变量,存储新的Label引用
|
|
|
|
|
def open_image():
|
|
|
|
|
# 弹出文件选择对话框并返回用户选择的文件路径
|
|
|
|
|
file_path = filedialog.askopenfilename()
|
|
|
|
|
return file_path
|
|
|
|
|
def load_and_show_image():
|
|
|
|
|
file_path = open_image() # 调用函数获取文件路径
|
|
|
|
|
show_image(eaowej(file_path)) # 传递文件路径给加载图像的函数
|
|
|
|
|
|
|
|
|
|
# 显示历史记录的函数
|
|
|
|
|
|
|
|
|
|
# 创建标签以显示图像
|
|
|
|
|
label = ttk.Label(root, image=photo_image)
|
|
|
|
|
label.pack() # 将标签添加到窗口中并自动调整大小
|
|
|
|
|
def plt_show0(img):
|
|
|
|
|
# cv2与plt的图像通道不同:cv2为[b,g,r];plt为[r, g, b]
|
|
|
|
|
b, g, r = cv2.split(img)
|
|
|
|
|
img = cv2.merge([r, g, b])
|
|
|
|
|
plt.imshow(img)
|
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
# 防止Tkinter在图像数据被垃圾回收时删除图像
|
|
|
|
|
label.image = photo_image
|
|
|
|
|
|
|
|
|
|
# 运行Tkinter事件循环
|
|
|
|
|
def show_history():
|
|
|
|
|
# 创建一个新的窗口来显示历史记录
|
|
|
|
|
history_window = tk.Toplevel(root)
|
|
|
|
|
history_window.title("历史记录")
|
|
|
|
|
history_window.geometry("600x400") # 根据需要调整大小
|
|
|
|
|
|
|
|
|
|
# 创建一个文本框来显示历史记录
|
|
|
|
|
txt_history = tk.Text(history_window, height=10, width=50)
|
|
|
|
|
txt_history.pack(pady=20)
|
|
|
|
|
|
|
|
|
|
# 将历史记录数据添加到文本框中
|
|
|
|
|
for record in history:
|
|
|
|
|
txt_history.insert(tk.END, record + "\n")
|
|
|
|
|
button_history = tk.Button(root, text="查看历史记录", command=show_history)
|
|
|
|
|
button_history.pack()
|
|
|
|
|
# 创建一个按钮,点击时调用 load_and_show_image 函数
|
|
|
|
|
button = tk.Button(root, text="导入图片", command=load_and_show_image)
|
|
|
|
|
button.pack()
|
|
|
|
|
root.mainloop()
|
|
|
|
|
print(history)
|