opencv motion tracker with identification
Question:
Currently, the following script works perfectly fine, but I now want to give each rectangular bound box an identifier.
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)
For example, given the following image:
I want to be able to identify each of the four rectangular bounds as an object. (i.e. the leftmost is a bound box for the Queen of Diamonds card, and the rightmost is a bound box for the Ace of Hearts card)
Right now, I am stumped as to how I could possibly achieve this, and was wondering if anyone could give me inspiration.
Answers:
All you need to do is find the contours using difference between Successive frames and loop across contours and order the coordinates to detect each contour individually and then u can label them …
for reference http://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/
Currently, the following script works perfectly fine, but I now want to give each rectangular bound box an identifier.
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)
For example, given the following image:
I want to be able to identify each of the four rectangular bounds as an object. (i.e. the leftmost is a bound box for the Queen of Diamonds card, and the rightmost is a bound box for the Ace of Hearts card)
Right now, I am stumped as to how I could possibly achieve this, and was wondering if anyone could give me inspiration.
All you need to do is find the contours using difference between Successive frames and loop across contours and order the coordinates to detect each contour individually and then u can label them …
for reference http://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/