(项目笔记)OpenCV目标检测程序

本文介绍了一个基于OpenCV的背景 subtractor MOG2 算法目标检测程序,适用于背景固定的摄像头场景。程序通过比较当前帧与背景模板的像素差识别前景物体,更新并追踪目标位置。当物体离开画面再进入时,会被赋予新的ID,无法记录物体特征。主要步骤包括创建MOG2对象,图像二值化,找寻并标记物体。在背景变化时,算法可能出现误检。
摘要由CSDN通过智能技术生成

本跟踪程序核心为OpenCV里BackgroundSubtractorMOG2()类。该类通过对当前帧和背景模板的每个像素做减法,来区分前景和背景物体。这一算法只适用于背景相对固定的情况,如固定摄像头。对于跟踪小车这种需要实时更新背景的情况不适用

1 Tacker类:标记并更新获取到的目标

import math


class EuclideanDistTracker:
    def __init__(self):
        # Store the center positions of the objects
        self.center_points = {
   }
        # Keep the count of the IDs
        # each time a new object id detected, the count will increase by one
        self.id_count = 0


    # record IDs of objects tracked
    # @param: the rectangular frame that captures an object
    # @return: objects boxes and ids
    def update(self, objects_rect):
        # Objects boxes and ids
        objects_bbs_ids = []

        # Get center point of new object
        # time complexity O(N^2)
        for rect in objects_rect:
            # find the center of the object
            x, y, w, h = rect
            cx = (x + x + w) // 2
            cy = (y + y + h) // 2

            # Find out if that object was detected already
            same_object_detected = False
            # search the objects stored for the same object
            for id, pt in self.center_points.items():
                distance = math.hypot(cx - pt[0], cy - pt[1])

                # if the distance between two objects detected are very close, we consider
                # them to be one object
                if distance < 25:
                    self.center_points[id] = (cx, cy)
                    print(self.center_points)
                    objects_bbs_ids.append([x, y, w, h, id])
                    same_object_detected = True
                    break

            # New object is detected we assign the ID to that object
            if same_object_detected is False:
                self.center_p
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值