Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

counting vehicle number with cascade

hi, ı writed this codes to dedect vehicle which is inside of each frame in the video. now ı want to count but ı don't know how to do, could you show me how ı'll make it by writing several codes?

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++) // bulunan yüz ifadelerini dikdörtgen içine alma
    {
        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;

}

click to hide/show revision 2
No.2 Revision

counting vehicle number with cascade

hi, ı writed this codes to dedect vehicle which is inside of each frame in the video. now ı want to count but ı don't know how to do, could you show me how ı'll make it by writing several codes?

include "stdafx.h"

include <opencv2 imgproc="" imgproc.hpp="">

include <opencv2 objdetect="" objdetect.hpp="">

include <opencv2 highgui="" highgui.hpp="">

include <iostream>

include <vector>

#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;

std; int main(){

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++) // bulunan yüz ifadelerini dikdörtgen içine alma
 {
  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;
}

}

click to hide/show revision 3
No.3 Revision

counting vehicle number with cascade

hi, ı writed I wrote this codes to dedect vehicle which is detect vehicles inside of each frame in the video. now ı Now I want to count them but ı I don't know how to do, could do it. Could you show me how ı'll make it by writing several codes?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++) // bulunan yüz ifadelerini dikdörtgen içine alma
        {
            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;
}

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++) // bulunan yüz ifadelerini dikdörtgen içine alma
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;
}