-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVehicle.cpp
More file actions
276 lines (173 loc) · 6.84 KB
/
Vehicle.cpp
File metadata and controls
276 lines (173 loc) · 6.84 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "Vehicle.h"
const int FRAME_HEIGHT = 360;
const int FRAME_WIDTH = 640;
Vehicle::Vehicle()
{
}
void Vehicle::trackVehicles()
{
Blob _blob;
vector <Blob> _Blob;
//Mat object to store the diffrence of the frame
Mat diffFrame;
Mat threshedImg;
//Mat object to grab the frames from the video
Mat videoFrame2;
//to store the copies of the frame
Mat videoFrame2Copy, videoFrame1Copy,threshedImgCopy;
_video >> videoFrame2;
//resize the video to 640x360;
resize(videoFrame2, videoFrame2, Size(FRAME_WIDTH, FRAME_HEIGHT), 0, 0, INTER_CUBIC);
//copy the frame for other use
videoFrame2Copy = videoFrame2.clone();
videoFrame1Copy = _firstFrame.clone();
cvtColor(videoFrame1Copy, videoFrame1Copy, CV_BGR2GRAY);
cvtColor(videoFrame2Copy, videoFrame2Copy, CV_BGR2GRAY);
GaussianBlur(videoFrame1Copy, videoFrame1Copy, Size(5, 5), 0);
GaussianBlur(videoFrame2Copy, videoFrame2Copy, Size(5, 5), 0);
absdiff(videoFrame2Copy, videoFrame1Copy, diffFrame);
//calcute the FPS
//putText(_firstFrame, _FPS, Point(600, 100), CV_FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 8);
//display the frames
//imshow("Video", _firstFrame);
threshold(diffFrame, threshedImg, 30, 255.0, CV_THRESH_BINARY);
//dilate and erode for better results
dilate(threshedImg, threshedImg, getStructuringElement(CV_SHAPE_RECT, Size(5, 5)));
dilate(threshedImg, threshedImg, getStructuringElement(CV_SHAPE_RECT, Size(5, 5)));
erode(threshedImg, threshedImg, getStructuringElement(CV_SHAPE_RECT, Size(5, 5)));
//clone the threshed image because finding contors will change the image
threshedImgCopy = threshedImg.clone();
//find the contours
findContours(threshedImgCopy, _contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
Mat contoursImg(threshedImgCopy.size(), CV_8UC3, Scalar(0,0,0));
drawContours(contoursImg, _contours, -1, Scalar(255, 255, 255), -1);
vector < vector<Point> > _convexHulls(_contours.size());
////extract the convex hulls out of the contours
for (int j=0; j < _contours.size(); j++) {
convexHull(_contours[j], _convexHulls[j]);
}
convexImg=Mat(_firstFrame.size(), CV_8UC3, Scalar(0, 0, 0));
//extract the cars out of the video
_extractCars(_convexHulls, _blob,_Blob);
imshow("Contours", contoursImg);
imshow("thresh", threshedImg);
//_Blob.clear();
//_existingBlob.clear();
_firstFrame = videoFrame2.clone();
isFirstFrame = false;
frameCount++;
}
void Vehicle::getFirstframe(VideoCapture &video, vector <Blob> existingBlob)
{
_video = video;
_existingBlob = existingBlob;
//sprintf(_FPS, "%d", _video.get(CV_CAP_PROP_FPS));
Mat firstFrame;
_video.read(firstFrame);
//resize the video to 640x360;
resize(firstFrame, firstFrame, Size(FRAME_WIDTH, FRAME_HEIGHT), 0, 0, INTER_CUBIC);
_firstFrame = firstFrame;
isFirstFrame = true;
frameCount = 2;
}
void Vehicle::_extractCars(vector < vector<Point> > &convexHulls, Blob &_blob, vector <Blob> &_Blob)
{
//get the blob and calculate its specifications like height,width etc.
for (int i = 0; i < _contours.size(); i++) {
_blob.getBlobSpecs(convexHulls[i]);
//store the blobs that qualify minimum requiremnet.
if (_blob.bound.area() >= 600 && _blob.boundWidth >= 100 && _blob.boundWidth<250 &&
_blob.AspectRatio >= 0.7&&_blob.AspectRatio <= 1.2&&
_blob.boundHeight > 50&&_blob.boundHeight<=350 && _blob.DiagonalSize > 100.0) {
_Blob.push_back(_blob);
}
}
//clear the convexhull vector to add new values
convexHulls.clear();
for (int i = 0; i < _Blob.size(); i++) {
convexHulls.push_back(_Blob[i].contour);
}
drawContours(convexImg, convexHulls, -1, Scalar(255, 255, 255), -1);
imshow("Blobs", convexImg);
for (int i = 0; i < _Blob.size(); i++) {
rectangle(_firstFrame, _Blob[i].bound, Scalar(0, 255, 0), 2);
}
imshow("Video", _firstFrame);
//try to match the next frame blobs with the current frame blobs
if (isFirstFrame == true) {
for (int i = 0; i < _Blob.size(); i++) {
_existingBlob.push_back(_Blob[i]);
}
}
else {
_matchExistingFrameBlobwWithCurrentFrameBlob(_existingBlob,_Blob);
}
drawBlobInfoOnImage(_existingBlob, _firstFrame);
imshow("Video", _firstFrame);
}
void Vehicle::_matchExistingFrameBlobwWithCurrentFrameBlob(vector <Blob> &existingBlob, vector <Blob> ¤tBlob)
{
for (int i = 0; i < existingBlob.size(); i++) {
//add some boolean
existingBlob[i].blnCurrentMatchFoundOrNewBlob = false;
existingBlob[i].predictNextPosition();
}
for (int i = 0; i < currentBlob.size(); i++) {
int indexOfMinDist = 0;
double minDist = 10000.0;
for (unsigned int i = 0; i < existingBlob.size(); i++) {
if (existingBlob[i].existingStillBeingTracked == true) {
double distance = distanceBetweenPoints(currentBlob[i].centerPositions.back(), existingBlob[i].predictedNextPosition);
if (distance < minDist) {
minDist = distance;
indexOfMinDist = i;
}
}
}
if (minDist < currentBlob[i].DiagonalSize*1.15) {
addBlobToExistingBlobs(currentBlob[i],existingBlob,indexOfMinDist);
}
else {
addNewBlob(currentBlob[i], existingBlob);
}
}
for (int i = 0; i < existingBlob.size(); i++) {
if (existingBlob[i].blnCurrentMatchFoundOrNewBlob == false) {
existingBlob[i].intNumOfConsecutiveFramesWithoutAMatch++;
}
if (existingBlob[i].intNumOfConsecutiveFramesWithoutAMatch >= 5) {
existingBlob[i].existingStillBeingTracked = false;
}
}
}
double Vehicle::distanceBetweenPoints(Point point1, Point point2) {
int intX = abs(point1.x - point2.x);
int intY = abs(point1.y - point2.y);
return(sqrt(pow(intX, 2) + pow(intY, 2)));
}
void Vehicle::addBlobToExistingBlobs(Blob ¤t_blob, vector<Blob> &existing_blob, int minDistIndex)
{
existing_blob[minDistIndex].contour = current_blob.contour;
existing_blob[minDistIndex].bound = current_blob.bound;
existing_blob[minDistIndex].centerPositions.push_back(current_blob.centerPositions.back());
existing_blob[minDistIndex].DiagonalSize = current_blob.DiagonalSize;
existing_blob[minDistIndex].AspectRatio = current_blob.AspectRatio;
existing_blob[minDistIndex].existingStillBeingTracked = true;
existing_blob[minDistIndex].blnCurrentMatchFoundOrNewBlob = true;
}
void Vehicle::addNewBlob(Blob ¤t_blob, vector <Blob> &existing_blob)
{
current_blob.blnCurrentMatchFoundOrNewBlob = true;
existing_blob.push_back(current_blob);
}
void Vehicle::drawBlobInfoOnImage(std::vector<Blob> &blobs, cv::Mat &imgFrame2Copy) {
for (unsigned int i = 0; i < blobs.size(); i++) {
if (blobs[i].existingStillBeingTracked == true) {
cv::rectangle(imgFrame2Copy, blobs[i].bound, Scalar(0,0,255), 2);
int intFontFace = CV_FONT_HERSHEY_SIMPLEX;
double dblFontScale = blobs[i].DiagonalSize / 60.0;
int intFontThickness = (int)std::round(dblFontScale * 1.0);
cv::putText(imgFrame2Copy, std::to_string(i), blobs[i].centerPositions.back(), intFontFace, dblFontScale, Scalar(0,255,0), intFontThickness);
}
}
}