-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
284 lines (222 loc) · 6.71 KB
/
main.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
277
278
279
280
281
282
//Opencv includes
#include <opencv2/core/utility.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/cudaimgproc.hpp"
//Stander includes
#include <math.h>
#include <iostream>
#include <ctype.h>
#include <thread>
#include <queue>
#include <vector>
// ZED includes
#include <sl/Camera.hpp>
//Communication includes
//#include "can_send.h"
//#include "linux_sever.h"
using namespace sl;
using namespace cv;
using namespace std;
//Main data struct
typedef struct {
cv::cuda::GpuMat L;
cv::cuda::GpuMat R;
bool finish;
cv::Mat position;
}IMAGE_PAIR;
vector<IMAGE_PAIR> image_pair_vector;
//Varibales for mouse selected and trackbar
bool backprojMode = false;
bool selectObject = false;
int trackObject = 0;
bool showHist = true;
Point origin;
Rect selection;
int vmin = 10, vmax = 256, smin = 30;
//Image on cpu for showing and drawing result on
cv::Mat image_ocv_L, image_ocv_R;
//Histogram of the object selected
cv::Mat hist;
//Mutex of all threads
Mutex mtx;
// Create a ZED camera object
Camera zed;
// draw a box around object to track. This triggers CAMShift to start tracking
static void onMouse(int event, int x, int y, int, void*)
{
if (selectObject)
{
selection.x = MIN(x, origin.x);
selection.y = MIN(y, origin.y);
selection.width = std::abs(x - origin.x);
selection.height = std::abs(y - origin.y);
selection &= Rect(0, 0, image_ocv_L.cols, image_ocv_L.rows);
}
switch (event)
{
case EVENT_LBUTTONDOWN:
origin = Point(x, y);
selection = Rect(x, y, 0, 0);
selectObject = true;
break;
case EVENT_LBUTTONUP:
selectObject = false;
if (selection.width > 0 && selection.height > 0)
trackObject = -1; // Set up CAMShift properties in main() loop
break;
}
}
//Covert sl mat to cv mat gpu
sl::Mat cvGpuMat2slGpuMat(cv::cuda::GpuMat& input)
{
// Mapping between MAT_TYPE and CV_TYPE
sl::MAT_TYPE type;
switch (input.type()) {
case CV_8UC3: type = MAT_TYPE_8U_C3; break;
case CV_8UC4: type = MAT_TYPE_8U_C4; break;
default: break;
}
// Since cv::Mat data requires a uchar* pointer, we get the uchar1 pointer from sl::Mat (getPtr<T>())
// cv::Mat and sl::Mat will share a single memory structure
return sl::Mat(size_t(input.rows), size_t(input.cols), type, (sl::uchar1*)input.data, input.step, MEM_GPU);
}
cv::cuda::GpuMat slGpuMat2cvGpuMat(sl::Mat& input)
{
// Mapping between MAT_TYPE and CV_TYPE
int cv_type = -1;
switch (input.getDataType()) {
case MAT_TYPE_32F_C1: cv_type = CV_32FC1; break;
case MAT_TYPE_32F_C2: cv_type = CV_32FC2; break;
case MAT_TYPE_32F_C3: cv_type = CV_32FC3; break;
case MAT_TYPE_32F_C4: cv_type = CV_32FC4; break;
case MAT_TYPE_8U_C1: cv_type = CV_8UC1; break;
case MAT_TYPE_8U_C2: cv_type = CV_8UC2; break;
case MAT_TYPE_8U_C3: cv_type = CV_8UC3; break;
case MAT_TYPE_8U_C4: cv_type = CV_8UC4; break;
default: break;
}
// Since cv::Mat data requires a uchar* pointer, we get the uchar1 pointer from sl::Mat (getPtr<T>())
// cv::Mat and sl::Mat will share a single memory structure
return cv::cuda::GpuMat(input.getHeight(), input.getWidth(), cv_type, input.getPtr<sl::uchar1>(MEM_GPU), input.getStepBytes(MEM_GPU));
}
void camshift_thread(cuda::GpuMat& img)
{
cuda::GpuMat hsv;
cuda::cvt
}
void process_thread()
{
}
int main()
{
// Set configuration parameters
InitParameters init_params;
init_params.camera_resolution = RESOLUTION_VGA;
init_params.camera_fps = 100;
init_params.depth_mode = DEPTH_MODE_NONE;
init_params.coordinate_units = UNIT_METER;
waitKey(2000);
// Open the camera
ERROR_CODE err = zed.open(init_params);
if (err != SUCCESS) {
printf("%s\n", errorCode2str(err).c_str());
zed.close();
return 1; // Quit if an error occurred
}
// Set runtime parameters after opening the camera
//zed.setCameraSettings(CAMERA_SETTINGS_SATURATION,8);
//zed.setCameraSettings(CAMERA_SETTINGS_CONTRAST,4);
//zed.setCameraSettings(CAMERA_SETTINGS_GAIN,60);
//zed.setCameraSettings(CAMERA_SETTINGS_EXPOSURE,50);
RuntimeParameters runtime_parameters;
runtime_parameters.sensing_mode = SENSING_MODE_STANDARD;
//Define some variables to be use later and allocate memory
Resolution image_size = zed.getResolution();
cuda::HostMem MEM_L(image_size.height, image_size.width, CV_8UC3, cuda::HostMem::SHARED);
cv::cuda::GpuMat image_cvGPU_L = MEM_L.createGpuMatHeader();
sl::Mat image_zed_L = cvGpuMat2slGpuMat(image_cvGPU_L);
cv::Mat image_cvCPU_L = MEM_L.createMatHeader();
cuda::HostMem MEM_R(image_size.height, image_size.width, CV_8UC3, cuda::HostMem::SHARED);
cv::cuda::GpuMat image_cvGPU_R = MEM_R.createGpuMatHeader();
sl::Mat image_zed_R = cvGpuMat2slGpuMat(image_cvGPU_R);
cv::Mat image_cvCPU_R = MEM_L.createMatHeader();
cv::cuda::Stream stream;
IMAGE_PAIR image_temp;
namedWindow("L", 0);
setMouseCallback("L", onMouse, 0);
createTrackbar("Vmin", "L", &vmin, 256, 0);
createTrackbar("Vmax", "L", &vmax, 256, 0);
createTrackbar("Smin", "L", &smin, 256, 0);
int hsize = 4;
float hranges[] = { 0,180 };
const float* phranges = hranges;
bool stop = false;
char c;
vector<int> a;
while (!stop)
{
if (zed.grab(runtime_parameters) == SUCCESS) {
// Retrieve the left image, depth image in half-resolution
zed.retrieveImage(image_zed_L, VIEW_LEFT, MEM_GPU);
zed.retrieveImage(image_zed_R, VIEW_RIGHT, MEM_GPU);
image_temp.L = image_cvGPU_L;
image_temp.R = image_cvGPU_R;
image_temp.finish = false;
image_temp.position = cv::Mat::zeros(4, 1, CV_32FC1);
while (!mtx.trylock()) {}
image_pair_vector.push_back(image_temp);
mtx.unlock();
}
else
{
cout << "failed" << endl;
break;
}
if (trackObject)
{
if (trackObject < 0)
{
// Object has been selected by user, set up CAMShift search properties once
cv::Mat hsv, mask;
cvtColor(image_cvCPU_L, hsv, COLOR_BGR2HSV);
int _vmin = vmin, _vmax = vmax;
inRange(hsv, Scalar(0, smin, MIN(_vmin, _vmax)),
Scalar(180, 256, MAX(_vmin, _vmax)), mask);
int ch[] = { 0, 0 };
cv::Mat hue;
hue.create(hsv.size(), hsv.depth());
mixChannels(&hsv, 1, &hue, 1, ch, 1);
cv::Mat roi(hue, selection), maskroi(mask, selection);
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
normalize(hist, hist, 0, 255, NORM_MINMAX);
trackObject = 1; // Don't set up again, unless user selects new ROI
}
}
imshow("L", image_cvCPU_L);
char c = (char)waitKey(1);
if (c == 27)
break;
switch (c)
{
case 'b':
backprojMode = !backprojMode;
break;
case 'h':
showHist = !showHist;
if (!showHist)
destroyWindow("Histogram");
else
namedWindow("Histogram", 1);
break;
default:
;
}
}
zed.close();
}