当前位置:   article > 正文

用python实现单目标、多目标、多尺度、自定义特征的KCF跟踪算法_多尺度跟踪

多尺度跟踪

目录

单目标跟踪:

多目标跟踪:

多尺度检测的KCF、自定义所用特征的KCF

值得参考 


单目标跟踪:

直接调用opencv中封装的tracker即可。

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sun Jan 5 17:50:47 2020
  5. 第四章 kcf跟踪
  6. @author: youxinlin
  7. """
  8. import cv2
  9. from items import MessageItem
  10. import time
  11. import numpy as np
  12. '''
  13. 监视者模块,负责入侵检测,目标跟踪
  14. '''
  15. class WatchDog(object):
  16. #入侵检测者模块,用于入侵检测
  17. def __init__(self,frame=None):
  18. #运动检测器构造函数
  19. self._background = None
  20. if frame is not None:
  21. self._background = cv2.GaussianBlur(cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY),(21,21),0)
  22. self.es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
  23. def isWorking(self):
  24. #运动检测器是否工作
  25. return self._background is not None
  26. def startWorking(self,frame):
  27. #运动检测器开始工作
  28. if frame is not None:
  29. self._background = cv2.GaussianBlur(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), (21, 21), 0)
  30. def stopWorking(self):
  31. #运动检测器结束工作
  32. self._background = None
  33. def analyze(self,frame):
  34. #运动检测
  35. if frame is None or self._background is None:
  36. return
  37. sample_frame = cv2.GaussianBlur(cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY),(21,21),0)
  38. diff = cv2.absdiff(self._background,sample_frame)
  39. diff = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]
  40. diff = cv2.dilate(diff, self.es, iterations=2)
  41. image, cnts, hierarchy = cv2.findContours(diff.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  42. coordinate = []
  43. bigC = None
  44. bigMulti = 0
  45. for c in cnts:
  46. if cv2.contourArea(c) < 1500:
  47. continue
  48. (x,y,w,h) = cv2.boundingRect(c)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/78578
推荐阅读
相关标签
  

闽ICP备14008679号