Ask Your Question

Bounding box not showing?

asked 2017-03-18 12:12:43 -0500

achima gravatar image

updated 2017-03-18 12:30:57 -0500

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)

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

firstFrame = None

while True:
    (grabbed, frame) =

    if not grabbed:

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

    if firstFrame is None:
        firstFrame = gray

    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,

    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:

edit retag flag offensive close merge delete


which opencv version is it, exactly ?

and, shouldn't cv2.rectangle be inside the for loop ? (indentation problem)

berak gravatar imageberak ( 2017-03-18 12:21:24 -0500 )edit

Yes, I think that was an error from copying the code over, its the same indentation as the line above in my code. cv2 version is 3.1.0

achima gravatar imageachima ( 2017-03-18 12:30:24 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-18 12:41:34 -0500

berak gravatar image

updated 2017-03-18 13:22:33 -0500

if you're using opencv3:

>>> help(cv2.findContours)

 findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy

i'd say, cv2.findContours(....)[0] is wrong, and that it has to be: cv2.findContours(....)[1] instead.

(in other words, make sure to feed the contours into the boundingRect(), not the image)

edit flag offensive delete link more


Thanks, I've changed the line to

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1] The bounding box now shows, but there are many boxes that appear on each car, instead of just the one.

achima gravatar imageachima ( 2017-03-18 12:48:09 -0500 )edit

No worries, I've changed the number of iterations and it has gotten rid of most of the small boxes.

achima gravatar imageachima ( 2017-03-18 12:54:59 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-03-18 12:12:43 -0500

Seen: 23 times

Last updated: Mar 18