机器视觉实验二:道路车流量计数实验(OpenCV-python代码)

12 篇文章 8 订阅
12 篇文章 3 订阅

一、实验目的

用OpenCV编写一个车辆计数程序,强化对课堂讲授内容如图像腐蚀、轮廓提取、边缘检测、视频读写等知识的深入理解和灵活应用。

二、实验要求

1、用OpenCV编写一个车辆计数程序,对一段视频里道路上的来往车辆进行计数统计,要求避免同一车辆重复统计,并尽量避免漏检、错检;
2、认真撰写实验报告,要求说明实验原理,对实验过程叙述清楚,关键代码给出注释,对实验结果给出合理解释,实验分析部分则需要指出实验结果优劣的原因以及如何进一步提高实验性能的方法或手段。
3、利用python版的OpenCV编写代码。

三、实验过程

import cv2
import numpy as np

min_w = 90
min_h = 90

#检测线的高度
line_high = 550

#线的偏移
offset = 7

#统计车的数量
carno =0

#存放有效车辆的数组
cars = []

def center(x, y, w, h): #计算矩阵中心点
    x1 = int(w/2)
    y1 = int(h/2)
    cx = x + x1
    cy = y + y1

    return cx, cy

cap = cv2.VideoCapture('video.mp4')

bgsubmog =cv2.createBackgroundSubtractorMOG2() #去背景
#形态学kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))

while True:
    ret, frame = cap.read()
    if(ret == True):     

        #灰度
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        #去噪(高斯)
        blur = cv2.GaussianBlur(frame, (3,3), 5)
        #去背影
        mask = bgsubmog.apply(blur)

        #腐蚀, 去掉图中小斑块
        erode = cv2.erode(mask, kernel) 

        #膨胀, 还原放大
        dilate = cv2.dilate(erode, kernel, iterations = 3)

        #闭操作,去掉物体内部的小块
        close = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel)
        close = cv2.morphologyEx(close, cv2.MORPH_CLOSE, kernel)

        cnts, 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(cnts):
            (x,y,w,h) = cv2.boundingRect(c)

            #对车辆的宽高进行判断
            #以验证是否是有效的车辆
            isValid = ( w >= min_w ) and ( h >= min_h) 
            if( not isValid):
                continue

            #到这里都是有效的车 
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0,0,255), 2)
            cpoint = center(x, y, w, h)
            cars.append(cpoint) 
            cv2.circle(frame, (cpoint), 5, (0,0,255), -1)

            for (x, y) in cars:
                if( (y > line_high - offset) and (y < line_high + offset ) ):
                    carno +=1
                    cars.remove((x , y ))
                    print(carno)
        
        cv2.putText(frame, "Cars Count:" + str(carno), (500, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,0,0), 5)
        cv2.imshow('video', frame)
        #cv2.imshow('erode', close)
    
    key = cv2.waitKey(1)
    if(key == 27):
        break

cap.release()
cv2.destroyAllWindows()

四、实验结果

实验结果如图所示,按Esc键退出。
在这里插入图片描述

五、实验分析

1.本实验很好的结合并运用了之前博客讲述的openCV基础知识。从而实现道路车流量计数,如图像的灰度图,图像的去噪,图像处理kernel的构建,图像腐蚀,图像膨胀,图像闭操作、图像背景消除等。
2.具体实现是根据估计定义汽车选框的大小,先去除视频中的背景去除,然后逐帧读取视频,转化为灰度图,并找出连续几帧中通过检测线的物体,并将其框起来,再通过根据物体的尺寸大小判断是否是汽车,如果是计数加一,否则不加以计数。
3.但根据实际代码跑的情况来看,效果并不是太好,代码还不够完善,算法还有很大优化空间,比如对于摩托车算法有时也会计算出来,多个汽车在一起会导致计数错误。具体改进可以加入深度学习技术加入对汽车形体的识别,以及对汽车阴影的去除,从而提高技术的精确度。后续可能会更新加入深度学习技术的代码,尽情期待。

图像的灰度图在之前博客openCV基础知识(三)具体说明,链接为:https://blog.csdn.net/qq_26274961/article/details/121617487
图像的去噪在之前博客openCV基础知识(五)具体说明,链接为:
https://blog.csdn.net/qq_26274961/article/details/121621414
图像处理kernel的构建,图像腐蚀,图像膨胀,图像闭操作等图像的数学形态学在之前博客openCV基础知识(六)具体说明,链接为:https://blog.csdn.net/qq_26274961/article/details/121641637
图像背景消除在之前博客openCV基础知识(八)具体说明,链接为:https://blog.csdn.net/qq_26274961/article/details/121731186

六、代码文件

小程序员将代码文件和相关素材整理到了百度网盘里,因为文件大小基本不大,大家也不用担心限速问题。后期小程序员有能力的话,将在gitee或者github上上传相关素材。
链接:https://pan.baidu.com/s/1Ce14ZQYEYWJxhpNEP1ERhg?pwd=7mvf
提取码:7mvf

  • 16
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
The error message "Failed building wheel for opencv-python Failed to build opencv-python ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects" usually means that there is a problem with the installation of the opencv-python package. One solution is to try installing the package using conda instead of pip. You can create a new conda environment and install opencv-python in that environment using the following commands: ``` conda create --name myenv conda activate myenv conda install -c conda-forge opencv ``` This will create a new environment called "myenv" and activate it, and then install opencv-python in that environment. If you still want to use pip to install opencv-python, you can try upgrading pip to the latest version using the following command: ``` python -m pip install --upgrade pip ``` Then, you can try installing opencv-python again using pip: ``` pip install opencv-python ``` If this still doesn't work, you can try installing the dependencies for opencv-python manually before installing opencv-python itself. The dependencies are numpy and setuptools. You can install them using the following commands: ``` pip install numpy pip install setuptools ``` Once you have installed these dependencies, you can try installing opencv-python again using pip: ``` pip install opencv-python ``` If none of these solutions work, you can try installing a pre-built binary package of opencv-python. You can download the package from the official opencv-python website and install it using pip: ``` pip install opencv_python‑4.2.0.32‑cp37‑cp37m‑win_amd64.whl ``` Replace the package name with the appropriate version and platform for your system.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值