blob: 77adc832f3c43883f790a4b72b929790e325a75d [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;
61 double fl = intrinsics.focal_length;
62
63 ::Eigen::Matrix<double, 1, 3> pts{pt.x(), pt.y() + y, 0.0};
64
65 {
66 double theta = r1;
67 double s = sin(theta);
68 double c = cos(theta);
69 pts = (::Eigen::Matrix<double, 3, 3>() << c, 0, -s, 0, 1, 0, s, 0,
70 c).finished() *
71 pts.transpose();
72 }
73
74 pts(2) += z;
75
76 {
77 double theta = r2;
78 double s = sin(theta);
79 double c = cos(theta);
80 pts = (::Eigen::Matrix<double, 3, 3>() << c, 0, -s, 0, 1, 0, s, 0,
81 c).finished() *
82 pts.transpose();
83 }
84
85 // TODO: Apply 15 degree downward rotation.
86 {
87 double theta = rup;
88 double s = sin(theta);
89 double c = cos(theta);
90
91 pts = (::Eigen::Matrix<double, 3, 3>() << 1, 0, 0, 0, c, -s, 0, s,
92 c).finished() *
93 pts.transpose();
94 }
95
96 // TODO: Final image projection.
97 ::Eigen::Matrix<double, 1, 3> res = pts;
98
99 float scale = fl / res.z();
100 return Vector<2>(res.x() * scale + 320.0, 240.0 - res.y() * scale);
101}
102
103Target Project(const Target &target, const IntrinsicParams &intrinsics,
104 const ExtrinsicParams &extrinsics) {
105 auto project = [&](Vector<2> pt) {
106 return Project(pt, intrinsics, extrinsics);
107 };
108 Target new_targ;
109 new_targ.right.is_right = true;
110 new_targ.right.top = project(target.right.top);
111 new_targ.right.inside = project(target.right.inside);
112 new_targ.right.bottom = project(target.right.bottom);
113 new_targ.right.outside = project(target.right.outside);
114
115 new_targ.left.top = project(target.left.top);
116 new_targ.left.inside = project(target.left.inside);
117 new_targ.left.bottom = project(target.left.bottom);
118 new_targ.left.outside = project(target.left.outside);
119
120 return new_targ;
121}
122
123// Used at runtime on a single image given camera parameters.
124struct RuntimeCostFunctor {
125 RuntimeCostFunctor(Vector<2> result, Vector<2> template_pt,
126 IntrinsicParams intrinsics)
127 : result(result), template_pt(template_pt), intrinsics(intrinsics) {}
128
129 bool operator()(const double *const x, double *residual) const {
130 auto extrinsics = ExtrinsicParams::get(x);
131 auto pt = result - Project(template_pt, intrinsics, extrinsics);
132 residual[0] = pt.x();
133 residual[1] = pt.y();
134 return true;
135 }
136
137 Vector<2> result;
138 Vector<2> template_pt;
139 IntrinsicParams intrinsics;
140};
141
142IntermediateResult TargetFinder::ProcessTargetToResult(const Target &target,
143 bool verbose) {
144 // Memory for the ceres solver.
145 double params[ExtrinsicParams::kNumParams];
146 default_extrinsics_.set(&params[0]);
147
148 Problem problem;
149
150 auto target_value = target.toPointList();
151 auto template_value = target_template_.toPointList();
152
153 for (size_t i = 0; i < 8; ++i) {
154 auto a = template_value[i];
155 auto b = target_value[i];
156
157 problem.AddResidualBlock(
158 new NumericDiffCostFunction<RuntimeCostFunctor, CENTRAL, 2, 4>(
159 new RuntimeCostFunctor(b, a, intrinsics_)),
160 NULL, &params[0]);
161 }
162
163 Solver::Options options;
164 options.minimizer_progress_to_stdout = false;
165 Solver::Summary summary;
166 Solve(options, &problem, &summary);
167
168 IntermediateResult IR;
169 IR.extrinsics = ExtrinsicParams::get(&params[0]);
170 IR.solver_error = summary.final_cost;
171
172 if (verbose) {
173 std::cout << summary.BriefReport() << "\n";
174 std::cout << "y = " << IR.extrinsics.y / kInchesToMeters << ";\n";
175 std::cout << "z = " << IR.extrinsics.z / kInchesToMeters << ";\n";
176 std::cout << "r1 = " << IR.extrinsics.r1 * 180 / M_PI << ";\n";
177 std::cout << "r2 = " << IR.extrinsics.r2 * 180 / M_PI << ";\n";
178 std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";\n";
179 std::cout << "fl = " << intrinsics_.focal_length << ";\n";
180 std::cout << "error = " << summary.final_cost << ";\n";
181 }
182 return IR;
183}
184
185} // namespace vision
186} // namespace y2019