python gui界面设计:设计一个基于霍夫变换的直线检测系统界面
时间: 2023-12-19 17:07:03 浏览: 36
好的,以下是一个基于Python的GUI界面设计,用于显示基于霍夫变换的直线检测系统的结果:
```python
import cv2
import numpy as np
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
class LineDetectionGUI:
def __init__(self, master):
self.master = master
self.master.title("Line Detection System")
self.img_path = None
self.img = None
self.processed_img = None
self.canvas = tk.Canvas(self.master, width=500, height=500)
self.canvas.pack()
self.load_btn = tk.Button(self.master, text="Load Image", command=self.load_image)
self.load_btn.pack(side=tk.LEFT)
self.process_btn = tk.Button(self.master, text="Process Image", command=self.process_image)
self.process_btn.pack(side=tk.LEFT)
self.quit_btn = tk.Button(self.master, text="Quit", command=self.quit)
self.quit_btn.pack(side=tk.LEFT)
def load_image(self):
self.img_path = filedialog.askopenfilename()
if self.img_path:
self.img = cv2.imread(self.img_path)
self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
self.img = Image.fromarray(self.img)
self.img = ImageTk.PhotoImage(self.img)
self.canvas.create_image(0, 0, anchor='nw', image=self.img)
def process_image(self):
if self.img_path:
gray_img = cv2.cvtColor(cv2.imread(self.img_path), cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_img, 50, 150, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
if lines is not None:
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(self.img, (x1, y1), (x2, y2), (0, 0, 255), 2)
self.processed_img = Image.fromarray(self.img)
self.processed_img = ImageTk.PhotoImage(self.processed_img)
self.canvas.create_image(0, 0, anchor='nw', image=self.processed_img)
def quit(self):
self.master.quit()
if __name__ == "__main__":
root = tk.Tk()
app = LineDetectionGUI(root)
root.mainloop()
```
该界面包括三个按钮:一个用于加载图像,一个用于处理图像,一个用于退出系统。在加载图像后,用户可以点击“Process Image”按钮,将执行霍夫变换,检测图像中的直线并将结果显示在屏幕上。