-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTrackingPointSelector.cpp
More file actions
191 lines (173 loc) · 6.5 KB
/
TrackingPointSelector.cpp
File metadata and controls
191 lines (173 loc) · 6.5 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
/*
* TrackingPointSelector.cpp
* WebARKit
*
* This file is part of WebARKit.
*
* WebARKit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WebARKit 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with WebARKit. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2018 Realmax, Inc.
* Copyright 2015 Daqri, LLC.
* Copyright 2010-2015 ARToolworks, Inc.
* Copyright 2023 WebARKit
*
* Author(s): Philip Lamb, Daniel Bell, Walter Perdan
*
* This code was taken from artoolkitX https://github.com/artoolkitx/artoolkitx
* with small modifications to adapt to existing WebARKIt code.
*
*/
#include <WebARKitTrackers/WebARKitOpticalTracking/TrackingPointSelector.h>
TrackingPointSelector::TrackingPointSelector()
{
}
TrackingPointSelector::TrackingPointSelector(std::vector<cv::Point2f> pts, int width, int height, int markerTemplateWidth)
{
_pts = pts;
DistributeBins(width, height, markerTemplateWidth);
}
void TrackingPointSelector::DistributeBins(int width, int height, int markerTemplateWidth)
{
int numberOfBins = 10;
// Split width and height dimensions into 10 bins each, for total of 100 bins.
int totalXBins = width/numberOfBins;
int totalYBins = height/numberOfBins;
// Init empty bins.
for(int i=0; i<(numberOfBins * numberOfBins) ; i++) {
trackingPointBin.insert(std::pair<int, std::vector<TrackedPoint> >(i, std::vector<TrackedPoint>()));
}
// Iterate the points and add points to each bin.
for(int i=0, id=0; i<_pts.size(); i++) {
int bx = (int)_pts[i].x/totalXBins;
int by = (int)_pts[i].y/totalYBins;
int index = bx + (by * numberOfBins);
cv::Rect templateRoi = cv::Rect(_pts[i].x-markerTemplateWidth, _pts[i].y-markerTemplateWidth, markerTemplateWidth*2, markerTemplateWidth*2);
bool is_inside = (templateRoi & cv::Rect(0, 0, width, height)) == templateRoi; // templateRoi must not intersect image boundary.
if(is_inside) {
TrackedPoint newPt;
newPt.id = id;
newPt.pt = _pts[i];
newPt.pt3d = cv::Point3f(_pts[i].x, _pts[i].y, 0);
newPt.markerRoi = templateRoi;
trackingPointBin[index].push_back(newPt);
id++;
}
}
}
void TrackingPointSelector::SetHomography(cv::Mat newHomography)
{
homography = newHomography;
}
cv::Mat TrackingPointSelector::GetHomography()
{
return homography;
}
void TrackingPointSelector::UpdatePointStatus(std::vector<uchar> status)
{
int index = 0;
for(std::vector<TrackedPoint>::iterator it = _selectedPts.begin(); it != _selectedPts.end(); ++it) {
if(it->tracking) {
it->SetTracking((int)status[index++]);
}
}
}
void TrackingPointSelector::SelectPoints()
{
_selectedPts.clear();
for(auto &track : trackingPointBin) {
for(auto &trackPt : track.second) {
trackPt.SetSelected(false);
trackPt.SetTracking(false);
}
}
for(auto &track : trackingPointBin) {
if(track.second.size()>0) {
//Get a random idex for this track and choose that point from the points bin
int tIndex = rng.uniform(0, static_cast<int>(track.second.size()));
track.second[tIndex].SetSelected(true);
track.second[tIndex].SetTracking(true);
_selectedPts.push_back(track.second[tIndex]);
}
}
}
std::vector<cv::Point2f> TrackingPointSelector::GetSelectedFeatures()
{
std::vector<cv::Point2f> selectedPoints;
for(std::vector<TrackedPoint>::iterator it = _selectedPts.begin(); it != _selectedPts.end(); ++it) {
if(it->IsSelected()) {
selectedPoints.push_back(it->pt);
}
}
return selectedPoints;
}
std::vector<cv::Point2f> TrackingPointSelector::GetTrackedFeatures()
{
std::vector<cv::Point2f> selectedPoints;
for(std::vector<TrackedPoint>::iterator it = _selectedPts.begin(); it != _selectedPts.end(); ++it) {
if(it->IsTracking()) {
selectedPoints.push_back(it->pt);
}
}
return selectedPoints;
}
std::vector<cv::Point3f> TrackingPointSelector::GetSelectedFeatures3d()
{
std::vector<cv::Point3f> selectedPoints;
for(std::vector<TrackedPoint>::iterator it = _selectedPts.begin(); it != _selectedPts.end(); ++it) {
if(it->IsTracking()) {
selectedPoints.push_back(it->pt3d);
}
}
return selectedPoints;
}
std::vector<cv::Point2f> TrackingPointSelector::GetSelectedFeaturesWarped()
{
std::vector<cv::Point2f> warpedPoints;
std::vector<cv::Point2f> selectedPoints;
for(std::vector<TrackedPoint>::iterator it = _selectedPts.begin(); it != _selectedPts.end(); ++it) {
if(it->IsTracking()) {
selectedPoints.push_back(it->pt);
}
}
perspectiveTransform(selectedPoints, warpedPoints, homography);
return warpedPoints;
}
std::vector<cv::Point2f> TrackingPointSelector::GetAllFeatures()
{
std::vector<cv::Point2f> allBinnedPoints;
for(auto &track : trackingPointBin) {
for(auto &trackPt : track.second) {
allBinnedPoints.push_back(trackPt.pt);
}
}
return allBinnedPoints;
}
void TrackingPointSelector::CleanUp()
{
_selectedPts.clear();
_pts.clear();
trackingPointBin.clear();
homography.release();
}