改善BackgroundSubtractorMOG2和findContours

时间:2016-02-03 21:02:34

标签: python python-2.7 opencv

我试图使用固定相机跟踪足球场上的球员。 BackgroundSubtractorMOG2和findContours导致检测到许多小轮廓。如何更改BackgroundSubtractorMOG2 / findContours的结果,以便一个形状(一个玩家)产生一个我可以跟踪的轮廓?

这是我使用的代码:

import numpy as np
import cv2

cap = cv2.VideoCapture("images/Keeper.mov")
history = 30   # or whatever you want it to be
accelerate = 5

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
        fgbg = cv2.BackgroundSubtractorMOG2()

while(1):
    for i in (1, accelerate):
        ret, frame = cap.read()

    fgmask = fgbg.apply(frame, learningRate=1.0/history)

    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
    cv2.imshow('frame',fgmask)

    h, w = fgmask.shape[:2]

    contours0, hierarchy = cv2.findContours( fgmask.copy(),
                       cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = [cv2.approxPolyDP(cnt, 3, True) for cnt in contours0]

    vis = np.zeros((h, w, 3), np.uint8)
    levels = 17
    cv2.drawContours( vis, contours, (-1, 3)[levels <= 0],
                      (128,255,255),-1, cv2.CV_AA, hierarchy, abs(levels) )
    cv2.imshow('contours', vis)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

0 个答案:

没有答案