上次写的不好,这次重写一个霍夫变化的框架,里面包括灰度转换、滤波
canny边缘检测、霍夫变换等......
原始图像
python
import cv2
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
%matplotlib inline
def detect_parking_lines(image_path):
# 读取图像
img = cv2.imread(image_path)
# 灰度转换
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 高斯滤波
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# Canny 边缘检测
edges = cv2.Canny(gray, 50, 150)
# Hough 变换检测直线
lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)
# 绘制检测到的直线
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(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 显示结果
cv2.imshow('Detected Parking Lines', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 调用函数进行车位线检测
detect_parking_lines('chessboard.jpg')
识别后图像: