BackgroundSubtractor

class MOG2(history=120, var_threshold=250, detect_shadows=True, cuda=False)

MOG2 based background subtractor.

Typical usage:

mog2_process = edgeiq.MOG2()

while True:
        frame = video_stream.read()
        mog_frame = mog2_process.process_frame(frame)
Parameters
  • history (integer) – Length of the history in frames to store before executing a background model update

  • var_threshold (float) – Threshold on the squared Mahalanobis distance between a pixel and the background model distrbution.

  • detect_shadows (boolean) – Parameter defining whether shadow detection should be enabled.

  • cuda (boolean) – Parameter defining whether to use NVIDIA Jetson GPU to perform the background subtraction calculation.

process_frame(frame, learning_rate=- 1)

Process MOG2 background subtraction.

Parameters
  • frame (numpy array of frame) – The video frame to process.

  • learning_rate (float) – The value between 0 and 1 that indicates how fast the background model is learnt. Negative parameter value makes the algorithm use some automatically chosen learning rate. 0 means that the background model is not updated at all, 1 means that the background model is completely reinitialized from the last frame.

Returns

binary image numpy array

get_contours(raw_contours)

Get contours from value returned by cv2.findContours().

Typical usage:

raw_contours = cv2.findContours(binary-frame,
                                cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
contours = edgeiq.get_contours(raw_contours=raw_contours)
Parameters

raw_contours (array of arrays) – Contours returned from cv2.findContours()function. Each contour is stored as a vector of points.

Returns

list of contours numpy arrays

get_boundingboxes(contours)

Get OpenCV bounding boxes for contours.

Parameters

contours (list of numpy arrays) – list of contours numpy arrays

A list of bounding box tuples of the form (x,y,w,h). Where (x,y) is the top-left coordinate of the rectangle and (w,h) is the width and height.

Returns

A list of contour bounding boxes tuples

get_moments(contours)

Get contours centriods from the moments.

Parameters

contours (list of numpy arrays) – list of contours numpy arrays

Method use the contour’s moments to calculate the centriod

Returns

a list of tuples of x y centriod coordinates

class SortingMethod(value)

Contour sorting methods supported by sort function.

LEFT_RIGHT = 'left-to-right'
RIGHT_LEFT = 'right-to-left'
BOTTOM_TOP = 'bottom-to-top'
TOP_BOTTOM = 'top-to-bottom'
get_sorted(method, contours)

Get sorted contours, OpenCV bounding boxes and moments.

Parameters
  • method (SortingMethod) – Indicates how the contours, bounding boxes and moments should be sorted based on orientation in the image.

  • contours (List[ndarray]) – A list of contours numpy arrays

Returns

a sorted tuple of type lists (contours , boundingboxes, moments)