Getting total number of vehicles in a video

asked 2013-06-13 11:54:20 -0500

Krishnananthan gravatar image

Hi friends,

Now I am doing a project based on opencv. One part of the project is getting the total number of object in a video. For that I've found the below piece of code from web. Initially the output showing in the command line. I have got that in a notepad by adding, freopen("out.txt","w",stdout); line to the code.

This is giving the number of vehicles in each frame. But I have to get the total amount of vehicles in the whole video. I've tried to get the each frame vehicle count to a loop and get the sum. But Its not successful. I think its a small trick. But I couldn't get it yet.

void detect(IplImage *img,CvRect *r) { ////CvRect *r; CvSize img_size = cvGetSize(img); ////int count;

CvSeq *object = cvHaarDetectObjects(img,cascade,storage, 1.1, //1.1,//1.5, //-------------------SCALE FACTOR 1, //2 //------------------MIN NEIGHBOURS 0, //CV_HAAR_DO_CANNY_PRUNING cvSize(0,0),//cvSize( 30,30), // ------MINSIZE img_size //cvSize(70,70)//cvSize(640,480) //---------MAXSIZE );

std::cout << "Total: " << object->total << " cars" << std::endl;

for(int i = 0 ; i < ( object ? object->total : 0 ) ; i++) { /int *ptr; int value; *ptr = 9; value = *ptr;/

 CvRect *r = (CvRect*)cvGetSeqElem(object, i);
 ///CvRect fs[object->total];
 cvRectangle(img,cvPoint(r->x, r->y),cvPoint(r->x + r->width, r->y + r->height),CV_RGB(255, 0, 0), 2, 8, 0) ;

////CvPoint* p = CV_GET_SEQ_ELEM( CvPoint, object, i );


cvShowImage("video", img);


Can anyone help me on this? It is must needed for my project.

Thanks in advance.

edit retag flag offensive close merge delete


I didn't understand the question properly. What exactly is the problem you are facing?

vinayverma gravatar imagevinayverma ( 2013-06-14 06:44:35 -0500 )edit

std::cout << "Total: " << object->total << " cars" << std::endl; By using this line I am getting the number of vehicles in a single frame. I want to get the sum of vehicle from the whole video.(i.e. sum of vehicles in all the frames).


Krishnananthan gravatar imageKrishnananthan ( 2013-06-14 12:12:49 -0500 )edit

If you directly sum up the vehicles in all the frames, there would be repetition of vehicles (same vehicle counted multiple times). Is that what you want? Or do you want to count the number of distinct vehicles which were in the view of camera?

vinayverma gravatar imagevinayverma ( 2013-06-17 02:06:38 -0500 )edit

I want to count the number of distinct vehicles which were in the view of camera. But, for temporarily I want to sum up the vehicles in all the frames including repetitive vehicles.

Krishnananthan gravatar imageKrishnananthan ( 2013-06-18 00:59:39 -0500 )edit

If I am not mistaken, you are nowhere adding the vehicles in each frame? you'll hv to sum them up in the for loop.

vinayverma gravatar imagevinayverma ( 2013-06-19 00:55:47 -0500 )edit

You are right. I mentioned that in my question also. I tried to sum that and the output returns the pointer address. And where can i sum the count?

Krishnananthan gravatar imageKrishnananthan ( 2013-06-19 01:22:02 -0500 )edit

I've solved it. Do u know the way to count the number of distinct vehicles in the video?

Krishnananthan gravatar imageKrishnananthan ( 2013-06-21 12:51:24 -0500 )edit

Hello sir, Did u find the method to count the number of distinct vehicles in the video? I am also doing the project related to that, can u help me?

chotu gravatar imagechotu ( 2014-02-04 00:47:23 -0500 )edit

hi am trying to do something similar, need to count the cars passing along a street and give a total of cars, I could help

franchesca peña gravatar imagefranchesca peña ( 2015-09-01 14:44:43 -0500 )edit