-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenCVObjectTracker.cpp
276 lines (213 loc) · 5.76 KB
/
OpenCVObjectTracker.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
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 "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/tracking.hpp>
#include <opencv2/tracking/tracking.hpp>
#include <opencv2/videoio.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/core.hpp"
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
using namespace cv;
struct Color32
{
uchar red;
uchar green;
uchar blue;
uchar alpha;
};
String windowName = "Unity OpenCV Object Tracking " ;
VideoCapture capture;
Rect2d bBox;
//open Camera
int StartCam()
{
cout << "In StartCam() ";
capture.open(0);
if (!capture.isOpened())
{
cout << "StarCam() : Camera was not opened" << endl;
return -2;
}
}
extern "C" int __declspec(dllexport) __stdcall Initialise(int& outputCameraWidth, int& outputCameraHeight)
{
cout << "In Initialise()" << endl;
// Open the stream.
capture.open(0);
if (!capture.isOpened())
return -2;
outputCameraWidth = capture.get(CAP_PROP_FRAME_WIDTH);
outputCameraHeight = capture.get(CAP_PROP_FRAME_HEIGHT);
return 0;
}
extern "C" void __declspec(dllexport) __stdcall StopTracking()
{
capture.release();
}
//CSRT tracker
extern "C" void __declspec(dllexport) __stdcall Track_CSRT()
{
cout << "In Track_CSRT()" << endl;
Rect roi;
Mat frame;
Ptr<Tracker> csrtTracker = TrackerCSRT::create();
if (!capture.isOpened())
{
cout << "Track_CSRT() : Camera was not opened" << endl;
}
capture >> frame;
if (frame.empty())
{
cout << "Track_CSRT() : Frames were empty" << endl;
return;
}
//select ROI
roi = selectROI("CSRT Tracker : Select ROI", frame);
if (roi.width == 0 || roi.height == 0)
{
cout << "Track_CSRT(): ROI was not selected, width and height were 0" << endl;
return ;
}
// initialsie CSRT Tracker
csrtTracker->init(frame, roi);
//track object
cout << "Track_CSRT(): Start tracking Process" <<endl;
for (;;)
{
//get frame form the video
capture >> frame;
//stop if no more img data
if (frame.rows == 0 || frame.cols == 0)
{
cout << "Track_CSRT(): Rows and Columns in frame are zero";
return;
}
//update the tracking result
csrtTracker->update(frame, roi);
//draw tracked obj
rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);
//show the tarcked object
imshow("CSRT Tracked Object", frame);
}
}
//GOTURN tracker
extern "C" void __declspec(dllexport) __stdcall Track_GOTURN(bool trackFlag)
{
cout << "In Track_GOTURN()" << endl;
Rect roi;
Mat frame;
Ptr<Tracker> goturnTracker = TrackerGOTURN::create();
if (!capture.isOpened())
{
cout << "Track_GOTURN() : Camera was not opened" << endl;
}
capture >> frame;
if (frame.empty())
{
cout << "Track_GOTURN() : Frames were empty" << endl;
return;
}
//select ROI
roi = selectROI("GOTURN Tracker : Select ROI", frame);
if (roi.width == 0 || roi.height == 0)
{
cout << "Track_GOTURN(): ROI was not selected, width and height were 0" << endl;
return;
}
// initialsie GOTURN Tracker
goturnTracker->init(frame, roi);
//track object
cout << "Track_GOTURN(): Start tracking Process" << endl;
for (;;)
{
//get frame form the video
capture >> frame;
//stop if no more img data
if (frame.rows == 0 || frame.cols == 0)
{
cout << "Track_GOTURN(): Rows and Columns in frame are zero";
return;
}
//update the tracking result
goturnTracker->update(frame, roi);
//draw tracked obj
rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);
//show the tarcked object
imshow("GOTURN Tracked Object", frame);
}
}
//with predefined bounding box
//tracker with predefined ROI from Unity
extern "C" void __declspec(dllexport) __stdcall Track_CSRT_1()
{
cout << "In Track_CSRT_1()" << endl;
Rect roi;
Mat frame;
Rect bBox(200, 400, 100, 400);
Ptr<Tracker> csrtTracker = TrackerCSRT::create();
if (!capture.isOpened())
{
cout << "Track_CSRT() : Camera was not opened" << endl;
}
capture >> frame;
if (frame.empty())
{
cout << "Track_CSRT() : Frames were empty" << endl;
return;
}
// initialsie CSRT Tracker
csrtTracker->init(frame, bBox);
//track object
cout << "Track_CSRT(): Start tracking Process" << endl;
//get frame form the video
capture >> frame;
//stop if no more img data
if (frame.rows == 0 || frame.cols == 0)
{
cout << "Track_CSRT(): Rows and Columns in frame are zero";
return;
}
//update the tracking result
csrtTracker->update(frame, bBox);
//draw tracked obj
rectangle(frame, bBox, Scalar(255, 0, 0), 2, 1);
//show the tarcked object
imshow("CSRT Tracked Object", frame);
}
//with webcam texture passed from Unity
extern "C" void __declspec(dllexport) __stdcall Track_CSRT_2(Color32 **rawImg, int width, int height)
{
cout << "In Track_CSRT_2()" << endl;
Rect roi;
Mat frame;
Rect bBox(200, 400, 100, 400);
Ptr<Tracker> csrtTracker = TrackerCSRT::create();
cout << "In Track_CSRT_2(): before while loop" << endl;
//while(*rawImg)
//{
cout << "In Track_CSRT_2(): in while loop" << endl;
Mat imgCopy;
Mat image(height, width, CV_8SC4, *rawImg);
//image.copyTo(imgCopy);
// initialsie CSRT Tracker
csrtTracker->init(image, bBox);
//track object
cout << "Track_CSRT(): Start tracking Process" << endl;
//stop if no more img data
if (image.rows == 0 || image.cols == 0)
{
cout << "Track_CSRT(): Rows and Columns in current image frame are zero";
return;
}
//update the tracking result
csrtTracker->update(image, bBox);
//draw tracked obj
rectangle(image, bBox, Scalar(255, 0, 0), 2, 1);
//show the tarcked object
//imshow("CSRTTest", imgCopy);
//}
}