-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChilitagsTuio.cpp
192 lines (159 loc) · 6.27 KB
/
ChilitagsTuio.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
/*
Chilitags TUIO Server
Copyright 2013 Quentin Bonnard [email protected]
This program is a modification of the
TUIO C++ Server Demo - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2009 Martin Kaltenbrunner <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ChilitagsTuio.h"
ChilitagsTuio::ChilitagsTuio(int xRes, int yRes, int cameraIndex, const std::string &host, int port, bool showFeedback):
tuioServer(host.c_str(), port),
cvCapture(cameraIndex),
showFeedback(showFeedback)
{
if (cvCapture.isOpened())
{
#ifdef OPENCV3
cvCapture.set(cv::CAP_PROP_FRAME_WIDTH, xRes);
cvCapture.set(cv::CAP_PROP_FRAME_HEIGHT, yRes);
#else
cvCapture.set(CV_CAP_PROP_FRAME_WIDTH, xRes);
cvCapture.set(CV_CAP_PROP_FRAME_HEIGHT, yRes);
#endif
}
else {
// TODO: should actually quit the program...
std::cerr << "Unable to initialise video capture.\n";
}
if (showFeedback) cv::namedWindow("ChilitagsTuio");
}
ChilitagsTuio::~ChilitagsTuio() {
if (showFeedback) cv::destroyWindow("ChilitagsTuio");
cvCapture.release();
}
namespace {
// a helper function taking care of drawing the tags if a visual feedback
// is wanted
void drawFeedback(
cv::Mat inputImage,
const std::map<int, chilitags::Quad> &tags,
double processingTime) {
static const cv::Scalar COLOR(255, 0, 255);
cv::Mat outputImage = inputImage.clone();
for (auto tag : tags) {
// borders
const cv::Mat_<cv::Point2f> corners(tag.second);
for (size_t i = 0; i < 4; ++i) {
// sub-pixel precision drawing
static const int SHIFT = 16;
static const float PRECISION = 1<<SHIFT;
cv::line(
outputImage,
PRECISION*corners(i),
PRECISION*corners((i+1)%4),
COLOR, 1, CV_AA, SHIFT);
}
// tag ID
cv::Point2f center = 0.5*(corners(0) + corners(2));
cv::putText(outputImage, cv::format("%d", tag.first), center,
cv::FONT_HERSHEY_SIMPLEX, 0.5, COLOR);
}
// resolution and processing time
cv::putText(outputImage,
cv::format("%dx%d %4.0f ms (press q to quit)",
outputImage.cols, outputImage.rows,
processingTime),
cv::Point(32,32),
cv::FONT_HERSHEY_SIMPLEX, 0.5, COLOR);
cv::imshow("ChilitagsTuio", outputImage);
}
}
void ChilitagsTuio::run() {
// Main loop, exiting when 'q is pressed'
// TODO: key are ignored when feedback is hidden
for (; 'q' != (char) cv::waitKey(1); ) {
cvCapture.read(inputImage);
int64 startTime = cv::getTickCount();
auto tags = chilitags.find(inputImage);
int64 endTime = cv::getTickCount();
double processingTime = 1000.0*((double) endTime - startTime)/cv::getTickFrequency();
tuioServer.initFrame(TUIO::TuioTime::getSessionTime());
// Handle detected tags
for (auto tag: tags) {
int tagId = tag.first;
const cv::Mat_<cv::Point2f> corners(tag.second);
cv::Point2f center = 0.5*(corners(0) + corners(2));
float x = center.x / (float) inputImage.cols;
float y = center.y / (float) inputImage.rows;
cv::Point2f topLine = corners(1)-corners(0);
float angle = std::atan2(topLine.y,topLine.x);
auto tuioObject = tuioObjects.find(tagId);
if(tuioObject == tuioObjects.end()) {
// new chilitag has been detected
std::cout << "[NEW TAG]: id " << tagId << std::endl;
tuioObjects[tagId] = tuioServer.addTuioObject(tagId, x, y, angle);
}
else
{
// chilitag already detected in the previous frame
std::cout << "[UPDATE TAG]: id " << tagId << " " << x << " " << y << " " << angle << std::endl;
tuioServer.updateTuioObject(tuioObject->second, x, y, angle);
}
}
// Remove disappeared tags
for (auto tuioObject = tuioObjects.begin(); tuioObject != tuioObjects.end(); ) {
if (tags.find(tuioObject->first) == tags.end()) {
// chilitag not detected anymore
std::cout << "[REMOVE TAG]: id " << tuioObject->first << std::endl;
tuioServer.removeTuioObject(tuioObject->second);
tuioObjects.erase(tuioObject++);
}
else {
++tuioObject;
}
}
tuioServer.commitFrame();
if(showFeedback) drawFeedback(inputImage, tags, processingTime);
}
}
int main(int argc, char* argv[])
{
if (( argc != 1) && ( argc != 3) && ( argc != 4) && ( argc != 6) && ( argc != 7) ) {
std::cout << "usage: "<< argv[0] <<" [x-Cam-Resolution y-Cam-Resolution] [cameraIndex] [host port] [hideFeedback]\n";
return 0;
}
int xRes = 640;
int yRes = 480;
int cameraIndex = 0;
std::string host = "127.0.0.1";
int port = 3333;
bool showFeedback = true;
if (argc >= 3) {
xRes = std::atoi(argv[1]);
yRes = std::atoi(argv[2]);
}
if (argc >= 4) {
cameraIndex = atoi(argv[3]);
}
if( argc >= 6 ) {
host = argv[4];
port = atoi(argv[5]);
};
if( argc == 7 ) {
showFeedback = false;
}
ChilitagsTuio(xRes, yRes, cameraIndex, host, port, showFeedback).run();
return 0;
}