8#include <opencv2/core/types.hpp>
10#include <networktables/NetworkTableInstance.h>
11#include <networktables/NetworkTable.h>
12#include <cameraserver/CameraServer.h>
13#include <wpi/StringExtras.h>
38inline cv::Size
getResolution(cs::VideoMode vm) {
return cv::Size(vm.height, vm.width); }
71template<
typename num_t,
typename dnum_t>
static inline
72cv::Size_<num_t>
operator/(cv::Size_<num_t> input, dnum_t scale) {
73 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
74 static_assert(std::is_arithmetic<dnum_t>::value,
"Template parameter (dnum_t) must be arithemetic type");
75 return cv::Size_<num_t>(input.width/scale, input.height/scale);
83template<
typename num_t>
84inline bool inRange(num_t a, num_t lower, num_t upper) {
85 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
86 return a <= upper && a >= lower;
91void reinsert(std::vector<t>& vec,
size_t idx) {
92 vec.insert(vec.begin(), vec.at(idx));
93 vec.erase(vec.begin() + idx + 1);
96inline void swapIdx(std::vector<t>& vec,
size_t idx1,
size_t idx2) {
97 std::iter_swap(vec.begin()+idx1, vec.begin()+idx2);
100template <
typename num_t>
101inline int sgn(num_t val) {
return (num_t(0) < val) - (val < num_t(0)); }
103template<
typename num_t>
104inline cv::Size_<num_t>
distance(
const cv::Point_<num_t>& a,
const cv::Point_<num_t>& b) {
105 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
106 return cv::Size_<num_t>(std::abs(a.x-b.x), std::abs(a.y-b.y));
108template<
typename num_t>
109inline cv::Point3_<num_t>
distance(
const cv::Point3_<num_t>& a,
const cv::Point3_<num_t>& b) {
110 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
111 return cv::Point3_<num_t>(std::abs(a.x-b.x), std::abs(a.y-b.y), std::abs(a.z-b.z));
114template<
typename num_t>
115inline bool operator<(
const cv::Size_<num_t>& a,
const cv::Size_<num_t>& b) {
116 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
117 return a.width < b.width && a.height < b.height;
119template<
typename num_t>
120inline bool operator<=(
const cv::Size_<num_t>& a,
const cv::Size_<num_t>& b) {
121 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
122 return a.width <= b.width && a.height <= b.height;
124template<
typename num_t>
125inline bool operator>(
const cv::Size_<num_t>& a,
const cv::Size_<num_t>& b) {
126 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
127 return a.width > b.width && a.height > b.height;
129template<
typename num_t>
130inline bool operator>=(
const cv::Size_<num_t>& a,
const cv::Size_<num_t>& b) {
131 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
132 return a.width >= b.width && a.height >= b.height;
135template<
typename num_t>
136inline cv::Point_<num_t>
operator - (
const cv::Point_<num_t>& a,
const cv::Size_<num_t>& b) {
137 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
138 return cv::Point_<num_t>(a.x-b.width, a.y-b.height);
140template<
typename num_t>
141inline cv::Point_<num_t>
operator + (
const cv::Point_<num_t>& a,
const cv::Size_<num_t>& b) {
142 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
143 return cv::Point_<num_t>(a.x+b.width, a.y+b.height);
145template<
typename num_t>
146inline cv::Size_<num_t>
operator - (
const cv::Size_<num_t>& a,
const cv::Point_<num_t>& b) {
147 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
148 return cv::Point_<num_t>(a.width-b.x, a.height-b.y);
150template<
typename num_t>
151inline cv::Size_<num_t>
operator + (
const cv::Size_<num_t>& a,
const cv::Point_<num_t>& b) {
152 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
153 return cv::Point_<num_t>(a.width+b.x, a.height+b.y);
156template<
typename inum1_t,
typename inum2_t,
typename onum_t>
157inline cv::Size_<onum_t>
operator / (
const cv::Size_<inum1_t>& a,
const cv::Size_<inum2_t>& b) {
158 static_assert(std::is_arithmetic<inum1_t>::value,
"Template parameter (inum1_t) must be arithemetic type");
159 static_assert(std::is_arithmetic<inum2_t>::value,
"Template parameter (inum2_t) must be arithemetic type");
160 static_assert(std::is_arithmetic<onum_t>::value,
"Template parameter (onum_t) must be arithemetic type");
161 return cv::Size_<onum_t>((onum_t)a.width / b.width, (onum_t)a.height / b.height);
164template<
typename num_t>
165void operator -= (
const cv::Point_<num_t>& a,
const cv::Size_<num_t>& b);
166template<
typename num_t>
167void operator += (
const cv::Point_<num_t>& a,
const cv::Size_<num_t>& b);
168template<
typename num_t>
169void operator -= (
const cv::Size_<num_t>& a,
const cv::Point_<num_t>& b);
170template<
typename num_t>
171void operator += (
const cv::Size_<num_t>& a,
const cv::Point_<num_t>& b);
173template<
typename num_t>
174void rescale(std::vector<cv::Point_<num_t> >& points,
size_t scale);
175template<
typename num_t>
176void _rescale(std::vector<cv::Point_<num_t> >& points,
size_t scale);
177template<
typename num_t>
178void rescale(std::vector<cv::Point_<num_t> >& points,
double scale);
179template<
typename num_t>
180void rescale(std::vector<std::vector<cv::Point_<num_t> > >& contours,
double scale);
181template<
typename num_t>
182void rescale(std::vector<cv::Rect_<num_t> >& rects,
double scale);
184template<
typename num_t>
185cv::Point_<num_t>
findCenter(
const std::vector<cv::Point_<num_t> >& contour);
186template<
typename onum_t,
typename inum_t>
187cv::Point_<onum_t>
findCenter(
const std::vector<cv::Point_<inum_t> >& contour);
188template<
typename num_t>
189cv::Point3_<num_t>
findCenter3D(
const std::vector<cv::Point3_<num_t> >& contour);
190template<
typename onum_t,
typename inum_t>
191cv::Point3_<onum_t>
findCenter3D(
const std::vector<cv::Point3_<inum_t> >& contour);
193template<
typename num_t>
194cv::Point_<num_t>
findCenter(
const cv::Rect_<num_t>& rect);
195template<
typename onum_t,
typename inum_t>
196cv::Point_<onum_t>
findCenter(
const cv::Rect_<inum_t>& rect);
198template<
typename num_t>
200template<
typename num_t>
204template<
typename num_t,
size_t s>
205std::array<cv::Point3_<num_t>, s>
operator+(
const std::array<cv::Point_<num_t>, s>& base, num_t depth);
206template<
typename num_t,
size_t s>
207std::array<cv::Point3_<num_t>, s>
operator+(
const std::array<cv::Point3_<num_t>, s>& base, num_t depth);
208template<
typename num_t,
size_t s>
209void operator+=(std::array<cv::Point3_<num_t>, s>& base, num_t depth);
210template<
typename num_t>
211std::vector<cv::Point3_<num_t> >
operator+(
const std::vector<cv::Point_<num_t> >& base, num_t depth);
212template<
typename num_t>
213std::vector<cv::Point3_<num_t> >
operator+(
const std::vector<cv::Point3_<num_t> >& base, num_t depth);
214template<
typename num_t>
215void operator+=(std::vector<cv::Point3_<num_t>>& base, num_t depth);
216template<
typename num_t,
size_t s>
218template<
typename num_t>
249template<
typename num_t>
250void rescale(std::vector<cv::Point_<num_t> >& points,
size_t scale) {
251 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
252 for(
size_t i = 0; i < points.size(); i++) {
253 points[i] *= (int)scale;
256template<
typename num_t>
257void _rescale(std::vector<cv::Point_<num_t> >& points,
size_t scale) {
258 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
259 for(
size_t i = 0; i < points.size(); i++) {
260 points[i] /= (int)scale;
263template<
typename num_t>
264void rescale(std::vector<cv::Point_<num_t> >& points,
double scale) {
265 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
266 for(
size_t i = 0; i < points.size(); i++) {
270template<
typename num_t>
271void rescale(std::vector<std::vector<cv::Point_<num_t> > >& contours,
double scale) {
272 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
273 for(std::vector<cv::Point_<num_t> >& contour : contours) {
274 for(cv::Point_<num_t>& point : contour) {
279template<
typename num_t>
280void rescale(std::vector<cv::Rect_<num_t> >& rects,
double scale) {
281 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
282 for(cv::Rect_<num_t>& r : rects) {
290template<
typename num_t>
291cv::Point_<num_t>
findCenter(
const std::vector<cv::Point_<num_t> >& contour) {
292 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
293 if(contour.size() > 0) {
294 cv::Point_<num_t> ret;
295 for(
size_t i = 0; i < contour.size(); i++) {
296 ret.x += contour[i].x;
297 ret.y += contour[i].y;
299 ret.x /= contour.size();
300 ret.y /= contour.size();
303 return cv::Point_<num_t>();
305template<
typename onum_t,
typename inum_t>
306cv::Point_<onum_t>
findCenter(
const std::vector<cv::Point_<inum_t> >& contour) {
307 static_assert(std::is_arithmetic<onum_t>::value,
"Template parameter (num_t) must be arithemetic type");
308 static_assert(std::is_arithmetic<inum_t>::value,
"Template parameter (num_t) must be arithemetic type");
309 if(contour.size() > 0) {
310 cv::Point_<onum_t> ret;
311 for(
size_t i = 0; i < contour.size(); i++) {
312 ret.x += contour[i].x;
313 ret.y += contour[i].y;
315 ret.x /= contour.size();
316 ret.y /= contour.size();
319 return cv::Point_<onum_t>();
321template<
typename num_t>
322cv::Point3_<num_t>
findCenter3D(
const std::vector<cv::Point3_<num_t> >& contour) {
323 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
324 if(contour.size() > 0) {
325 cv::Point3_<num_t> ret;
326 for(
size_t i = 0; i < contour.size(); i++) {
327 ret.x += contour[i].x;
328 ret.y += contour[i].y;
329 ret.z += contour[i].z;
331 ret.x /= contour.size();
332 ret.y /= contour.size();
333 ret.z /= contour.size();
336 return cv::Point3_<num_t>();
338template<
typename onum_t,
typename inum_t>
339cv::Point3_<onum_t>
findCenter3D(
const std::vector<cv::Point3_<inum_t> >& contour) {
340 static_assert(std::is_arithmetic<onum_t>::value,
"Template parameter (num_t) must be arithemetic type");
341 static_assert(std::is_arithmetic<inum_t>::value,
"Template parameter (num_t) must be arithemetic type");
342 if(contour.size() > 0) {
343 cv::Point3_<onum_t> ret;
344 for(
size_t i = 0; i < contour.size(); i++) {
345 ret.x += contour[i].x;
346 ret.y += contour[i].y;
347 ret.z += contour[i].z;
349 ret.x /= contour.size();
350 ret.y /= contour.size();
351 ret.z /= contour.size();
354 return cv::Point3_<onum_t>();
357template<
typename num_t>
359 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
360 cv::Point_<num_t> ret;
361 ret.x = rect.tl().x + rect.br().x;
362 ret.y = rect.tl().y + rect.br().y;
367template<
typename onum_t,
typename inum_t>
368cv::Point_<onum_t>
findCenter(
const cv::Rect_<inum_t>& rect) {
369 static_assert(std::is_arithmetic<onum_t>::value,
"Template parameter (num_t) must be arithemetic type");
370 static_assert(std::is_arithmetic<inum_t>::value,
"Template parameter (num_t) must be arithemetic type");
371 cv::Point_<onum_t> ret;
372 ret.x = rect.tl().x + rect.br().x;
373 ret.y = rect.tl().y + rect.br().y;
379template<
typename num_t>
381 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
382 cv::Point_<num_t> center = findCenter<num_t, num_t>(points), abuff, bbuff;
386 [center, &abuff, &bbuff](
const cv::Point_<num_t>& a,
const cv::Point_<num_t>& b) {
389 return -atan2(abuff.x, abuff.y) < -atan2(bbuff.x, bbuff.y);
393template<
typename num_t>
395 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
396 cv::Point_<num_t> center = findCenter<num_t, num_t>(points), abuff, bbuff;
400 [center, &abuff, &bbuff](
const cv::Point_<num_t>& a,
const cv::Point_<num_t>& b) {
403 return atan2(abuff.x, abuff.y) < atan2(bbuff.x, bbuff.y);
408template<
typename num_t,
size_t s>
409std::array<cv::Point3_<num_t>, s>
operator+(
const std::array<cv::Point_<num_t>, s>& base, num_t depth) {
410 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
411 std::array<cv::Point3_<num_t>, s> ret;
412 for(
size_t i = 0; i < s; i++) {
413 ret[i] = std::move(cv::Point3_<num_t>(base[i].x, base[i].y, depth));
417template<
typename num_t,
size_t s>
418std::array<cv::Point3_<num_t>, s>
operator+(
const std::array<cv::Point3_<num_t>, s>& base, num_t depth) {
419 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
420 std::array<cv::Point3_<num_t>, s> ret;
421 for(
size_t i = 0; i < s; i++) {
426template<
typename num_t,
size_t s>
427void operator+=(std::array<cv::Point3_<num_t>, s>& base, num_t depth) {
428 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
429 for(
size_t i = 0; i < s; i++) {
433template<
typename num_t>
434std::vector<cv::Point3_<num_t> >
operator+(
const std::vector<cv::Point_<num_t> >& base, num_t depth) {
435 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
436 std::vector<cv::Point3_<num_t> > ret(base.size());
437 for(
size_t i = 0; i < base.size(); i++) {
438 ret.emplace_back(base[i].x, base[i].y, depth);
442template<
typename num_t>
443std::vector<cv::Point3_<num_t> >
operator+(
const std::vector<cv::Point3_<num_t> >& base, num_t depth) {
444 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
445 std::vector<cv::Point3_<num_t> > ret(base.size());
446 for(
size_t i = 0; i < base.size(); i++) {
447 ret.emplace_back(base[i].x, base[i].y, base[i].z + depth);
451template<
typename num_t>
452void operator+=(std::vector<cv::Point3_<num_t>>& base, num_t depth) {
453 static_assert(std::is_arithmetic<num_t>::value,
"Template parameter (num_t) must be arithemetic type");
454 for(
size_t i = 0; i < base.size(); i++) {
455 base[i].depth += depth;
void rescale(std::vector< cv::Point_< num_t > > &points, size_t scale)
void reorderClockWise(std::vector< cv::Point_< num_t > > &points)
bool operator>=(const cv::Size_< num_t > &a, const cv::Size_< num_t > &b)
void _rescale(std::vector< cv::Point_< num_t > > &points, size_t scale)
cv::Point_< num_t > operator-(const cv::Point_< num_t > &a, const cv::Size_< num_t > &b)
bool operator<=(const cv::Size_< num_t > &a, const cv::Size_< num_t > &b)
cv::Size_< num_t > distance(const cv::Point_< num_t > &a, const cv::Point_< num_t > &b)
void swapIdx(std::vector< t > &vec, size_t idx1, size_t idx2)
bool operator<(const cv::Size_< num_t > &a, const cv::Size_< num_t > &b)
cv::Size getResolution(cs::VideoMode vm)
cv::Point3_< num_t > findCenter3D(const std::vector< cv::Point3_< num_t > > &contour)
void reorderCClockWise(std::vector< cv::Point_< num_t > > &points)
bool operator>(const cv::Size_< num_t > &a, const cv::Size_< num_t > &b)
static cv::Size_< num_t > operator/(cv::Size_< num_t > input, dnum_t scale)
cv::Point_< num_t > findCenter(const std::vector< cv::Point_< num_t > > &contour)
cs::VideoMode getJsonVideoMode(const wpi::json &config)
bool inRange(num_t a, num_t lower, num_t upper)
void operator-=(const cv::Point_< num_t > &a, const cv::Size_< num_t > &b)
void reinsert(std::vector< t > &vec, size_t idx)
cv::Point_< num_t > operator+(const cv::Point_< num_t > &a, const cv::Size_< num_t > &b)
void operator+=(const cv::Point_< num_t > &a, const cv::Size_< num_t > &b)