当前位置:   article > 正文

python+opencv目标跟踪_静态背景小目标跟踪 python

静态背景小目标跟踪 python

test1.py

  1. import cv2
  2. """
  3. 背景和需要跟踪的物体差异很大
  4. """
  5. # 获取视频
  6. video = cv2.VideoCapture('../opencv/20210423_164452.mp4')
  7. # 生成椭圆结构元素
  8. es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 4))
  9. # 设置背景帧
  10. background = None
  11. while True:
  12. # 读取视频每一帧
  13. ret, frame = video.read()
  14. print(ret)
  15. # 获取背景帧
  16. if background is None:
  17. # 将视频的第一帧图像转为灰度图
  18. background = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  19. # 对灰度图进行高斯模糊,平滑图像
  20. background = cv2.GaussianBlur(background, (21, 21), 0)
  21. continue
  22. if ret:
  23. # 将视频的每一帧图像转为灰度图
  24. gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  25. # 对灰度图进行高斯模糊,平滑图像
  26. gray_frame = cv2.GaussianBlur(gray_frame, (21, 21), 0)
  27. # 获取当前帧与背景帧之间的图像差异,得到差分图
  28. diff = cv2.absdiff(background, gray_frame)
  29. # 利用像素点值进行阈值分割,得到一副黑白图像
  30. diff = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]
  31. # 膨胀图像,减少错误
  32. diff = cv2.dilate(diff, es, iterations=2)
  33. # 得到图像中的目标轮廓
  34. cnts, hierarchy = cv2.findContours(diff.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  35. for c in cnts:
  36. if cv2.contourArea(c) < 1500:
  37. continue
  38. # 绘制目标矩形框
  39. (x, y, w, h) = cv2.boundingRect(c)
  40. cv2.rectangle(frame, (x+2, y+2), (x+w, y+h), (0, 255, 0), 2)
  41. # 显示检测视频
  42. cv2.namedWindow('contours', 0)
  43. cv2.resizeWindow('contours', 600, 400)
  44. cv2.imshow('contours', frame)
  45. # 显示差异视频
  46. cv2.namedWindow('diff', 0)
  47. cv2.resizeWindow('diff', 600, 400)
  48. cv2.imshow('diff', diff)
  49. if cv2.waitKey(1) & 0xff == ord('q'):
  50. break
  51. else:
  52. break
  53. # 结束
  54. cv2.destroyAllWindows()
  55. video.release()

test2.py

背景分割器

  1. import cv2
  2. # 获取视频
  3. video = cv2.VideoCapture('../opencv/20210423_164452.mp4')
  4. # KNN背景分割器,设置阴影检测
  5. bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
  6. while True:
  7. # 读取视频每一帧
  8. ret, frame = video.read()
  9. # 计算视频的前景掩码
  10. if ret:
  11. fgmask = bs.apply(frame)
  12. # 图像阈值化
  13. th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
  14. # 膨胀图像,减少错误
  15. dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
  16. # 得到图像中的目标轮廓
  17. contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  18. for c in contours:
  19. if cv2.contourArea(c) > 1600:
  20. # 绘制目标矩形框
  21. (x, y, w, h) = cv2.boundingRect(c)
  22. cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 0), 2)
  23. # 显示差异视频
  24. cv2.imshow('mog', fgmask)
  25. # cv2.imshow('thresh', th)
  26. # 显示检测视频
  27. cv2.imshow('detection', frame)
  28. if cv2.waitKey(1) & 0xff == ord('q'):
  29. break
  30. else:
  31. break
  32. video.release()
  33. cv2.destroyAllWindows()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/78519
推荐阅读
相关标签
  

闽ICP备14008679号