blob: 1c1fb99a6c9f12a51aaff21f2b76551cd1c28074 [file] [log] [blame]
Parker Schuh2a1447c2019-02-17 00:25:29 -08001#include "y2019/vision/target_finder.h"
2
3#include "ceres/ceres.h"
4
5#include <math.h>
6
7using ceres::NumericDiffCostFunction;
8using ceres::CENTRAL;
9using ceres::CostFunction;
10using ceres::Problem;
11using ceres::Solver;
12using ceres::Solve;
13
14namespace y2019 {
15namespace vision {
16
17static constexpr double kInchesToMeters = 0.0254;
18
19using namespace aos::vision;
20using aos::vision::Vector;
21
22Target Target::MakeTemplate() {
23 Target out;
24 // This is how off-vertical the tape is.
25 const double theta = 14.5 * M_PI / 180.0;
26
27 const double tape_offset = 4 * kInchesToMeters;
28 const double tape_width = 2 * kInchesToMeters;
29 const double tape_length = 5.5 * kInchesToMeters;
30
31 const double s = sin(theta);
32 const double c = cos(theta);
33 out.right.top = Vector<2>(tape_offset, 0.0);
34 out.right.inside = Vector<2>(tape_offset + tape_width * c, tape_width * s);
35 out.right.bottom = Vector<2>(tape_offset + tape_width * c + tape_length * s,
36 tape_width * s - tape_length * c);
37 out.right.outside =
38 Vector<2>(tape_offset + tape_length * s, -tape_length * c);
39
40 out.right.is_right = true;
41 out.left.top = Vector<2>(-out.right.top.x(), out.right.top.y());
42 out.left.inside = Vector<2>(-out.right.inside.x(), out.right.inside.y());
43 out.left.bottom = Vector<2>(-out.right.bottom.x(), out.right.bottom.y());
44 out.left.outside = Vector<2>(-out.right.outside.x(), out.right.outside.y());
45 return out;
46}
47
48std::array<Vector<2>, 8> Target::toPointList() const {
49 return std::array<Vector<2>, 8>{{right.top, right.inside, right.bottom,
50 right.outside, left.top, left.inside,
51 left.bottom, left.outside}};
52}
53
54Vector<2> Project(Vector<2> pt, const IntrinsicParams &intrinsics,
55 const ExtrinsicParams &extrinsics) {
56 double y = extrinsics.y;
57 double z = extrinsics.z;
58 double r1 = extrinsics.r1;
59 double r2 = extrinsics.r2;
60 double rup = intrinsics.mount_angle;
Parker Schuh9e1d1692019-02-24 14:34:04 -080061 double rbarrel = intrinsics.barrel_mount;
Parker Schuh2a1447c2019-02-17 00:25:29 -080062 double fl = intrinsics.focal_length;
63
64 ::Eigen::Matrix<double, 1, 3> pts{pt.x(), pt.y() + y, 0.0};
65
66 {
67 double theta = r1;
68 double s = sin(theta);
69 double c = cos(theta);
70 pts = (::Eigen::Matrix<double, 3, 3>() << c, 0, -s, 0, 1, 0, s, 0,
71 c).finished() *
72 pts.transpose();
73 }
74
75 pts(2) += z;
76
77 {
78 double theta = r2;
79 double s = sin(theta);
80 double c = cos(theta);
81 pts = (::Eigen::Matrix<double, 3, 3>() << c, 0, -s, 0, 1, 0, s, 0,
82 c).finished() *
83 pts.transpose();
84 }
85
86 // TODO: Apply 15 degree downward rotation.
87 {
88 double theta = rup;
89 double s = sin(theta);
90 double c = cos(theta);
91
92 pts = (::Eigen::Matrix<double, 3, 3>() << 1, 0, 0, 0, c, -s, 0, s,
93 c).finished() *
94 pts.transpose();
95 }
96
Parker Schuh9e1d1692019-02-24 14:34:04 -080097 // TODO: Maybe barrel should be extrinsics to allow rocking?
98 // Also, in this case, barrel should go above the rotation above?
99 pts = ::Eigen::AngleAxis<double>(rbarrel, ::Eigen::Vector3d(0.0, 0.0, 1.0)) *
100 pts.transpose();
101
Parker Schuh2a1447c2019-02-17 00:25:29 -0800102 // TODO: Final image projection.
103 ::Eigen::Matrix<double, 1, 3> res = pts;
104
105 float scale = fl / res.z();
106 return Vector<2>(res.x() * scale + 320.0, 240.0 - res.y() * scale);
107}
108
109Target Project(const Target &target, const IntrinsicParams &intrinsics,
110 const ExtrinsicParams &extrinsics) {
111 auto project = [&](Vector<2> pt) {
112 return Project(pt, intrinsics, extrinsics);
113 };
114 Target new_targ;
115 new_targ.right.is_right = true;
116 new_targ.right.top = project(target.right.top);
117 new_targ.right.inside = project(target.right.inside);
118 new_targ.right.bottom = project(target.right.bottom);
119 new_targ.right.outside = project(target.right.outside);
120
121 new_targ.left.top = project(target.left.top);
122 new_targ.left.inside = project(target.left.inside);
123 new_targ.left.bottom = project(target.left.bottom);
124 new_targ.left.outside = project(target.left.outside);
125
126 return new_targ;
127}
128
129// Used at runtime on a single image given camera parameters.
130struct RuntimeCostFunctor {
131 RuntimeCostFunctor(Vector<2> result, Vector<2> template_pt,
132 IntrinsicParams intrinsics)
133 : result(result), template_pt(template_pt), intrinsics(intrinsics) {}
134
135 bool operator()(const double *const x, double *residual) const {
136 auto extrinsics = ExtrinsicParams::get(x);
137 auto pt = result - Project(template_pt, intrinsics, extrinsics);
138 residual[0] = pt.x();
139 residual[1] = pt.y();
140 return true;
141 }
142
143 Vector<2> result;
144 Vector<2> template_pt;
145 IntrinsicParams intrinsics;
146};
147
148IntermediateResult TargetFinder::ProcessTargetToResult(const Target &target,
149 bool verbose) {
150 // Memory for the ceres solver.
151 double params[ExtrinsicParams::kNumParams];
152 default_extrinsics_.set(&params[0]);
153
154 Problem problem;
155
156 auto target_value = target.toPointList();
157 auto template_value = target_template_.toPointList();
158
159 for (size_t i = 0; i < 8; ++i) {
160 auto a = template_value[i];
161 auto b = target_value[i];
162
163 problem.AddResidualBlock(
164 new NumericDiffCostFunction<RuntimeCostFunctor, CENTRAL, 2, 4>(
165 new RuntimeCostFunctor(b, a, intrinsics_)),
166 NULL, &params[0]);
167 }
168
169 Solver::Options options;
170 options.minimizer_progress_to_stdout = false;
171 Solver::Summary summary;
172 Solve(options, &problem, &summary);
173
174 IntermediateResult IR;
175 IR.extrinsics = ExtrinsicParams::get(&params[0]);
176 IR.solver_error = summary.final_cost;
177
178 if (verbose) {
179 std::cout << summary.BriefReport() << "\n";
180 std::cout << "y = " << IR.extrinsics.y / kInchesToMeters << ";\n";
181 std::cout << "z = " << IR.extrinsics.z / kInchesToMeters << ";\n";
182 std::cout << "r1 = " << IR.extrinsics.r1 * 180 / M_PI << ";\n";
183 std::cout << "r2 = " << IR.extrinsics.r2 * 180 / M_PI << ";\n";
184 std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";\n";
185 std::cout << "fl = " << intrinsics_.focal_length << ";\n";
186 std::cout << "error = " << summary.final_cost << ";\n";
187 }
188 return IR;
189}
190
191} // namespace vision
192} // namespace y2019