在python opencv中是否支持BackgroundSubtractorMOG2

时间:2013-04-19 08:37:59

标签: c++ python opencv

对于BackgroundSubtractorMOG2是否有Python绑定,cpp显然有。
我当时认为在python版本之间存在兼容性 openCV和cpp版本。

1 个答案:

答案 0 :(得分:0)

OpenCV 2.x API为您提供了BackgroundSubtractorMOG的Python绑定,只需查看此网页即可 http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html

这是一个例子

import cv2

backsub = cv2.BackgroundSubtractorMOG()

capture = cv2.VideoCapture("Balcony4_Vis.mpg")

if capture:
    while True:
        ret, frame = capture.read()
        if ret:
            fgmask = backsub.apply(frame, None, 0.01)
            contours, hierarchy = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL,
                                               cv2.CHAIN_APPROX_NONE)
            try: hierarchy = hierarchy[0]
            except: hierarchy = []
            for contour, hier in zip(contours, hierarchy):
                (x,y,w,h) = cv2.boundingRect(contour)
                if w > 10 and h > 10:
                    # figure out id
                    best_id = 1
                    .....

                    cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)
                    cv2.putText(frame, str(best_id), (x,y-5), cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (255, 0, 0), 2)
            cv2.imshow("Track", frame)

            key = cv2.waitKey(10)
            if key == ord('q'):
                break