Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # necessary imports
- import numpy as np # for maths operations
- import cv2 # OpenCV for Python ver. 2.4.10
- import datetime # for time stamp
- # OpenCV can retieve any video source like
- # USB cam - cv2.VideoCapture(0) - where param is no. of USB device
- # Video - cv2.VideoCapture("video.avi") - where param is path to video
- # Video Stream - cv2.VideoCapture("http://192.168.0.101/stream.mpeg") - where param is path to stream
- cap = cv2.VideoCapture("video.avi")
- # taking first frame as a background (background can be retaken later)
- # params are history, number of Gaussian mixtures and baground ratio
- # parameters were chosen experimentally
- bg = cv2.BackgroundSubtractorMOG(3, 100, 0.8)
- # number of founded objects
- prev_contours = -1
- # main loop of program
- while(1):
- # read frame from source
- # ret can be True or False, frame is readed image if ret is True
- ret, frame = cap.read()
- # loop for operations if image was readed correctly
- if ret == True:
- # subtracting background from frame, it will return binary image with:
- # white pixel it was diffrent on background and frame
- # black if pixel was the same on both frames
- fgmask = bg.apply(frame)
- # kernel is matrix full of ones, first param determinate size of it
- kernel_open = np.ones((25,25),np.uint8)
- kernel_close = np.ones((3,3),np.uint8)
- # morphology opening is erosion followed by dilatation
- # removing noise in binary image
- fgmask_open = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel_close)
- # morphology closing is dilatation followed by eresion (oposite for opening)
- # usefull to close small holes inside foreground objects, or small black points in the object
- fgmask_open_close = cv2.morphologyEx(fgmask_open, cv2.MORPH_CLOSE, kernel_open)
- # fiding the contours of objects in binary image
- # params are source image, contour retrieval mode and contour approximation method
- contours, hierarchy = cv2.findContours(fgmask_open_close,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
- # drawing the contours if founded
- if len(contours) > 0:
- # loop for each founded contours
- for i in range(len(contours)):
- cnt = contours[i]
- x,y,w,h = cv2.boundingRect(cnt)
- # drawing rectangle on founded object
- cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
- # generate time stamp if there is new amount of moving objects in frames
- if len(contours)!=prev_contours:
- time_stamp = str(datetime.datetime.now().strftime("%H:%M:%S.%f"))[:-3]
- print time_stamp + ' - contours: ' + str(len(contours))
- prev_contours = len(contours)
- # show frame with image of each operation taken
- cv2.imshow('masked',fgmask) # image with subtracted background
- cv2.imshow('after_open',fgmask_open) # image after open
- cv2.imshow('after_close',fgmask_open_close) # image after close
- cv2.imshow('original',frame) # original image with contours on objects
- # keyboard key support
- k = cv2.waitKey(30) & 0xff
- # ESC to close
- if k == 27:
- break
- #n to take new background image
- if k == 110:
- bg = cv2.BackgroundSubtractorMOG()
- # close aplication if there is no readed image
- else:
- cap.release()
- cv2.destroyAllWindows()
- break
- # close aplication after finishing main loop
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment