opencv笔记(车辆识别实现)

Posted on 2022-12-06 18:24  呱呱呱呱叽里呱啦  阅读(351)  评论(0编辑  收藏  举报

opencv笔记(车辆计数实现)

注意:更准确的车辆计数实现应考虑深度学习。

基本实现思路

  1. 加载视频
  2. 通过形态学识别车辆
  3. 对车辆进行统计
  4. 显示车辆统计信息

涉及知识

  • 窗口展示
  • 图像/视频加载
  • 基本图形的绘制
  • 车辆识别
    • 基本图像运算与处理
    • 形态学
    • 轮廓查找

分步骤代码

加载视频

import cv2


# 创建窗口对象
cv2.namedWindow('video', cv2.WINDOW_NORMAL)
cap = cv2.VideoCapture('2.mp4')

while 1:
    ok, frame = cap.read()
    if ok:
        # 窗口显示
        cv2.imshow('video', frame)
        key = cv2.waitKey(1)
        if key == 27 or cv2.getWindowProperty('color', cv2.WND_PROP_VISIBLE) < 1.0:
            break
    else:
        break

# 释放资源
cap.release()
cv2.destroyAllWindows()

通过形态学识别车辆

import cv2

# 车辆过滤条件应根据参照物适配,此处仅为演示原理
min_w = 100
min_h = 100

line_high = 200
line_width = 6

cv2.namedWindow('video', cv2.WINDOW_AUTOSIZE)
cap = cv2.VideoCapture('6.ts')
bs = cv2.createBackgroundSubtractorMOG2()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
cars = []
while 1:
    ok, frame = cap.read()
    if ok:
        # 转换绘图图
        cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # 高斯去噪
        blur = cv2.GaussianBlur(frame, (3, 3), 5)
        # 去除背景
        mask = bs.apply(blur)
        # 腐蚀
        erode = cv2.erode(mask, kernel, iterations=2)
        # 膨胀
        dilate = cv2.dilate(erode, kernel, iterations=2)
        # 闭运算
        close = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel)
        close = cv2.morphologyEx(close, cv2.MORPH_CLOSE, kernel)

        # 查找轮廓
        contours, _ = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        # 过滤、绘制轮廓,车辆计数
        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)
            # 过滤
            if w < min_w or h < min_h:
                continue
            # 绘制轮廓
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

        cv2.imshow('video', frame)
        key = cv2.waitKey(42)
        if key == 27:
            break
    else:
        break

# 释放资源
cap.release()
cv2.destroyAllWindows()

对车辆进行统计

import cv2

# 车辆过滤条件应根据参照物适配,此处仅为演示原理
min_w = 100
min_h = 100

# 检测线高度、误差
line_high = 190
line_offset = 5

cv2.namedWindow('video', cv2.WINDOW_AUTOSIZE)
cap = cv2.VideoCapture('6.ts')
bs = cv2.createBackgroundSubtractorMOG2()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
car_count = 0
while 1:
    ok, frame = cap.read()
    if ok:
        # 转换绘图图
        cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # 高斯去噪
        blur = cv2.GaussianBlur(frame, (3, 3), 3)
        # 去除背景
        mask = bs.apply(blur)
        # 腐蚀
        erode = cv2.erode(mask, kernel, iterations=3)
        # 膨胀
        dilate = cv2.dilate(erode, kernel, iterations=3)
        # 闭运算
        close = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel)
        close = cv2.morphologyEx(close, cv2.MORPH_CLOSE, kernel)
        # 查找轮廓
        contours, _ = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        # 绘制检测线
        cv2.line(frame, (0, line_high), (frame.shape[1], line_high), (255, 0, 0), 3)
        # 过滤、绘制轮廓,车辆计数
        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)
            # 过滤
            if w < min_w or h < min_h:
                continue

            # 搜集有效车辆(此处有逻辑限制:车辆不能停止于检测线区域内)
            center = y + h / 2
            if line_high - line_offset < center < line_high + line_offset:
                car_count += 1
                # 绘制轮廓
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        cv2.imshow('video', frame)
        key = cv2.waitKey(42)
        if key == 27:
            break
    else:
        break
# 释放资源
cap.release()
cv2.destroyAllWindows()

显示车辆统计信息

import cv2

# 车辆过滤条件应根据参照物适配,此处仅为演示原理
min_w = 100
min_h = 100

# 检测线高度、误差
line_high = 150
line_offset = 3

cv2.namedWindow('video', cv2.WINDOW_AUTOSIZE)
cap = cv2.VideoCapture('6.ts')
bs = cv2.createBackgroundSubtractorMOG2()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
car_count = 0
while 1:
    ok, frame = cap.read()
    if ok:
        # 转换绘图图
        cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # 高斯去噪
        blur = cv2.GaussianBlur(frame, (3, 3), 3)
        # 去除背景
        mask = bs.apply(blur)
        # 腐蚀
        erode = cv2.erode(mask, kernel, iterations=3)
        # 膨胀
        dilate = cv2.dilate(erode, kernel, iterations=3)
        # 闭运算
        close = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel)
        close = cv2.morphologyEx(close, cv2.MORPH_CLOSE, kernel)
        # 查找轮廓
        contours, _ = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        # 绘制检测线
        cv2.line(frame, (0, line_high), (frame.shape[1], line_high), (255, 0, 0), 3)
        # 过滤、绘制轮廓,车辆计数
        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)
            # 过滤
            if w < min_w or h < min_h:
                continue
            # 搜集有效车辆(此处有逻辑限制:车辆不能停止于检测线区域内,准确计数应该为每辆车提供唯一标识)
            center = y + h / 2
            if line_high - line_offset < center < line_high + line_offset:
                car_count += 1
                # 绘制轮廓
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(frame, f'Cars: {car_count}', (300, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 3)
        cv2.imshow('video', frame)
        key = cv2.waitKey(42)
        if key == 27:
            break
    else:
        break
# 释放资源
cap.release()
cv2.destroyAllWindows()