People counter 6 – Find contours

Again, sorry for the delay between posts, I’ve been busy with other projects :S

So far, we have filtered the video stream so we only get movement:

peoplecount filter1

Now it’s time to detect contours on the frames. This is really simple with openCV’s findCountours function.

Here´s the code:

import numpy as np
import cv2

# http://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a&gsc.tab=0
##http://docs.opencv.org/master/d4/d73/tutorial_py_contours_begin.html#gsc.tab=0

cap = cv2.VideoCapture('peopleCounter.avi') #Open video file
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True) #Create the background substractor
kernelOp = np.ones((3,3),np.uint8)
kernelCl = np.ones((11,11),np.uint8)

while(cap.isOpened()):
ret, frame = cap.read() #read a frame

fgmask = fgbg.apply(frame) #Use the substractor
try:
ret,imBin= cv2.threshold(fgmask,200,255,cv2.THRESH_BINARY)
#Opening (erode->dilate) para quitar ruido.
mask = cv2.morphologyEx(imBin, cv2.MORPH_OPEN, kernelOp)
#Closing (dilate -> erode) para juntar regiones blancas.
mask = cv2.morphologyEx(mask , cv2.MORPH_CLOSE, kernelCl)
except:
#if there are no more frames to show...
print('EOF')
break

_, contours0, hierarchy = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for cnt in contours0:
cv2.drawContours(frame, cnt, -1, (0,255,0), 3, 8)

cv2.imshow('Frame',frame)

#Abort and exit with 'Q' or ESC
k = cv2.waitKey(30) & 0xff
if k == 27:
break

cap.release() #release video file
cv2.destroyAllWindows() #close all openCV windows

 

The important thing here are these lines:

    _, contours0, hierarchy = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    for cnt in contours0:
        cv2.drawContours(frame, cnt, -1, (0,255,0), 3, 8)

 

We give the function our mask, cv2.RETR_EXTERNAL means we only care about external contours (contours within contours will not be detected), and cv2.CHAIN_APPROX_NONE is the algorithm used to “make” the contour (you can change it to another one as a test).

Draw contours is only used to visually appreciate the contours on the image we display.

All of the other lines of the code have been previously explained in the series.

contours