基于行人跟踪的例子(卡尔曼滤波+CAMShift)

最近在看目标跟踪的东西,写了一个最基本的opencv实现的程序,没有用到深度的东西,不过这是一个基础,任何深度的东西都是在这上面进行的,所以先搞懂这个demo吧哈哈。

基本工作流程是:

1)检查第一帧

2)检查后面输入的帧,从场景的开始通过背景分割器来识别场景中的行人

3)为每个行人建立ROI,并利用Kalman/CAMShift来跟踪行人ID

4)检查下一帧是否有进入场景的新行人

import cv2
import numpy as np
import os.path as path
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-a", "--algorithm",
    help = "m (or nothing) for meanShift and c for camshift")
args = vars(parser.parse_args())

def center(points):
    x = (points[0][0] + points[1][0] + points[2][0] + points[3][0]) / 4
    y = (points[0][1] + points[1][1] + points[2][1] + points[3][1]) / 4
    return np.array([np.float32(x), np.float32(y)], np.float32)

font = cv2.FONT_HERSHEY_SIMPLEX

class Pedestrian():
    def __init__(self, id, frame, track_window):
        self.id = int(id)
        x, y, w, h = track_window
        self.track_window = track_window
        self.roi = cv2.cvtColor(frame[y:y+h, x:x+w], cv2.COLOR_BGR2HSV)
        roi_hist = cv2.calcHist([self.roi], [0], None, [16], [0, 180])
        self.roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

        self.kalman = cv2.KalmanFilter(4, 2)
        self.kalman.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
        self.kalman.processNoiseCov = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32) * 0.03
        self.measurement = np.zeros((2, 1), np.float32)
        self.prediction = np.zeros((2, 1), np.float32)
        self.term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)       #停止条件
        self.center = None
        self.update(frame)

    def __del__(self):
        print("Pedestrian %d destroyed" %self.id)

    def update(self, frame):
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        back_project = cv2.calcBackProject([hsv], [0], self.roi_hist, [0, 180], 1)

        if args.get("algorithm") == "c":
            ret, self.track_window = cv2.CamShift(back_project, self.track_window, self.term_crit)
            pts = cv2.boxPoints(ret)
            pts = np.int0(pts)
            self.center = center(pts)
            cv2.polylines(frame, [pts], True, 255, 1)

        if not args.get("algorithm") or args.get("algorithm") == "m":
            ret, self.track_window = cv2.meanShift(back_project, self.track_window, self.term_crit)
            x, y, w, h = self.track_window
            self.center = center([[x, y], [x+w, y], [x, y+h], [x+w, y+h]])
            cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 255, 0), 2)

        self.kalman.correct(self.center)
        prediction = self.kalman.predict()
        cv2.circle(frame, (int(prediction[0]), int(prediction[1])), 4, (255, 0, 0), -1)


def main():
    camera = cv2.VideoCapture("E:/768x576.avi")
    history = 20
    bs = cv2.createBackgroundSubtractorKNN()

    cv2.namedWindow("surveillance")
    pedestrians = {}
    firstFrame = True
    frames = 0

    while True:
        grabbed, frame = camera.read()
        if (grabbed is False):
            print ("failed to grab frame.")
            break

        fgmask = bs.apply(frame)        #前景掩码

        if frames < history:
            frames += 1
            continue

        th = cv2.threshold(fgmask.copy(), 127, 255, cv2.THRESH_BINARY)[1]
        th = cv2.erode(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations = 2)
        dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations = 2)
        image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        counter = 0
        for c in contours:
            if cv2.contourArea(c) > 50:
                (x, y, w, h) = cv2.boundingRect(c)
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 1)

                if firstFrame is True:
                    pedestrians[counter] = Pedestrian(counter, frame, (x, y, w, h))
                counter += 1

        for i, p in pedestrians.items():
            p.update(frame)
        firstFrame = False
        frames += 1

        cv2.imshow("surveillance", frame)
        #out.write(frame)
        if cv2.waitKey(110) & 0xff == 27:
            break
    camera.release()

if __name__ == "__main__":
    main()

 

你可能感兴趣的:(Python,opencv)