-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKinectHandler.cpp
More file actions
169 lines (142 loc) · 4.48 KB
/
KinectHandler.cpp
File metadata and controls
169 lines (142 loc) · 4.48 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
// KinectHandler.cpp : Defines the entry point for the console application.
//
#include "util.h"
const string FILENAME = "calib.txt";
const unsigned int THRESHOLD = 2000;
const char DEST_IP[16] = "127.0.0.1";
const unsigned int DEST_PORT = 45000;
int _tmain(int argc, _TCHAR* argv[])
{
VideoStream color, depth;
int res = OpenNI::initialize();
Array<DeviceInfo> camera;
OpenNI::enumerateDevices(&camera);
if (camera.getSize() < 1)
{
cout << "No devices Connected" << endl;
OpenNI::shutdown();
return 1;
}
int device_id = 0;
Device device;
res = device.open(camera[device_id].getUri());
res = depth.create(device, SENSOR_DEPTH);
res = color.create(device, SENSOR_COLOR);
device.setDepthColorSyncEnabled(TRUE);
device.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR);
initColorSettings(color);
initDepthSettings(depth);
openni::VideoStream** input_streams = new openni::VideoStream*[2];
input_streams[0] = &color;
input_streams[1] = &depth;
color.start();
depth.start();
int depthIndex = 0, colorIndex = 0;
VideoFrameRef depthFrame;
const DepthPixel* depthpixel;
NetConn connection(DEST_IP, DEST_PORT);
res = connection.getResult();
if (res != 0)
{
cout << "Connection errors\nreturning..." << endl;
return 1;
}
Mat colorImage, green_destframe, orange_destframe;
vector<Scalar> min_max(4);
bool calibration = false;
fstream calibFile(FILENAME);
if (calibFile == nullptr)
{
min_max[0] = Scalar(1, 1, 1);
min_max[1] = Scalar(254, 254, 254);
min_max[2] = Scalar(1, 1, 1);
min_max[3] = Scalar(254, 254, 254);
calibration = true;
cout << "No calibration file detected" << endl;
}
else min_max = readCalibValues(calibFile);
int MarkerId = 0, MarkerIdOld = 0;
MarkerDetector MDetect;
vector<Marker> Markers;
while (true)
{
do
{
int streamIndex = -1;
OpenNI::waitForAnyStream(input_streams, 2, &streamIndex);
switch(streamIndex)
{
case 0:
colorImage = getColorImage(color);
colorIndex = 1;
break;
case 1:
depth.readFrame(&depthFrame);
depthpixel = (const DepthPixel*)depthFrame.getData();
depthIndex = 1;
break;
}
} while (!depthIndex || !colorIndex);
depthIndex = colorIndex = 0;
if (calibration)
{
min_max = calibrateColor();
Mat colorImage_YCrCb = convertColorSpace(colorImage, CV_BGR2YCrCb);
inRange(colorImage_YCrCb, min_max[0], min_max[1], green_destframe);
inRange(colorImage_YCrCb, min_max[2], min_max[3], orange_destframe);
imshow("Filtering Green", green_destframe);
imshow("Filtering Orange", orange_destframe);
}
else
{
MDetect.detect(colorImage, Markers);
dropDepthVal(colorImage, depthpixel, THRESHOLD);
for (int i = 0; i < Markers.size(); i++) Markers[i].draw(colorImage, Scalar(0,0,255), 2);
if (Markers.size() > 0)
{
MarkerId = Markers[0].id;
if (MarkerId != MarkerIdOld) connection.mysend(ARUCO_ID, (float)MarkerId, (float)0, (float)0);
MarkerIdOld = MarkerId;
}
Mat colorImage_YCrCb = convertColorSpace(colorImage, CV_BGR2YCrCb);
Mat orangeComponent;
inRange(colorImage_YCrCb, min_max[2], min_max[3], orangeComponent);
Mat greenComponent;
inRange(colorImage_YCrCb, min_max[0], min_max[1], greenComponent);
int cx = 0, cy = 0;
float xpos, ypos, zpos;
getPointerPos(greenComponent, depth, depthpixel, xpos, ypos, zpos, cx, cy);
circle(colorImage, Point(cx, cy), 3, Scalar(0, 255, 255));
if (connection.mysend(GREEN_ID, -xpos, ypos, zpos) < 0)
{
cout << "Error in mysend: " << res << endl;
return 1;
}
getPointerPos(orangeComponent, depth, depthpixel, xpos, ypos, zpos, cx, cy);
circle(colorImage, Point(cx, cy), 3, Scalar(0, 255, 255));
if (connection.mysend(ORANGE_ID, -xpos, ypos, zpos) < 0)
{
cout << "Error in mysend: " << res << endl;
return 1;
}
imshow("Color Image YCrCb", colorImage_YCrCb);
imshow("Orange", orangeComponent);
imshow("Green", greenComponent);
}
resize(colorImage, colorImage, Size(WIN_XSIZE * 2, WIN_YSIZE * 2));
imshow("Color Image RGB", colorImage); //Shows Camera Image
resize(colorImage, colorImage, Size(WIN_XSIZE, WIN_YSIZE));
int c = waitKey(5);
switch (c)
{
case 's':
if (!saveCalibValues(min_max, FILENAME)) cout << "Calibration saved..." << endl;
calibration = false;
break;
case 'c':
calibration = true;
break;
}
}
return 0;
}