Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

About tracking a centroid of contour and a people counter

I am using python for opencv programming and i'm developing a people counter using it. I've drawn the contours and found its centroid. But i am not able to track the centroids so that i can use their positions in 2 different frames to see if the contour has entered or exited from the room. Please help me with this.

About tracking a centroid of contour and a people counter

I am using python for opencv programming and i'm developing a people counter using it. I've drawn the contours and found its centroid. But i am not able to track the centroids so that i can use their positions in 2 different frames to see if the contour has entered or exited from the room. Please help me with this.

import datetime import numpy as np import cv2 as cv

functions for counter

def EntranceCrossing(y, ent, exi): if (ent-y<10 and y<ent): return="" 1="" else:="" return="" 0="" def="" exitcrossing(y,="" ent,="" exi):="" if="" (y-exi&lt;8="" and="" y="">exi): #there is a possibility that during entry also this'll be calculated return 1 else: return 0

video capture

var=cv.VideoCapture('sample-02.mp4') fgbg = cv.bgsegm.createBackgroundSubtractorMOG() EntranceCounter= 0 ExitCounter= 0

while True:

if grabbed enter loop else break

(grabbed, frame) = var.read()
text = "No Video"
if not grabbed:
    break

adjusting frame size and blurring

frame = cv.resize(frame, ( 480,480 ))
gray= cv.cvtColor(frame,cv.COLOR_BGR2GRAY, cv.CV_8UC1)

resize = cv.GaussianBlur( gray,(21,21),0)

background subtraction

fgmask= fgbg.apply(resize)
ret, th3 = cv.threshold(fgmask ,25,200,cv.THRESH_BINARY+cv.THRESH_OTSU)

smoothing and filling holes

blur = cv.blur(th3,(5,5))
kernel = np.ones((5,5),np.uint8)
dil= cv.dilate( blur,kernel,iterations = 1)

ret, th3 = cv.threshold(dil,0,50,cv.THRESH_BINARY+cv.THRESH_OTSU)

contours and tracking

im2, contours, hierarchy = cv.findContours(th3.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(im2, contours, -1, (200,50,50), 2)
#grab all contours and draw rectangles and their centroids in original frame    
for c in contours:
    if cv.contourArea(c) < 6000:
        continue
    (x,y,w,h)= cv.boundingRect(c)
    cv.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
    Centroid = (int((x + x + w) /2), int((y + y + h) /2))
    YCentroid= (y+y+h)/2
    cv.circle(frame, Centroid, 1, (0, 0, 255), 4)

counter functions

    if (ExitCrossing(YCentroid,350,350)):
        ExitCounter += 1
    if (EntranceCrossing(YCentroid,350,350)):
        EntranceCounter += 1

display the output

cv.line(frame,(0,350),(480,350),(0,255,0),1)
cv.putText(frame, "WentIn:"+format(str(EntranceCounter)),(10,332),cv.FONT_HERSHEY_SIMPLEX,.5,(0,255,0))
cv.putText(frame, "WentOut:"+format(str(ExitCounter)),(10,365),cv.FONT_HERSHEY_SIMPLEX,.5,(0,0,255))
cv.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
           (10, frame.shape[0] - 10), cv.FONT_HERSHEY_SIMPLEX, 0.35, (255, 0, 0), 1)
cv.putText(frame, "Inside:"+format(str(EntranceCounter-ExitCounter)),(10,320),cv.FONT_HERSHEY_SIMPLEX,.5,(0,0,0))
cv.imshow('PeopleCount', frame)


if cv.waitKey(1) & 0xFF==ord('q'):
     break

var.release() cv.destroyAllWindows()

click to hide/show revision 3
None

updated 2018-07-02 05:48:22 -0600

berak gravatar image

About tracking a centroid of contour and a people counter

I am using python for opencv programming and i'm developing a people counter using it. I've drawn the contours and found its centroid. But i am not able to track the centroids so that i can use their positions in 2 different frames to see if the contour has entered or exited from the room. Please help me with this.

import datetime
import numpy as np
import cv2 as cv

functions cv #functions for counter

counter def EntranceCrossing(y, ent, exi): if (ent-y<10 and y<ent): return="" 1="" else:="" return="" 0="" def="" exitcrossing(y,="" ent,="" exi):="" if="" (y-exi&lt;8="" and="" y="">exi): y<ent): return 1 else: return 0 def ExitCrossing(y, ent, exi): if (y-exi<8 and y>exi): #there is a possibility that during entry also this'll be calculated return 1 else: return 0

video capture

0 #video capture var=cv.VideoCapture('sample-02.mp4') fgbg = cv.bgsegm.createBackgroundSubtractorMOG() EntranceCounter= 0 ExitCounter= 0

0 while True:

if True: #if grabbed enter loop else break

break
(grabbed, frame) = var.read()
 text = "No Video"
 if not grabbed:
 break

adjusting #adjusting frame size and blurring

blurring
frame = cv.resize(frame, ( 480,480 ))
 gray= cv.cvtColor(frame,cv.COLOR_BGR2GRAY, cv.CV_8UC1)
 resize = cv.GaussianBlur( gray,(21,21),0)

background subtraction

#background subtraction
fgmask= fgbg.apply(resize)
 ret, th3 = cv.threshold(fgmask ,25,200,cv.THRESH_BINARY+cv.THRESH_OTSU)

smoothing #smoothing and filling holes

holes
blur = cv.blur(th3,(5,5))
 kernel = np.ones((5,5),np.uint8)
 dil= cv.dilate( blur,kernel,iterations = 1)
 ret, th3 = cv.threshold(dil,0,50,cv.THRESH_BINARY+cv.THRESH_OTSU)

contours and tracking

#contours and tracking
im2, contours, hierarchy = cv.findContours(th3.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
 cv.drawContours(im2, contours, -1, (200,50,50), 2)
 #grab all contours and draw rectangles and their centroids in original frame
 for c in contours:
 if cv.contourArea(c) < 6000:
 continue
  (x,y,w,h)= cv.boundingRect(c)
 cv.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
  Centroid = (int((x + x + w) /2), int((y + y + h) /2))
 YCentroid= (y+y+h)/2
  cv.circle(frame, Centroid, 1, (0, 0, 255), 4)

counter functions

#counter functions
 if (ExitCrossing(YCentroid,350,350)):
  ExitCounter += 1
 if (EntranceCrossing(YCentroid,350,350)):
  EntranceCounter += 1

display #display the output

output
cv.line(frame,(0,350),(480,350),(0,255,0),1)
 cv.putText(frame, "WentIn:"+format(str(EntranceCounter)),(10,332),cv.FONT_HERSHEY_SIMPLEX,.5,(0,255,0))
 cv.putText(frame, "WentOut:"+format(str(ExitCounter)),(10,365),cv.FONT_HERSHEY_SIMPLEX,.5,(0,0,255))
 cv.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
  (10, frame.shape[0] - 10), cv.FONT_HERSHEY_SIMPLEX, 0.35, (255, 0, 0), 1)
 cv.putText(frame, "Inside:"+format(str(EntranceCounter-ExitCounter)),(10,320),cv.FONT_HERSHEY_SIMPLEX,.5,(0,0,0))
 cv.imshow('PeopleCount', frame)
 if cv.waitKey(1) & 0xFF==ord('q'):
 break
var.release()
cv.destroyAllWindows()

var.release() cv.destroyAllWindows()