opencv车道线检测 python做一个界面,并把这个界面和python的代码联系起来
时间: 2024-05-10 18:19:08 浏览: 184
Python+OpenCV实现车道线检测-源码
5星 · 资源好评率100%
好的,以下是一个简单的实现:
1. 首先,安装所需的库:opencv-python, tkinter。
```
pip install opencv-python
pip install tkinter
```
2. 创建一个GUI界面,包括按钮和一个画布,以显示车道线检测的结果。
```
import tkinter as tk
from PIL import Image, ImageTk
class LaneDetectionGUI:
def __init__(self, master):
self.master = master
self.master.title("Lane Detection")
# Create a canvas widget to display the images
self.canvas = tk.Canvas(self.master, width=640, height=480)
self.canvas.pack()
# Add a button to start the lane detection
self.button = tk.Button(self.master, text="Start Lane Detection", command=self.start_lane_detection)
self.button.pack()
def start_lane_detection(self):
# TODO: Add lane detection code here
pass
root = tk.Tk()
app = LaneDetectionGUI(root)
root.mainloop()
```
3. 在 `start_lane_detection` 方法中添加车道线检测的代码。
```
import cv2
def start_lane_detection(self):
# Load the image
img = cv2.imread("test.jpg")
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply a Gaussian blur
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# Apply Canny edge detection
edges = cv2.Canny(blur, 50, 150)
# Apply a region of interest mask
mask = np.zeros_like(edges)
height, width = img.shape[:2]
vertices = np.array([[(0, height), (width/2, height/2), (width, height)]], dtype=np.int32)
cv2.fillPoly(mask, vertices, 255)
masked_edges = cv2.bitwise_and(edges, mask)
# Apply Hough transform to detect lines
rho = 2
theta = np.pi/180
threshold = 100
min_line_length = 50
max_line_gap = 50
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
# Draw the detected lines on the image
line_image = np.zeros_like(img)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(line_image, (x1, y1), (x2, y2), (0, 0, 255), 5)
result = cv2.addWeighted(img, 0.8, line_image, 1, 0)
# Display the result on the canvas
result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
result = Image.fromarray(result)
result = ImageTk.PhotoImage(result)
self.canvas.create_image(0, 0, anchor=tk.NW, image=result)
```
4. 运行代码,选择一张图片并点击“Start Lane Detection”按钮,即可在界面上看到检测到的车道线。
完整代码如下:
```
import tkinter as tk
from PIL import Image, ImageTk
import cv2
import numpy as np
class LaneDetectionGUI:
def __init__(self, master):
self.master = master
self.master.title("Lane Detection")
# Create a canvas widget to display the images
self.canvas = tk.Canvas(self.master, width=640, height=480)
self.canvas.pack()
# Add a button to start the lane detection
self.button = tk.Button(self.master, text="Start Lane Detection", command=self.start_lane_detection)
self.button.pack()
def start_lane_detection(self):
# Load the image
img = cv2.imread("test.jpg")
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply a Gaussian blur
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# Apply Canny edge detection
edges = cv2.Canny(blur, 50, 150)
# Apply a region of interest mask
mask = np.zeros_like(edges)
height, width = img.shape[:2]
vertices = np.array([[(0, height), (width/2, height/2), (width, height)]], dtype=np.int32)
cv2.fillPoly(mask, vertices, 255)
masked_edges = cv2.bitwise_and(edges, mask)
# Apply Hough transform to detect lines
rho = 2
theta = np.pi/180
threshold = 100
min_line_length = 50
max_line_gap = 50
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
# Draw the detected lines on the image
line_image = np.zeros_like(img)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(line_image, (x1, y1), (x2, y2), (0, 0, 255), 5)
result = cv2.addWeighted(img, 0.8, line_image, 1, 0)
# Display the result on the canvas
result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
result = Image.fromarray(result)
result = ImageTk.PhotoImage(result)
self.canvas.create_image(0, 0, anchor=tk.NW, image=result)
root = tk.Tk()
app = LaneDetectionGUI(root)
root.mainloop()
```
阅读全文