当前位置:   article > 正文

python识别对象移动轨迹_如何识别一个人走那边的路python

如何识别一个人走那边的路python

安装cv2

pycharm开发环境。无法单独安装cv2,直接安装opencv-python即可。

 

报错:

cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::contourArea'

方法一,识别两点的坐标

  1. import numpy as np
  2. import cv2 as cv
  3. frameWidth = 640
  4. frameHeight = 480
  5. cap = cv.VideoCapture('.\VID20230519045724.mp4')
  6. size = (frameWidth, frameHeight)
  7. fgbg = cv.createBackgroundSubtractorMOG2()
  8. feature_params = dict(maxCorners=1,qualityLevel=.6,minDistance=25,blockSize=9)
  9. result = cv.VideoWriter('output.avi',
  10. cv.VideoWriter_fourcc(*'MJPG'),
  11. 10, size)
  12. while True:
  13. ret, oframe = cap.read()
  14. if oframe is None:
  15. break
  16. oframe = cv.resize(oframe, (frameWidth, frameHeight))
  17. mask = fgbg.apply(oframe)
  18. frame = cv.morphologyEx(mask,cv.MORPH_OPEN,np.ones((5,5),np.uint8))
  19. ball = cv.goodFeaturesToTrack(frame,**feature_params)
  20. if ball is not None:
  21. x,y = ball[0][0]
  22. cv.circle(oframe,(int(x),int(y)),8,(180,180,0),2)
  23. print("(x,y)=(",x,",",y,")")
  24. cv.imshow("Track", oframe)
  25. result.write(oframe)
  26. key = cv.waitKey(30)
  27. if key == ord('q') or key == 27:
  28. break
  29. result.release()

效果

方法二:

ROI,绘制轨迹

  1. import cv2 as cv
  2. import numpy as np
  3. cap = cv.VideoCapture('720p.mp4')
  4. #cap = cv.VideoCapture('object_tracking_example.mp4')
  5. # 读取第一帧
  6. ret,frame = cap.read()
  7. cv.namedWindow("Demo", cv.WINDOW_AUTOSIZE)
  8. # 选择ROI区域
  9. x, y, w, h = cv.selectROI("Demo", frame, True, False)
  10. track_window = (x, y, w, h)
  11. print("selectROI:x=",x, "y=",y, "w=",w, "h=",h)
  12. # 获取ROI直方图
  13. roi = frame[y:y+h, x:x+w]
  14. hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
  15. #mask = cv.inRange(hsv_roi, (26, 43, 46), (34, 255, 255))
  16. mask = cv.inRange(hsv_roi, (0, 0, 0), (255, 255, 255))
  17. roi_hist = cv.calcHist([hsv_roi],[0],mask,[180],[0,180])
  18. cv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)
  19. tracking_path = []
  20. # 设置迭代的终止标准,最多十次迭代
  21. term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )
  22. while True:
  23. ret, frame = cap.read()
  24. if ret is False:
  25. break;
  26. hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
  27. dst = cv.calcBackProject([hsv],[0],roi_hist,[0,180],1)
  28. # 搜索更新roi区域
  29. ret, track_box = cv.CamShift(dst, track_window, term_crit)
  30. #print(type(ret)," CamShift=",ret)
  31. # 可变角度的矩形框
  32. pts = cv.boxPoints(ret)
  33. pts = np.int0(pts)
  34. cv.polylines(frame, [pts], True, (0, 255, 0), 2)
  35. # 更新窗口
  36. track_window = track_box
  37. #print(track_box)
  38. # 椭圆中心
  39. pt = np.int32(ret[0])
  40. if pt[0] > 0 and pt[1] > 0:
  41. tracking_path.append(pt)
  42. print(pt[0],",",pt[1])
  43. # 绘制跟踪对象位置窗口与对象运行轨迹
  44. #cv.ellipse(frame, ret, (0, 0, 255), 3, 8)
  45. for i in range(1, len(tracking_path)):
  46. cv.line(frame, (tracking_path[i - 1][0], tracking_path[i - 1][1]),
  47. (tracking_path[i][0], tracking_path[i][1]), (0, 255, 0), 2, 6, 0)
  48. # 绘制窗口CAM,目标椭圆图
  49. cv.ellipse(frame, ret, (0, 0, 255), 3, 8)
  50. cv.imshow('Demo',frame)
  51. k = cv.waitKey(50) & 0xff
  52. if k == 27:
  53. break
  54. else:
  55. cv.imwrite(chr(k)+".jpg",frame)
  56. cv.destroyAllWindows()
  57. cap.release()

 效果

 

参考:

OpenCV视频分析-Meanshift、Camshift&运动轨迹绘制 - 知乎

GitHub - woonyee28/Table-Tennis-Ball-Tracker: This project aims to track the movement of a table tennis ball in a video using OpenCV. The process involves filtering out the ball, generating a foreground mask, and then adding circles to the original frames to visualize the ball's movement.

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

闽ICP备14008679号