使用opencv的对象计数软件

时间:2019-10-31 15:53:46

标签: python opencv

我正在计算软件。我已成功检测到产品,并希望对其进行计数。为了进行计数,我定义了两行,如果质心在两行之间,则计算乘积。我面临的两个问题。首先,由于我的产品发展很快,因此某些产品不会掉线,所以我增加了行距。其次,在增加行距后,如果产品下降两次,则将其计算两次。我试图将质心放入列表中,如果对产品进行一次计数,则删除了列表中的元素。计数是两次还是错误。请帮忙。 这是视频链接 https://drive.google.com/file/d/1Gqp937OlDpF4_Nx2kSOzHw3A85Xch-HX/view?usp=sharing

import cv2
import math
import numpy as np

width = 0
height = 0
EntranceCounter = 0
ExitCounter = 0
OffsetRefLines = 120
QttyOfContours = 0
area = 0
detect = []
area_min = 100
area_max = 4000

BinarizationThreshold = 70

#Check if an object in entering in monitored zone
##def CheckEntranceLineCrossing(CoordYCentroid, CoorYEntranceLine,CoorYExitLine,area):
##    AbsDistance = abs(CoordYCentroid - CoorYEntranceLine) 
##    if ((y>CoorYEntranceLine) and (y < CoorYExitLine) ):#and area>=5000):
##      return 1
##  #else:
##      #return 0

cap = cv2.VideoCapture('testvideo.avi')


while(cap.isOpened()):
    ret, frame = cap.read()
    height = np.size(frame,0)
    width = np.size(frame,1)


    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    _, threshold = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY)
    low = np.array([0,0,0])
    high = np.array([60, 60, 60])
    image_mask = cv2.inRange(threshold,low, high)
    contours,_= cv2.findContours(image_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    cv2.drawContours(frame, contours, -1, (0,255,0), 3)
    CoorYEntranceLine = int(150-OffsetRefLines)
    CoorYExitLine = int(150+OffsetRefLines)
    cv2.line(threshold, (0,CoorYEntranceLine), (width,CoorYEntranceLine), (0, 255, 0), 2)
    cv2.line(threshold , (0,CoorYExitLine), (width,CoorYExitLine), (0, 0, 255), 2)


    for (i,c)  in enumerate(contours):
        new = True
        QttyOfContours = QttyOfContours+1
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(threshold, (x, y), (x + w, y + h), (0, 0, 255), 2)
        area = cv2.contourArea(c)
        if area< area_min:
            continue
        #find object's centroid
        CoordXCentroid = int((x+x+w)/2)
        CoordYCentroid = int((y+y+h)/2)
        ObjectCentroid = (CoordXCentroid,CoordYCentroid)
        #center = pega_centro(x, y, w, h)
        center = (CoordXCentroid,CoordYCentroid)
        detect.append(center)


        for (x,y) in detect:
            if ((y < CoorYExitLine) and (y>CoorYEntranceLine) and (area>area_min) and (area<area_max)):
                if new ==True:
                    EntranceCounter += 1
                    detect.clear()
                    break


        cv2.putText(frame, str(EntranceCounter), ((CoordXCentroid),(CoordYCentroid-50)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

    cv2.putText(threshold, "Entrances: {}".format(str(EntranceCounter)), (10, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (250, 0, 1), 2)                            

    cv2.imshow('fram1',threshold)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1)==27:
        break

cap.release()
cv2.destroyAllWindows()


1 个答案:

答案 0 :(得分:0)

仅检查计数线与当前和先前对象位置点之间的线的交点。并且,作为额外保证,对跟踪的对象使用“计数”标志。