Ask Your Question
1

Detection and counting cars - OpenCV and C ++

asked 2015-08-27 13:29:52 -0500

Jose gravatar image

updated 2015-08-27 15:16:56 -0500

Hi, I'm working on software for vehicle control, I am using OpenCV and C ++ to create the software. for detection of the cars I am using a classifier haarcascade .XML and detects vehicles.

now my problem is in the count, I failed even stable algorithm that accounts cars passing through the streets. you could guide me in this part. please....

Investigating found another way to detect cars in OpenCV and C ++, as are the methods of substraction but equally do not understand how to establish an ROI to detect contarlo..o car and so on. I can create the rectangle over the image but do not understand how to establish a counter in the area

could lead me please

image description

edit retag flag offensive close merge delete

Comments

Could you specify what is your exact problem with the counting? Do you need to count cars over a single frame or count unique cars over a certain period of time? What do you consider stable/unstable?

LorenaGdL gravatar imageLorenaGdL ( 2015-08-27 14:13:21 -0500 )edit

I have to count the cars passing on the street, at first through a haardcascade to detect cars and based on that count, but the XML was not very accurate and had the same car several times, but now I'm using subtraction background and the need to count the number of cars, I read that the background subtraction method and other methods I can set an accountant, but I'm new to this type of programming and do not understand how to do this. I know how to make substraci├│n and draw a rectangle over the area you want to tell, but I do not understand how to count

Jose gravatar imageJose ( 2015-08-27 15:10:48 -0500 )edit

If you know how to draw rectangles over all the wanted moving objects/contours, just count how many rectangles you draw every frame. To be honest I'm still not sure if I've fully understood your problem...

LorenaGdL gravatar imageLorenaGdL ( 2015-08-27 15:18:03 -0500 )edit

I need to count the number of cars passing

Jose gravatar imageJose ( 2015-08-27 15:20:38 -0500 )edit

No how to draw rectangles on each object that moves. only static rectangles are drawn. Please could guide me. I like to draw rectangles on cars, or do to count the number of cars passing

Jose gravatar imageJose ( 2015-08-27 15:22:59 -0500 )edit

google is your friend

LBerger gravatar imageLBerger ( 2015-08-27 21:59:09 -0500 )edit

1 answer

Sort by ┬╗ oldest newest most voted
6

answered 2015-08-27 22:14:29 -0500

LorenaGdL gravatar image

Based on the info and the images that you added (they're not the best ones...), my first approach would be the following:

  1. For a 1-lane road, define two windows (entry and exit areas)
  2. Capture a frame, apply background subtraction and get the foreground mask (moving objects)
  3. Monitor moving objects over the previous windows: first, consider there's a moving object in the window if there a more than certain number of white pixels in the window (any other criteria may be valid); second, consider that a car entered/exited the road if it is detected over certain consecutive number of frames (it is required to know the mean speed of the vehicles to approximate the mean number of frames the car will be in the window).
  4. Update corresponding counters: total number of cars = cars that entered - cars that exited

Additional features/steps may be implemented:

  • Morphology operations (erode, dilate) on the foreground mask
  • "Timers": define a minimum and/or maximum time to cross the road (to pass between the entry and the exit window). This will prevent false detections (for example, you detect a car at frame 14. If 50 frames later (maximum time to cross) you haven't detected a car exiting, automatically decrease by 1 the number of cars)

Limitations - this approach won't work properly or won't work at all if:

  • There is not enough movement (traffic jams, traffic lights in the path, possibility to park in that road)
  • Longer vehicles than expected enter the road. For example, a truck is likely to be counted as 2 different cars

Some very basic code to start from might be the following (haven't tested it). Of course you will need to tweak parameters and see what works and what does not.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
#include <iostream>

using namespace cv;
using namespace std;

void main(){
    VideoCapture cap(0);
    BackgroundSubtractorMOG2 mog;
    Mat frame, foreground;

    //Define counting windows
    Rect in_window = Rect(x1, y1, width1, height1);       //fill with coordinates of entry window
    Rect out_window = Rect(x2, y2, width2, height2);     //fill with coordinates of exit window

    //Initialize counters
    int numberCars = 0;         //total number of cars in a frame
    int numberFramesIn = 0;     //number of consecutive frames a car is detected in the entry window 
    int numberFramesOut = 0;    //number of consecutive frames a car is detected in the exit window 

    while (true){
        cap >> frame;               //capture frame
        mog(frame, foreground);     //get and store foreground mask (moving objects)

        //--For entry window--
        int whitePixels = countNonZero(foreground(in_window));  //Count white pixels in window
        //If number is higher than a threshold, a car is in the window (here a car is detected 
        //if more than the 80% of the window is white. You can play around/choose any other criteria)
        if (whitePixels > (0.8 * in_window.width * in_window.height)){
            numberFramesIn++;           //update counter
            //If a car is detected over 3 (or whatever) consecutive frames, then it entered the road
            if (numberFramesIn > 3){    
                numberCars++;
            }
        }
        else {                      //If a car is ...
(more)
edit flag offensive delete link more

Comments

Thank you very much for your help. Beam I'll try my luck to do what I'm told. Thank you very much , I'm new to this type of programming and it is nice to find places like this and people like you willing to share their knowledge

Jose gravatar imageJose ( 2015-08-28 08:36:39 -0500 )edit

Excuse my ignorance. But how do I determine coordinates of the object?

because before it was used a "haardcascade" and to the classification and detection, but now with this background subtraction and do not know how

Jose gravatar imageJose ( 2015-08-28 09:14:26 -0500 )edit
1

This is a system just for counting cars because that's what you said you needed, not detecting/tracking them in the traditional way (bounding box). If you need such a system, @LBerger gave you a link before with a full package to accomplish that.

LorenaGdL gravatar imageLorenaGdL ( 2015-08-28 09:29:48 -0500 )edit

yeah, that's what I need, a count of cars, but it gives me error

Error C4700: local variable 'y2' used uninitialized

Error C4700: local variable 'x2' used uninitialized

Error C4700: local variable 'y1' used uninitialized

Error C4700: the local variable x1 is used uninitialized

Error C4700: local variable 'Width1' used uninitialized

Error C4700: local variable 'height1' used uninitialized

Error C4700: local variable 'width2' used uninitialized

Error C4700: local variable 'height2' used uninitialized

I do not understand how to initialize, I tried with various values, when 0 not count

Jose gravatar imageJose ( 2015-08-28 09:58:43 -0500 )edit
1

Oh, you meant the windows coordinates! Yes, yes, that x1, y1, etc was just to point out where you should write the appropiate coordinates. You have to manually set the corresponding entry and exit areas according to your image. For example, Rect(0,0, 25,25) if your window was located at the left-top part of the image (this is not your case but...)

LorenaGdL gravatar imageLorenaGdL ( 2015-08-28 10:02:05 -0500 )edit

OK, but how do you know which are the coordinates you want to use. that I can do to identify areas to work.

before drawing a rectangle like this: cvRectangle (imgactual, Point (640, 400), Point (480-150, 600-150), CV_RGB (255, 0, 0), 3);

but these values will not work for me, it gives me error

Jose gravatar imageJose ( 2015-08-28 10:22:00 -0500 )edit

I am using a 640 x 480 window, use the reference coodenades of

rect (0,0,640,480)

but does not count anything. and also tried other values, but I manage to identify the correct coordinates varoles

Jose gravatar imageJose ( 2015-08-28 10:54:09 -0500 )edit
1

You clearly didn't understand the proposed workflow. You need to define two areas (windows, rectangles) corresponding to the start and the end of the road, so that every time a car passes over the area you can say it entered/exited the road. How to know the coordinates? Easy, look at your image, identify your road, and mark the entry and exit. I can't do that for you. Also, I'd recommend you to search in the documentation if you don't know how to use the basic OpenCV structures and functions. We're here to help, but we expect some effort too

LorenaGdL gravatar imageLorenaGdL ( 2015-08-28 11:04:22 -0500 )edit

Ok, thanks for all your help, I will read the information and hope to achieve understand the methods you're saying. Thanks for everything and I hope to count on your support in future doubts

Jose gravatar imageJose ( 2015-08-28 13:26:56 -0500 )edit

hi, Can u explain how to identify the in window and out window weight and height... Rect in_window = Rect(x1, y1, width1, height1);
Rect out_window = Rect(x2, y2, width2, height2);

it is very helpful to me to continue my research ... :)

sinthujan gravatar imagesinthujan ( 2016-05-20 08:05:18 -0500 )edit
Login/Signup to Answer

Question Tools

3 followers

Stats

Asked: 2015-08-27 13:29:52 -0500

Seen: 9,829 times

Last updated: Jul 20 '16