带识别功能的opencv运动追踪器

opencv motion tracker with identification

目前,以下脚本工作得很好,但我现在想给每个矩形边界框一个标识符。

while True:
    # grab the current frame and initialize the occupied/unoccupied
    (grabbed, frame) = camera.read()

    if not grabbed:
        break

    # resize the frame, convert it to grayscale, and blur it
    frame = imutils.resize(frame, width=500)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    # if the first frame is None, initialize it
    if firstFrame is None:
        firstFrame = gray
        continue

    # compute the absolute difference between the current frame and
    # first frame
    frameDelta = cv2.absdiff(firstFrame, gray)
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]

    # dilate the thresholded image to fill in holes, then find contours
    # on thresholded image
    thresh = cv2.dilate(thresh, None, iterations=2)
    (_, cnts, _) = cv2.findContours(thresh.copy(),   cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

    # loop over the contours
    for c in cnts:
        # if the contour is too small, ignore it

        if cv2.contourArea(c) < args["min_area"]:
            continue

        # compute the bounding box for the contour, draw it on the frame
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

例如,给定下图:

我希望能够将四个矩形边界中的每一个都识别为一个对象。 (即最左边是方片皇后卡的绑定框,最右边是红心A卡的绑定框)

现在,我对如何实现这一目标感到困惑,并且想知道是否有人可以给我灵感

您需要做的就是使用连续帧之间的差异找到轮廓并在轮廓上循环并命令坐标单独检测每个轮廓,然后您可以标记它们...... 供参考http://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/