forked from rbmj/612-guillotine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisionalg.cpp
118 lines (100 loc) · 3.53 KB
/
visionalg.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
/* visionalg.cpp
*
* Copyright (c) 2011, 2012 Chantilly Robotics <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Implement algorithms for vision processing.
*/
#include "visionalg.h"
#include "ports.h"
#include <cmath>
#include <Vision/AxisCameraParams.h>
/* A few algorithms:
*
* FOV given aspect ratio x:y
*
* theta: diagonal angle covered by camera image
* theta_x: angle covered horizontally
* theta_y: angle covered vertically
*
* [ x ]
* theta_x = arctan [ --------------------- tan theta ]
* [ sqrt( x^2 + y^2 ) ]
*
* [ y ]
* theta_x = arctan [ --------------------- tan theta ]
* [ sqrt( x^2 + y^2 ) ]
*/
/*
* Angular Offset of a Point:
*
* [ 2 sin ( FOV / 2) * r_offset ]
* theta = arcsin [ ------------------------------- ]
* [ r_total ]
*/
#ifdef AXIS_CAMERA_M1011
const double CAM_FOV = 0.820304748; //47 degrees, Axis M1011
#elif defined AXIS_CAMERA_206
const double CAM_FOV = 0.959931089; //55 degrees, Axis 206
#else
#error Invalid Camera Specified
#endif
const int AR_X = 4; //4:3 aspect
const int AR_Y = 3;
#ifdef RESOLUTION_640_480
const int RES_X = 640;
const int RES_Y = 480;
const AxisCameraParams::Resolution_t axis_resolution = AxisCameraParams::kResolution_640x480;
#elif defined RESOLUTION_320_240
const int RES_X = 320;
const int RES_Y = 240;
const AxisCameraParams::Resolution_t axis_resolution = AxisCameraParams::kResolution_320x240;
#elif defined RESOLUTION_160_120
const int RES_X = 160;
const int RES_Y = 120;
const AxisCameraParams::Resolution_t axis_resolution = AxisCameraParams::kResolution_160x120;
#else
#error Invalid Resolution Specified
#endif
double angle_offset(int offset, int total, double FOV) {
double c = 2 * std::sin(FOV / 2);
c *= offset;
c /= total;
return std::asin(c);
}
double fov_axis(int axis_one, int axis_two, double total) {
//returns field of view along axis_one with aspect ratio one:two
double val = axis_one / std::sqrt((axis_one * axis_one) + (axis_two * axis_two));
val *= std::tan(total);
return std::atan(val);
}
camera_fov::camera_fov(double fov, aspect_ratio r) : field_of_view(fov), ratio(r) {
field_of_view_x = fov_axis(ratio.X(), ratio.Y(), field_of_view);
field_of_view_y = fov_axis(ratio.Y(), ratio.X(), field_of_view);
}
camera_fov fov_obj(CAM_FOV, aspect_ratio(AR_X, AR_Y));
const camera_fov& FOV() {
return fov_obj;
}
aspect_ratio res_obj(RES_X, RES_Y);
const aspect_ratio& RESOLUTION() {
return res_obj;
}
aspect_ratio::aspect_ratio(int xv, int yv) : x(xv), y(yv) {}
void init_camera() {
if (camera().GetResolution() != axis_resolution) {
camera().WriteResolution(axis_resolution);
}
}