-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.cpp
138 lines (103 loc) · 3.41 KB
/
Demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <fstream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <C:/Users/rahul/Documents/Visual Studio 2010/Projects/majorProject/major_project_includes.h>
#define DTTMFMT "%S " // For Date and Time- Here only seconds
#define DTTMSZ 10
//---------------------------------------------------------------------------------------------------
using namespace std;
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
// Function Declarations
void detect(IplImage *img);
static char *getDtTm (char *buff);
// MAIN Below
int main(int argc, char **argv)
{
CvCapture *capture = 0;
capture = cvCaptureFromAVI("./dataset/video.avi");
if(!capture)
{
std::cerr << "Cannot open video!" << std::endl;
return 1;
}
cascade = (CvHaarClassifierCascade*) cvLoad("cars3.xml");
storage = cvCreateMemStorage(0);
assert(cascade && storage && capture);
ofstream myfile;
myfile.open ("Statistics.dat");
int resize_factor = 100; // 50% of original image
int sec=0; // Storing time temporarily
char buff[DTTMSZ]; // Buffer of size 10 for storing seconds
sec=atoi(getDtTm(buff)); // Storing corrent time
IplImage *frame_aux = cvQueryFrame(capture);
IplImage *frame = cvCreateImage(cvSize((int)((frame_aux->width*resize_factor)/100) , (int)((frame_aux->height*resize_factor)/100)), frame_aux->depth, frame_aux->nChannels);
// Initializing Background Subtraction Methods
IBGS *bgs;
bgs = new PixelBasedAdaptiveSegmenter;
// Initializing Blob Tracking
cv::Mat img_blob;
BlobTracking* blobTracking;
blobTracking = new BlobTracking;
// Initializing Vehicle Counting Algorithm
VehicleCouting* vehicleCouting;
vehicleCouting = new VehicleCouting;
int key = 0;
while(key != 'q')
{
frame_aux = cvQueryFrame(capture);
if(!frame_aux) break;
cvResize(frame_aux, frame);
cv::Mat img_input(frame);
cv::imshow("input", img_input);
// bgs->process(...) method internally shows the foreground mask image
cv::Mat img_mask;
bgs->process(img_input, img_mask);
if(!img_mask.empty())
{
// Blob tracking each frame
blobTracking->process(img_input, img_mask, img_blob);
// Vehicle counting in each frame
vehicleCouting->setInput(img_blob);
vehicleCouting->setTracks(blobTracking->getTracks());
vehicleCouting->process();
// Haar Classification
detect(frame);
//Writing data to a file
myfile <<(((atoi(getDtTm(buff))-sec)< 0) ?(atoi(getDtTm(buff))+ 60 -sec):(atoi(getDtTm(buff))-sec))<< "\t" <<(vehicleCouting->countAB +vehicleCouting->countBA) << "\n" ;
}
key = cvWaitKey(1);
}
delete vehicleCouting;
delete blobTracking;
delete bgs;
cvDestroyAllWindows();
cvReleaseCapture(&capture);
cvReleaseHaarClassifierCascade(&cascade);
cvReleaseMemStorage(&storage);
cvReleaseImage(&frame);
myfile.close();
return 0;
}
void detect(IplImage *img)
{
CvSize img_size = cvGetSize(img);
CvSeq *object = cvHaarDetectObjects(img,cascade,storage,1.1,1,0,cvSize(0,0),img_size );
//std::cout << "Total: " << object->total << " cars" << std::endl;
for(int i = 0 ; i < ( object ? object->total : 0 ) ; i++)
{
CvRect *r = (CvRect*)cvGetSeqElem(object, i);
cvRectangle(img,
cvPoint(r->x, r->y),
cvPoint(r->x + r->width, r->y + r->height),
CV_RGB(255, 0, 0), 2, 8, 0);
}
cvShowImage("Haar Recognition", img);
}
static char *getDtTm (char *buff)
{
time_t t = time (0);
strftime (buff, DTTMSZ, DTTMFMT, localtime (&t));
return buff;
}