现TK界面中选择输入单张图片单个数字和多个数字进行基于基于连通区域的字符分割方法进行字符分割以及图像预处理,处理过程显示并保存的界面设计,以完整的Python代码实现
时间: 2023-06-26 21:07:52 浏览: 77
以下是一个基于 tkinter 的界面设计,可以进行单张图片的字符分割和图像预处理,可以选择单个数字或多个数字进行处理,处理过程会在界面上显示并保存。
```python
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import cv2
import numpy as np
class CharacterSegmentationApp(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
# 创建选择图片按钮
self.select_image_button = tk.Button(self)
self.select_image_button["text"] = "Select Image"
self.select_image_button["command"] = self.select_image
self.select_image_button.pack(side="top")
# 创建选择数字数量的下拉菜单
self.number_of_digits_label = tk.Label(self, text="Number of Digits:")
self.number_of_digits_label.pack(side="top")
self.number_of_digits_var = tk.StringVar(self)
self.number_of_digits_var.set("1")
self.number_of_digits_menu = tk.OptionMenu(self, self.number_of_digits_var, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
self.number_of_digits_menu.pack(side="top")
# 创建图像处理按钮
self.process_image_button = tk.Button(self)
self.process_image_button["text"] = "Process Image"
self.process_image_button["command"] = self.process_image
self.process_image_button.pack(side="top")
# 创建图像显示区域
self.image_canvas = tk.Canvas(self, width=500, height=500)
self.image_canvas.pack(side="top")
# 创建图像保存按钮
self.save_image_button = tk.Button(self)
self.save_image_button["text"] = "Save Image"
self.save_image_button["command"] = self.save_image
self.save_image_button.pack(side="top")
def select_image(self):
# 打开文件选择对话框
file_path = filedialog.askopenfilename()
# 加载图片并显示在界面上
self.image = Image.open(file_path)
self.image = self.image.resize((500, 500), Image.ANTIALIAS)
self.photo_image = ImageTk.PhotoImage(self.image)
self.image_canvas.create_image(0, 0, anchor="nw", image=self.photo_image)
def process_image(self):
# 获取选择的数字数量
number_of_digits = int(self.number_of_digits_var.get())
# 将图片转换为灰度图像
gray_image = cv2.cvtColor(np.array(self.image), cv2.COLOR_RGB2GRAY)
# 对图像进行二值化处理
_, binary_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# 进行连通区域分割,并获取每个连通区域的外接矩形
_, contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
bounding_rects = [cv2.boundingRect(contour) for contour in contours]
# 根据数字数量将外接矩形进行排序,从左到右、从上到下
bounding_rects = sorted(bounding_rects, key=lambda x: (x[1], x[0]))
# 将每个数字的外接矩形提取出来,并进行图像预处理
digit_images = []
for bounding_rect in bounding_rects[:number_of_digits]:
x, y, w, h = bounding_rect
digit_image = gray_image[y:y+h, x:x+w]
digit_image = cv2.resize(digit_image, (28, 28))
digit_image = cv2.GaussianBlur(digit_image, (3, 3), 0)
digit_image = cv2.adaptiveThreshold(digit_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
digit_images.append(digit_image)
# 将处理后的数字图像显示在界面上
processed_image = np.hstack(digit_images)
self.processed_image = Image.fromarray(processed_image)
self.processed_image = self.processed_image.resize((500, 100), Image.ANTIALIAS)
self.processed_photo_image = ImageTk.PhotoImage(self.processed_image)
self.image_canvas.create_image(0, 500, anchor="nw", image=self.processed_photo_image)
def save_image(self):
# 打开保存文件对话框
file_path = filedialog.asksaveasfilename()
# 将处理后的数字图像保存到文件中
self.processed_image.save(file_path)
root = tk.Tk()
app = CharacterSegmentationApp(master=root)
app.mainloop()
```
阅读全文