Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Bounding box not showing?

I am writing code to be able to detect moving objects in a video. I have blurred the video, computed the difference between the first and current frame, found contours and computed the bounding box. In theory, my code should draw the bounding box over the moving objects, but nothing is showing up. There is no error message either, so I can't see what it is that it incorrect.

Any help is appreciated!

import argparse
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())

# if the video argument is None, then we are reading from webcam
if args.get("video", None) is None:
    camera = cv2.VideoCapture(0)
    time.sleep(0.25)

else:
    camera = cv2.VideoCapture(args["video"])

firstFrame = None

while True:
    (grabbed, frame) = camera.read()

    if not grabbed:
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21,21),0)

    if firstFrame is None:
        firstFrame = gray
        continue

    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)[0]

    for c in cnts:

        (x, y, w, h) = cv2.boundingRect(c)
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)


    cv2.namedWindow("Original", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Original", 747,420)
    cv2.moveWindow("Original", 100, 100)

    cv2.namedWindow("Thresholded", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Thresholded", 747,420)
    cv2.moveWindow("Thresholded", 950, 100)

    cv2.imshow("Original", frame)
    cv2.imshow("Thresholded", frameDelta)
    key = cv2.waitKey(1) & 0xff
    if key == 27:
        break

camera.release()
cv2.destroyAllWindows()

Bounding box not showing?

I am writing code to be able to detect moving objects in a video. I have blurred the video, computed the difference between the first and current frame, found contours and computed the bounding box. In theory, my code should draw the bounding box over the moving objects, but nothing is showing up. There is no error message either, so I can't see what it is that it incorrect.

Any help is appreciated!

import argparse
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())

# if the video argument is None, then we are reading from webcam
if args.get("video", None) is None:
    camera = cv2.VideoCapture(0)
    time.sleep(0.25)

else:
    camera = cv2.VideoCapture(args["video"])

firstFrame = None

while True:
    (grabbed, frame) = camera.read()

    if not grabbed:
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21,21),0)

    if firstFrame is None:
        firstFrame = gray
        continue

    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)[0]

    for c in cnts:

        (x, y, w, h) = cv2.boundingRect(c)
     cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)


    cv2.namedWindow("Original", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Original", 747,420)
    cv2.moveWindow("Original", 100, 100)

    cv2.namedWindow("Thresholded", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Thresholded", 747,420)
    cv2.moveWindow("Thresholded", 950, 100)

    cv2.imshow("Original", frame)
    cv2.imshow("Thresholded", frameDelta)
    key = cv2.waitKey(1) & 0xff
    if key == 27:
        break

camera.release()
cv2.destroyAllWindows()

Bounding box not showing?

I am writing code to be able to detect moving objects in a video. I have blurred the video, computed the difference between the first and current frame, found contours and computed the bounding box. In theory, my code should draw the bounding box over the moving objects, but nothing is showing up. There is no error message either, so I can't see what it is that it incorrect.

Any help is appreciated!

import argparse
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())

# if the video argument is None, then we are reading from webcam
if args.get("video", None) is None:
    camera = cv2.VideoCapture(0)
    time.sleep(0.25)

else:
    camera = cv2.VideoCapture(args["video"])

firstFrame = None

while True:
    (grabbed, frame) = camera.read()

    if not grabbed:
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21,21),0)

    if firstFrame is None:
        firstFrame = gray
        continue

    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)[0]

    for c in cnts:

        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)


    cv2.namedWindow("Original", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Original", 747,420)
    cv2.moveWindow("Original", 100, 100)

    cv2.namedWindow("Thresholded", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Thresholded", 747,420)
    cv2.moveWindow("Thresholded", 950, 100)

    cv2.imshow("Original", frame)
    cv2.imshow("Thresholded", frameDelta)
    key = cv2.waitKey(1) & 0xff
    if key == 27:
        break

camera.release()
cv2.destroyAllWindows()