标题:车辆识别去除阴影
只看楼主
秃头预备军
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2022-3-11
结帖率:0
 问题点数:0 回复次数:2 
车辆识别去除阴影
大佬们有什么办法在识别车辆的时候不包括车的影子吗?


程序代码:
import cv2
import numpy as np
from skimage.morphology import remove_small_objects

#实验视频的存放地址
video_path = 'vvvv.mp4'
interval=5
times =0
rate1=0.0
rate2=0.0

min_w = 85
min_h = 85

#检测线的高度
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

#设定显示窗口的大小和名字
cv2.namedWindow('video',cv2.WINDOW_NORMAL)
cv2.resizeWindow('video',1280,720)

cap = cv2.VideoCapture(video_path)

#背景差分
bgsegm = cv2.createBackgroundSubtractorMOG2(detectShadows=True)
mog = cv2.bgsegm.createBackgroundSubtractorMOG()

#形态学kernel,卷积核
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
erode_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 4))
dilate_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))

while True:
    ret, frame = cap.read()
    white=0
    squre=0
    if(ret):
        #灰度
        gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        #去噪(高斯)
        blur = cv2.GaussianBlur(gray, (3,3), 5)

        #去背影
        mask = mog.apply(blur)
        _, thresh = cv2.threshold(mask, 244, 255, cv2.THRESH_BINARY)
        #腐蚀, 去掉图中小斑块
        erode = cv2.erode(mask, erode_kernel,thresh)

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

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

        #从二值图里查找轮廓
        cnts, h = cv2.findContours(close1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        #画一条检测线
        cv2.line(frame, (70, line_high), (1210, line_high), (255, 188, 0), 3)
        # times+=1

        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:  #x,y为红色中心点
                if( (y > line_high - offset) and (y < line_high + offset ) ):
                    carno +=1
                    cars.remove((x , y ))
            squre+=w*h
        if(times%interval==0):
            rate1=squre*1.0/(shape[0]*shape[1])
            rate2=1.0-rate1
        # 设置文本
        cv2.putText(frame, "Vehicles Count:" + str(carno), (400, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,0,0), 5)
        cv2.putText(frame,"White rate:" + str(('%.4f'%rate1)), (400, 120), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 0), 4)
        cv2.putText(frame,"Black rate:" + str(('%.4f'%rate2)), (400, 180), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 0), 4)
        cv2.imshow('video',frame)


    if cv2.waitKey(1)&0xFF==27:
        break

cap.release()
cv2.destroyAllWindows()
搜索更多相关主题的帖子: 识别 times str frame offset 
2022-11-26 09:24
lwy2xxj
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:36
专家分:148
注 册:2019-4-8
得分:0 
加个预测算法
2022-11-29 18:09
Rocket_Pro
Rank: 2
等 级:论坛游民
帖 子:12
专家分:10
注 册:2022-12-17
得分:0 
如果你真的很想去掉影子的话,可以一个像素一个像素的擦掉……
2022-12-18 09:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-510775-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058951 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved