赞
踩
目录
直接调用opencv中封装的tracker即可。
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Sun Jan 5 17:50:47 2020
- 第四章 kcf跟踪
- @author: youxinlin
- """
-
- import cv2
- from items import MessageItem
- import time
- import numpy as np
- '''
- 监视者模块,负责入侵检测,目标跟踪
- '''
- class WatchDog(object):
- #入侵检测者模块,用于入侵检测
- def __init__(self,frame=None):
- #运动检测器构造函数
- self._background = None
- if frame is not None:
- self._background = cv2.GaussianBlur(cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY),(21,21),0)
- self.es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
- def isWorking(self):
- #运动检测器是否工作
- return self._background is not None
- def startWorking(self,frame):
- #运动检测器开始工作
- if frame is not None:
- self._background = cv2.GaussianBlur(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), (21, 21), 0)
- def stopWorking(self):
- #运动检测器结束工作
- self._background = None
- def analyze(self,frame):
- #运动检测
- if frame is None or self._background is None:
- return
- sample_frame = cv2.GaussianBlur(cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY),(21,21),0)
- diff = cv2.absdiff(self._background,sample_frame)
- diff = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]
- diff = cv2.dilate(diff, self.es, iterations=2)
- image, cnts, hierarchy = cv2.findContours(diff.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- coordinate = []
- bigC = None
- bigMulti = 0
- for c in cnts:
- if cv2.contourArea(c) < 1500:
- continue
- (x,y,w,h) = cv2.boundingRect(c)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。