opencv实现运动物体检测跟踪
程序员文章站
2022-03-11 10:16:58
...
利用opencv实现运动物体的跟踪
def detect_video(video):
camera = cv2.VideoCapture(video)
history = 1
bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
bs.setHistory(history)
frames = 0
while True:
res, frame = camera.read()
if not res:
break
fg_mask = bs.apply(frame)
if frames < history:
frames += 1
continue
th = cv2.threshold(fg_mask.copy(), 244, 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)
for c in contours:
# 获取矩形框边界坐标
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("track", frame)
cv2.imshow("process", dilated)
if cv2.waitKey(110) & 0xff == 27:
break
camera.release()
下一篇: canvas画八卦图
推荐阅读
-
OPENCV_PYTHON官方文档学习_32.Shi-Tomasi 角点检测 & 适合于跟踪的图像特征
-
【opencv】动态背景下运动目标检测 FAST+SURF+FLANN配准差分 17/12/13更新图片
-
CV之DNN:基于OpenPose的OpenCV利用DNN算法实现对单人体姿态(美女跳舞)实时估计检测
-
python+opencv实现车道线检测
-
Python OpenCV利用笔记本摄像头实现人脸检测
-
python opencv实现图像边缘检测
-
python调用opencv实现猫脸检测功能
-
Layabox 3D游戏开发学习笔记---射线检测,鼠标控制物体运动
-
人脸检测识别opencv+python实现(Haar分类器)
-
Java+opencv3.2.0实现hough直线检测