counting vehicle number with cascade
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;
}
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 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