用Python编写一个GUI主界面,主界面有4个数字按钮的总功能表,点击第一个数字按钮实现页面跳转进入该数字页面下的功能表1,功能表1可以实现打开图像并分别对其进行彩色转灰度图,调暗、调亮、直方图均衡化;点击第二个数字按钮实现页面跳转进入该数字页面下的功能表2,并可以实现打开图像并分别对其进行图像平移、旋转、仿射、插值缩放、透视等;点击第三个数字按钮实现页面跳转进入该数字页面下的功能表3,功能表3可以实现打开图像并分别对其进行加噪、去噪点;点击第4个数字按钮实现页面跳转进入该数字页面下的功能表4,功能表4可以实现打开图像并分别对其进行各种边缘检测
时间: 2023-06-19 09:08:49 浏览: 129
好的,这个任务可以使用Python的GUI库Tkinter来完成。以下是一个简单的实现:
```python
import tkinter as tk
from tkinter import filedialog
import cv2
import numpy as np
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("数字图像处理工具")
self.geometry("400x300")
self.create_widgets()
def create_widgets(self):
# 创建数字按钮
self.button1 = tk.Button(self, text="1", width=10, height=5, command=self.page1)
self.button1.grid(row=0, column=0, padx=5, pady=5)
self.button2 = tk.Button(self, text="2", width=10, height=5, command=self.page2)
self.button2.grid(row=0, column=1, padx=5, pady=5)
self.button3 = tk.Button(self, text="3", width=10, height=5, command=self.page3)
self.button3.grid(row=1, column=0, padx=5, pady=5)
self.button4 = tk.Button(self, text="4", width=10, height=5, command=self.page4)
self.button4.grid(row=1, column=1, padx=5, pady=5)
def page1(self):
# 创建页面1
page1 = tk.Toplevel(self)
page1.title("功能表1")
page1.geometry("400x300")
# 创建文件选择按钮和图像显示区域
self.file_button = tk.Button(page1, text="选择图像", command=self.select_image)
self.file_button.pack(padx=10, pady=10)
self.image_label = tk.Label(page1)
self.image_label.pack(padx=10, pady=10)
# 创建功能按钮
self.gray_button = tk.Button(page1, text="灰度化", command=self.gray)
self.gray_button.pack(padx=10, pady=10)
self.bright_button = tk.Button(page1, text="调亮", command=self.bright)
self.bright_button.pack(padx=10, pady=10)
self.dark_button = tk.Button(page1, text="调暗", command=self.dark)
self.dark_button.pack(padx=10, pady=10)
self.hist_button = tk.Button(page1, text="直方图均衡化", command=self.hist)
self.hist_button.pack(padx=10, pady=10)
def page2(self):
# 创建页面2
page2 = tk.Toplevel(self)
page2.title("功能表2")
page2.geometry("400x300")
# 创建文件选择按钮和图像显示区域
self.file_button = tk.Button(page2, text="选择图像", command=self.select_image)
self.file_button.pack(padx=10, pady=10)
self.image_label = tk.Label(page2)
self.image_label.pack(padx=10, pady=10)
# 创建功能按钮
self.translate_button = tk.Button(page2, text="平移", command=self.translate)
self.translate_button.pack(padx=10, pady=10)
self.rotate_button = tk.Button(page2, text="旋转", command=self.rotate)
self.rotate_button.pack(padx=10, pady=10)
self.affine_button = tk.Button(page2, text="仿射", command=self.affine)
self.affine_button.pack(padx=10, pady=10)
self.scale_button = tk.Button(page2, text="缩放", command=self.scale)
self.scale_button.pack(padx=10, pady=10)
self.perspective_button = tk.Button(page2, text="透视", command=self.perspective)
self.perspective_button.pack(padx=10, pady=10)
def page3(self):
# 创建页面3
page3 = tk.Toplevel(self)
page3.title("功能表3")
page3.geometry("400x300")
# 创建文件选择按钮和图像显示区域
self.file_button = tk.Button(page3, text="选择图像", command=self.select_image)
self.file_button.pack(padx=10, pady=10)
self.image_label = tk.Label(page3)
self.image_label.pack(padx=10, pady=10)
# 创建功能按钮
self.add_noise_button = tk.Button(page3, text="加噪", command=self.add_noise)
self.add_noise_button.pack(padx=10, pady=10)
self.remove_noise_button = tk.Button(page3, text="去噪点", command=self.remove_noise)
self.remove_noise_button.pack(padx=10, pady=10)
def page4(self):
# 创建页面4
page4 = tk.Toplevel(self)
page4.title("功能表4")
page4.geometry("400x300")
# 创建文件选择按钮和图像显示区域
self.file_button = tk.Button(page4, text="选择图像", command=self.select_image)
self.file_button.pack(padx=10, pady=10)
self.image_label = tk.Label(page4)
self.image_label.pack(padx=10, pady=10)
# 创建功能按钮
self.edge_detection_button = tk.Button(page4, text="边缘检测", command=self.edge_detection)
self.edge_detection_button.pack(padx=10, pady=10)
def select_image(self):
# 选择图像文件
file_path = filedialog.askopenfilename()
# 读取图像文件
self.image = cv2.imread(file_path)
# 将BGR格式转换为RGB格式
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
# 将图像转换为PIL Image格式
self.image = Image.fromarray(self.image)
# 将图像显示在Label上
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def gray(self):
# 将图像转换为灰度图
self.image = cv2.cvtColor(self.image, cv2.COLOR_RGB2GRAY)
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def bright(self):
# 将图像调亮
self.image = np.uint8(np.clip(1.5 * self.image + 20, 0, 255))
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def dark(self):
# 将图像调暗
self.image = np.uint8(np.clip(0.5 * self.image - 20, 0, 255))
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def hist(self):
# 对图像进行直方图均衡化
self.image = cv2.equalizeHist(self.image)
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def translate(self):
# 图像平移
rows, cols, _ = self.image.shape
M = np.float32([[1, 0, 50], [0, 1, 50]])
self.image = cv2.warpAffine(self.image, M, (cols, rows))
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def rotate(self):
# 图像旋转
rows, cols, _ = self.image.shape
M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)
self.image = cv2.warpAffine(self.image, M, (cols, rows))
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def affine(self):
# 图像仿射变换
rows, cols, _ = self.image.shape
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
M = cv2.getAffineTransform(pts1, pts2)
self.image = cv2.warpAffine(self.image, M, (cols, rows))
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def scale(self):
# 图像插值缩放
rows, cols, _ = self.image.shape
M = cv2.getRotationMatrix2D((cols/2, rows/2), 0, 0.5)
self.image = cv2.warpAffine(self.image, M, (cols, rows))
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def perspective(self):
# 图像透视变换
rows, cols, _ = self.image.shape
pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M = cv2.getPerspectiveTransform(pts1, pts2)
self.image = cv2.warpPerspective(self.image, M, (300, 300))
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def add_noise(self):
# 加入噪声
noise = np.random.randint(0, 256, self.image.shape)
self.image = cv2.add(self.image, noise)
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def remove_noise(self):
# 去除噪声
self.image = cv2.medianBlur(self.image, 5)
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
def edge_detection(self):
# 边缘检测
self.image = cv2.cvtColor(self.image, cv2.COLOR_RGB2GRAY)
self.image = cv2.Canny(self.image, 100, 200)
self.image = Image.fromarray(self.image)
self.photo = ImageTk.PhotoImage(self.image)
self.image_label.config(image=self.photo)
app = App()
app.mainloop()
```
这个程序创建了一个主界面,其中有4个数字按钮,点击数字按钮可以跳转到相应的页面下的功能表。每个功能表中都有一个选择图像的按钮,可以选择要处理的图像。每个功能表中都有若干个功能按钮,可以对图像进行不同的处理。图像处理使用的是OpenCV和PIL两个库。
阅读全文