0

I'm working with a little program which will use OpenCV + python's background subtraction to count cars. I'm fine with background subtraction, I already have a background image. But when I use cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE), I get way too many contours. While I can do some basic filtering by checking contour area (cv2.contourArea(contour)), as shown in http://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/, not all cars are detected.

I also looked at cv2.groupRectangles(rectList, minNum[, eps]), but I can't seem to create a vector of rectangles (By the way, that's in http://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html, last function).

My code to find contours/draw rectangles:

contours, im2 = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    if MAXAREA >= cv2.contourArea(cnt) >= MINAREA:
        rect = cv2.minAreaRect(cnt)
        points = cv2.cv.BoxPoints(rect)
        cv2.rectangle(img, (int(points[1][0]), int(points[1][1])), (int(points[3][0]), int(points[3][1])), (0, 255, 0), 2)

(MINAREA and MAXAREA are the maximum and minimum areas for the contour to be drawn)

My Question: How can I either group rectangles or use some criteria to draw the correct rectangles (and keep cars from not being recognized)?

Any help is highly appreciated.

dccsillag
  • 919
  • 4
  • 14
  • 25
  • a few example images would help – Miki Dec 23 '15 at 17:43
  • @Miki [background image](https://drive.google.com/file/d/0B7zZpun7melFOFdTZU5adkNNOE0/view?usp=sharing) and [test frame](https://drive.google.com/file/d/0B7zZpun7melFUWdMUGxYVzVMenc/view?usp=sharing) – dccsillag Dec 23 '15 at 17:51
  • [fgmask](https://drive.google.com/file/d/0B7zZpun7melFeS01UnRISDZ0Vzg/view?usp=sharing) – dccsillag Dec 23 '15 at 18:04
  • how many contours should be found there? you could compute ccontourArea and drop contours too small – Micka Dec 23 '15 at 18:09
  • 3 contours. But how am I going to be sure that the contour that I didn't drop really is a car? – dccsillag Dec 23 '15 at 18:24

1 Answers1

2

As i have understood the question.You need to identify the rectangular shape(the car in the actual image) from the image.irrespective of the size.The minAreaRect function will fit a minimum area rectangle over all the contours given to it.This way u cannot identify the rectangle shape.But u can take this minimum rectangle as a template for each contours and match it with that particular contour.based on the matching score you can decide whether it is rectangular shape or not.One more approach you can try is How to detect simple geometric shapes using OpenCV.

Community
  • 1
  • 1
Jijeesh
  • 29
  • 4
  • I need a single box around each object, in this case, a car. The problem is I'm having more than one box for a single object, while tracking through several frames on the video. If this happens, I can't use contours to count the objects. – dccsillag Dec 25 '15 at 12:51
  • I have experienced the multiple contour problem when I was using canny edge detection.then i have taken only the parent contour from the multiple contours of that particular object.if you are experiencing the same issue you can also use the same method.if you check the hierarchy of that particular contour.you can find whether it is parent or not.refer this for better understanding of hierarchy http://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html#gsc.tab=0 – Jijeesh Dec 28 '15 at 04:47
  • How did you call cv2.findContours? Which flags did you use? Did you even use Python? – dccsillag Dec 29 '15 at 15:47
  • yes i have used python for this.contours,hierarchy = cv2.findContours(binaryImage,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) – Jijeesh Dec 31 '15 at 04:47