opencv运动跟踪器与识别

时间:2016-05-05 05:42:02

标签: python opencv tracking opencv-contour

目前,以下脚本完全正常,但我现在想给每个矩形边框标识符。

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)

例如,给出以下图像:

我希望能够将四个矩形边界中的每一个都识别为一个对象。 (即最左边是女王钻石卡的装订盒,最右边是心灵王牌的装订盒)

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

1 个答案:

答案 0 :(得分:2)

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