Ask Your Question
1

counting vehicle number with cascade

asked 2015-12-01 05:42:33 -0600

Berkay gravatar image

updated 2015-12-02 04:04:49 -0600

hi, I wrote this codes to detect vehicles inside each frame in the video. Now I want to count them but I don't know how to do it. Could you show me how, please?

#include "stdafx.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

int main(){

    VideoCapture cap("arabalar2.avi"); // open the video file for reading

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the video file" << endl;
         return -1;
    }

    Mat gray;

    string cascade_file= "C:/Cascades/haarcascades/cars.xml";
    CascadeClassifier cascade;

    if (cascade_file.empty() || !cascade.load(cascade_file))
    {
        cout << "Hata: cascade dosyası bulunamadı!n";
        return -1;
    }

    namedWindow("MyVideo",CV_WINDOW_NORMAL);  

    while(1){

        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
         {
              cout << "Cannot read the frame from video file" << endl;
              break;
         }

        cvtColor(frame, gray, CV_BGR2GRAY);
        equalizeHist(gray, gray);
        vector<Rect> cars;

        cascade.detectMultiScale(gray, cars, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING|CV_HAAR_SCALE_IMAGE, Size(30,30));

        for (int i = 0; i < cars.size(); i++) // draw a rectangle for dedected vehicles
        {
            Rect rect = cars[i];
            rectangle(frame, rect, CV_RGB(0,255,0), 1);
        }

        imshow("MyVideo", frame);

        if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
        {
                cout << "esc key is pressed by user" << endl; 
                break; 
        }

    }
    return 0;
}
edit retag flag offensive close merge delete

Comments

I just want to say that I am going to follow your project closely. I was interested in detecting vehicles in front of my house (too much heavy load traffic). I literally just tweeted asking for ideas. https://twitter.com/rinnamon/status/6...

Are you planning only on counting vehicles or do you plan on looking at direction of travel as well? Ideally, I'd want to have a way of distinguishing cars from buses and semi-trucks, sense direction of travel, and count the number of vehicles to get AADT.
I am just so stoked that someone else is thinking about this. Has car detection using OpenCV already been done before or is this new ground?

rubenk gravatar imagerubenk ( 2015-12-01 11:08:28 -0600 )edit
1

@rubenk I don't want to be cruel but... if you really think that car detection might be a new thing in computer vision, and if you're really surprised that someone else is trying to achieve the same task, then you clearly know very little about the state-of-the-art. Start by getting a grasp of what is going on out there, reading papers and easy implementations

LorenaGdL gravatar imageLorenaGdL ( 2015-12-02 04:24:11 -0600 )edit

1 answer

Sort by » oldest newest most voted
3

answered 2015-12-01 06:36:38 -0600

thdrksdfthmn gravatar image

You have done it yourself. Because you are using the minNeighbors parameter, the cascade is doing a groupRectangles, so the size of the rectangles vector is the number of detections. All you need to do is to print it somewhere:

std::cout << "Found " << cars.size() << " cars" << std::endl;

or display it in the frame using cv::putText:

std::ostringstream str;
str << "Found cars: " << cars.size();
cv::putText(image, cv::Point(10,30), str.str(), CV_FONT_HERSHEY_PLAIN, CV_RGB(0,0,250));
edit flag offensive delete link more

Comments

thanks for respond, ı added this two line num_of_vehicle+=cars.size();cout << "Found " <<num_of_vehicle&lt;&lt; "="" cars"="" &lt;<endl;<="" strong=""> and ı make number of car counted but this line does not give true vehicle number because cascade can perceive the same car several times until that car vanish on the screen

Berkay gravatar imageBerkay ( 2015-12-01 11:09:07 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-12-01 05:42:33 -0600

Seen: 1,070 times

Last updated: Dec 02 '15