loading...
  1. Simple Vehicle Tracking/Detection System (C++) OpenCV | Windows 10 | x64 | Visual Studio 2015

Simple Vehicle Tracking/Detection System (C++) OpenCV | Windows 10 | x64 | Visual Studio 2015

Hello Friends,

I have received a bunch of emails on this special Topic of vehicle detection(Thank you all for your feedback). So, here is the much demanded and simplest Vehicle Detection System using OpenCV.
I noticed that there are many other websites that are offering vehicle detection System but none of them provide it for three specific things:
a) Windows 10
b) 64- bit program
c) OpenCV 3.2 & above

Therefore, i decided to write my own program based on these requirements. OpenCV comes with an in-built trainer as well as detector. If you want to train your own classifier for any object like car, planes etc. you can use OpenCV to create one. Its full details are given here: Cascade Classifier Training.

Here is how the program looks like when it runs:

 

 

Let’s get started. For this program, we are going to be use our same build environment that we created using the following post: Configure OpenCV 3.3 on Windows 10 x64 with Visual Studio 2015

Pre-requisites:
1) OpenCV 3.2 & above build environment
2) Patience !! 🙂

Step I: Launch Visual Studio
-> Create a new project and name it “vehicle tracking”
-> Create a new file “main.cpp” under Source Files of your project
-> Set your build environment from the top navbar of Visual Studio to Win64
-> Right click the properties of your project and add the OpenCV includes and Libraries. if you are unsure on how to do that, follow this tutorial. Configure OpenCV 3.3 on Windows 10 x64 with Visual Studio 2015
Remember, we are looking for following Libs to be added:

opencv_core320.lib
opencv_highgui320.lib
opencv_imgproc320.lib
opencv_imgcodecs320.lib
opencv_video320.lib
opencv_videoio320.lib
opencv_objdetect320.lib

This completes your initial setup.

Step II : Compiling the code
Copy the below source to your main.cpp file and compile it using either Ctl+F5(debug) or simply F5(without debug)

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
 
//Code from pythonopencv.com
//Vehicle detection program
 
#include <opencv2/opencv.hpp>
#include <iostream>
 
const int KEY_SPACE = 32;
const int KEY_ESC = 27;
 
CvHaarClassifierCascade *cascade;
CvMemStorage            *storage;
 
void detect(IplImage *img);
 
int main(int argc, char** argv)
{
	std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;
 
	CvCapture *capture;
	IplImage  *frame;
	int input_resize_percent = 100;
 
	if (argc < 3)
	{
		std::cout << "Usage " << argv[0] << " cascade.xml video.avi" << std::endl;
		return 0;
	}
 
	if (argc == 4)
	{
		input_resize_percent = atoi(argv[3]);
		std::cout << "Resizing to: " << input_resize_percent << "%" << std::endl;
	}
 
	cascade = (CvHaarClassifierCascade*)cvLoad(argv[1], 0, 0, 0);
	storage = cvCreateMemStorage(0);
	capture = cvCaptureFromAVI(argv[2]);
 
	assert(cascade && storage && capture);
 
	cvNamedWindow("video", 1);
 
	IplImage* frame1 = cvQueryFrame(capture);
	frame = cvCreateImage(cvSize((int)((frame1->width*input_resize_percent) / 100), (int)((frame1->height*input_resize_percent) / 100)), frame1->depth, frame1->nChannels);
 
	int key = 0;
	do
	{
		frame1 = cvQueryFrame(capture);
 
		if (!frame1)
			break;
 
		cvResize(frame1, frame);
 
		detect(frame);
 
		key = cvWaitKey(33);
 
		if (key == KEY_SPACE)
			key = cvWaitKey(0);
 
		if (key == KEY_ESC)
			break;
 
	} while (1);
 
	cvDestroyAllWindows();
	cvReleaseImage(&frame);
	cvReleaseCapture(&capture);
	cvReleaseHaarClassifierCascade(&cascade);
	cvReleaseMemStorage(&storage);
 
	return 0;
}
 
void detect(IplImage *img)
{
	CvSize img_size = cvGetSize(img);
	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 detected." << 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("video", img);
}

Step III: Running the program
Once you hit F5, and everything is configured properly, you’ll notice that the program runs and exits immediately. This happens because we have to pass arguments in order to run it properly.
Navigate to the project directory by right clicking on the project and click on “Open in Windows Explorer” and find the project .exe file.

Following is the syntax to run this program.

1
<program_name.exe> <cascade_classifier> <video_name>

In our case, it would be:

1
vehicle_tracking.exe cars.xml video1.mov

Here is the link to download cars.xml .

Also, if you want dummy videos to test, you can use either of the below video files:

video1  video2

Enjoy and happy coding!!





Leave a Reply

Your email address will not be published. Required fields are marked *

Welcome to OpenCV World !! Come as a Guest, stay as a Family