实现TK界面中选择输入单张图片进行基于基于连通区域的字符分割方法分割单张图片多个数字以及图像预处理,处理过程显示并保存的界面设计,以完整的Python代码实现
时间: 2023-06-25 18:07:32 浏览: 79
以下是一个基于Python Tkinter的图像处理界面设计,其中包含选择单张图片、对图片进行预处理和字符分割、显示处理过程和保存处理结果的功能。
```python
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import cv2
import numpy as np
class ImageProcessingApp:
def __init__(self, master):
self.master = master
self.master.title("基于连通区域的字符分割方法")
self.master.geometry("800x600")
self.image = None
self.processed_image = None
self.segmented_images = []
self.original_image_label = tk.Label(self.master)
self.original_image_label.pack(side="left", padx=10, pady=10)
self.processed_image_label = tk.Label(self.master)
self.processed_image_label.pack(side="left", padx=10, pady=10)
self.segmented_images_frame = tk.Frame(self.master)
self.segmented_images_frame.pack(side="left", padx=10, pady=10)
self.select_image_button = tk.Button(self.master, text="选择图片", command=self.select_image)
self.select_image_button.pack(side="top", padx=10, pady=10)
self.preprocess_button = tk.Button(self.master, text="预处理", command=self.preprocess_image)
self.preprocess_button.pack(side="top", padx=10, pady=10)
self.segment_button = tk.Button(self.master, text="字符分割", command=self.segment_image)
self.segment_button.pack(side="top", padx=10, pady=10)
self.save_button = tk.Button(self.master, text="保存分割结果", command=self.save_segmented_images)
self.save_button.pack(side="top", padx=10, pady=10)
def select_image(self):
file_path = filedialog.askopenfilename()
if file_path:
self.image = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
self.show_image(self.original_image_label, self.image)
def preprocess_image(self):
if self.image is not None:
# 图像预处理
self.processed_image = cv2.GaussianBlur(self.image, (5, 5), 0)
self.processed_image = cv2.adaptiveThreshold(self.processed_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 11, 2)
self.show_image(self.processed_image_label, self.processed_image)
def segment_image(self):
if self.processed_image is not None:
# 连通区域分割
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(self.processed_image)
for i in range(1, num_labels):
x, y, w, h, area = stats[i]
if area > 100: # 过滤面积太小的连通区域
segmented_image = self.image[y:y + h, x:x + w]
self.segmented_images.append(segmented_image)
# 显示分割结果
for i, segmented_image in enumerate(self.segmented_images):
segmented_image_label = tk.Label(self.segmented_images_frame)
segmented_image_label.pack(side="left", padx=10, pady=10)
self.show_image(segmented_image_label, segmented_image)
def save_segmented_images(self):
if self.segmented_images:
for i, segmented_image in enumerate(self.segmented_images):
cv2.imwrite(f"segmented_image_{i}.jpg", segmented_image)
def show_image(self, label, image):
image = Image.fromarray(image)
image = ImageTk.PhotoImage(image)
label.config(image=image)
label.image = image
if __name__ == "__main__":
root = tk.Tk()
app = ImageProcessingApp(root)
root.mainloop()
```
在这个界面中,用户可以选择单张图片,对图片进行预处理和字符分割,并且可以显示和保存处理结果。其中,预处理包括高斯模糊和自适应阈值二值化,字符分割采用连通区域分割方法,过滤面积太小的连通区域。最终,处理结果以多个Label的形式显示在界面上。
阅读全文