生成一段opencv检测人行道的代码
时间: 2023-11-09 19:53:12 浏览: 115
好的,以下是一段简单的 OpenCV 代码,用于检测人行道:
```
import cv2
# 加载图像
image = cv2.imread('path/to/image.jpg')
# 将图像转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 对灰度图进行高斯模糊,以去除噪声
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
# 进行 Canny 边缘检测
canny = cv2.Canny(blurred, 100, 200)
# 在图像中查找轮廓
contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 遍历每个轮廓
for contour in contours:
# 计算轮廓的面积
area = cv2.contourArea(contour)
# 如果面积小于某个阈值,就忽略该轮廓
if area < 100:
continue
# 绘制矩形框,表示人行道的位置
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 显示图像
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
希望这段代码可以帮助到您!如果您有其它问题,欢迎随时提出。