Sorry that Im new on this and would like to learn how can I select a moving object in the video image where the motion is observed on the subtracted and thresholded background image while selection of object is held on the main video.

import cv2

cap = cv2.VideoCapture('sample11.mp4')
median = None

while True:
    img = cap.read()[1]

    if img is None:
        break

    img = cv2.resize(img, (600, 450))
    grayvideo = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    grayvideo = cv2.GaussianBlur(grayvideo, (35, 35), 0)

    if median is None:
        median = grayvideo

    difference = cv2.absdiff(median, grayvideo)
    _, threshold = cv2.threshold(difference, 35, 255, cv2.THRESH_BINARY)

    threshold = cv2.dilate(threshold, None, iterations=15)

    contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        x1, y1, x2, y2 = cv2.boundingRect(cnt)

        M = cv2.moments(cnt)

        centX = int(M["m10"] / M["m00"])
        centY = int(M["m01"] / M["m00"])

        cv2.putText(img, 'Centroid Coordinates=', (1, 25), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0), 2)
        cv2.putText(img, str(centX), (200, 25), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255, 0, 0), 2)
        cv2.putText(img, ",", (235, 25), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255, 0, 0), 2)
        cv2.putText(img, str(centY), (240, 25), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255, 0, 0), 2)

        cv2.circle(img, (centX, centY), 3, (255, 255, 255), -1)
        cv2.putText(img, "Centroid", (x1, y1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255, 255, 255), 2)

        cv2.rectangle(img, (x1, y1), (x1 + x2, y1 + y2), (255, 255, 255), 2)
        cv2.putText(img, 'Tracking...', (1, 50), cv2.FONT_HERSHEY_DUPLEX, 0.7, (255, 0, 0), 2)

    cv2.imshow('Video', img)
    cv2.imshow('Threshold', threshold)
    cv2.imshow('Absolute Difference', difference)
    cv2.waitKey(30)

cap.release()
cv2.destroyAllWindows()

Now Im just able to see the trace of every motion with cv2.findContours I would like to manually select the object to be tracked but it would be nice if it can use my subtracted image instead of ready to use trackers like MOSSE and CSRT. Many thanks in advance

0

There are 0 answers