python
复制代码
# 去背景
# 加载视频
import cv2
import numpy as np
cap = cv2.VideoCapture('tracker.mp4')
bgsubmog = cv2.bgsegm.createBackgroundSubtractorMOG()
# 形态学kernel,用于过滤噪声
kernel= cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
## 轮廓最小外界矩形的宽高筛选是否是车辆
min_w = 90
min_h = 90
## 存储图片上的车辆
cars = []
# 检测线的高度, 超参数
line_high = 620
## 线的偏移量,即在line_high(+-)区域内的车辆被计数
offset = 7
## 存储车辆数
carno = 0
def center(x, y, w, h):
'''
根据车辆外接矩形的左上角坐标(x, y)和宽高(w, h)得出举行的对角线交点(cx, cy),根据(cx, cy)与line_high的关系计数。
'''
x1 = int(w/2)
y1 = int(h/2)
cx = int(x) + x1
cy = int(y) + y1
return cx, cy
while True:
ret, frame = cap.read()
if ret == True:
## 彩色图转灰度图
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
## 高斯滤波去噪
blur = cv2.GaussianBlur(gray, (3, 3), 5)
# 1. 获取前景掩码
mask = bgsubmog.apply(blur)
# 2.通过腐蚀操作去掉轮廓小的噪声
erode = cv2.erode(mask, kernel)
## 通过膨胀操作再把保留轮廓还原回来
dilate = cv2.dilate(erode, kernel, iterations=2)
## 闭操作, 去掉轮廓内部的噪声
close = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel, iterations=2)
## 3.查找轮廓,筛选车辆
result, contours, h = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
## 画出检测线
cv2.line(frame, (10, line_high), (1200, line_high), (255, 255, 0), 3)
## 画出轮廓
for (i, c) in enumerate(contours):
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)
## 通过外接矩形的宽高大小来过滤掉小轮廓.保留的轮廓都是车
is_valid = (w >= min_w) and (h >= min_h)
if not is_valid:
continue
## 获取车轮廓的矩形框
cv2.rectangle(frame, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)
## 获取车的中心点.
cpoint = center(x, y, w, h)
cars.append(cpoint)
# 画出中心点
cv2.circle(frame, (cpoint), 5, (0, 0, 255), -1)
# 4.判断汽车是否过线.
for (x, y) in cars:
if y > (line_high - offset) and y < (line_high + offset):
# 计数加1
carno += 1
cars.remove((x, y))
print(carno)
## 打印计数信息
cv2.putText(frame, 'Vehicle Count:' + str(carno), (500, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 5)
cv2.imshow('frame', frame)
key = cv2.waitKey(10)
if key == 'q':
break
cap.release()
cv2.destroyAllWindows()