blob: 60cfbc929923b8af30ba6b3a33468a6a75efa9d1 [file] [log] [blame]
Parker Schuh9e1d1692019-02-24 14:34:04 -08001#include <fstream>
2
3#include "aos/logging/implementations.h"
4#include "aos/logging/logging.h"
5#include "aos/vision/blob/codec.h"
6#include "aos/vision/blob/find_blob.h"
7#include "aos/vision/events/socket_types.h"
8#include "aos/vision/events/udp.h"
9#include "aos/vision/image/image_dataset.h"
10#include "aos/vision/image/image_stream.h"
11#include "aos/vision/image/reader.h"
12#include "y2019/vision/target_finder.h"
13
14#undef CHECK_NOTNULL
15#undef CHECK_OP
16#undef PCHECK
17// CERES Clashes with logging symbols...
18#include "ceres/ceres.h"
19
Austin Schuhe623f9f2019-03-03 12:24:03 -080020DEFINE_int32(camera_id, -1, "The camera ID to calibrate");
Austin Schuh3b815f12019-03-08 20:10:37 -080021DEFINE_string(prefix, "", "The image filename prefix");
Austin Schuhe623f9f2019-03-03 12:24:03 -080022
Parker Schuh9e1d1692019-02-24 14:34:04 -080023using ::aos::events::DataSocket;
24using ::aos::events::RXUdpSocket;
25using ::aos::events::TCPServer;
26using ::aos::vision::DataRef;
27using ::aos::vision::Int32Codec;
28using ::aos::monotonic_clock;
29using aos::vision::Segment;
30
31using ceres::NumericDiffCostFunction;
32using ceres::CENTRAL;
33using ceres::CostFunction;
34using ceres::Problem;
35using ceres::Solver;
36using ceres::Solve;
37
38namespace y2019 {
39namespace vision {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080040namespace {
Parker Schuh9e1d1692019-02-24 14:34:04 -080041
42constexpr double kInchesToMeters = 0.0254;
43
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080044} // namespace
Parker Schuh9e1d1692019-02-24 14:34:04 -080045
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080046::std::array<double, 3> GetError(const DatasetInfo &info,
47 const double *const extrinsics,
48 const double *const geometry, int i) {
49 const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(extrinsics);
50 const CameraGeometry geo = CameraGeometry::get(geometry);
Parker Schuh9e1d1692019-02-24 14:34:04 -080051
Parker Schuha4e52fb2019-02-24 18:18:15 -080052 const double s = sin(geo.heading + extrinsic_params.r2);
53 const double c = cos(geo.heading + extrinsic_params.r2);
Parker Schuh9e1d1692019-02-24 14:34:04 -080054
Parker Schuha4e52fb2019-02-24 18:18:15 -080055 // Take the tape measure starting point (this will be at the perimeter of the
56 // robot), add the offset to the first sample, and then add the per sample
57 // offset.
Austin Schuh6cac52c2019-03-08 20:03:33 -080058 const double dx = (info.to_tape_measure_start[0] +
59 (info.beginning_tape_measure_reading + i) *
60 info.tape_measure_direction[0]) -
61 (geo.location[0] + c * extrinsic_params.z);
62 const double dy = (info.to_tape_measure_start[1] +
63 (info.beginning_tape_measure_reading + i) *
64 info.tape_measure_direction[1]) -
65 (geo.location[1] + s * extrinsic_params.z);
Parker Schuha4e52fb2019-02-24 18:18:15 -080066
Austin Schuh6cac52c2019-03-08 20:03:33 -080067 constexpr double kCalibrationTargetHeight = 28.5 * kInchesToMeters;
68 const double dz =
69 kCalibrationTargetHeight - (geo.location[2] + extrinsic_params.y);
Parker Schuh9e1d1692019-02-24 14:34:04 -080070 return {{dx, dy, dz}};
71}
72
73void main(int argc, char **argv) {
Parker Schuh9e1d1692019-02-24 14:34:04 -080074 using namespace y2019::vision;
Austin Schuh3b815f12019-03-08 20:10:37 -080075 ::gflags::ParseCommandLineFlags(&argc, &argv, false);
Parker Schuha4e52fb2019-02-24 18:18:15 -080076
77 DatasetInfo info = {
Austin Schuhe623f9f2019-03-03 12:24:03 -080078 FLAGS_camera_id,
Parker Schuha4e52fb2019-02-24 18:18:15 -080079 {{12.5 * kInchesToMeters, 0.5 * kInchesToMeters}},
80 {{kInchesToMeters, 0.0}},
81 26,
Austin Schuh3b815f12019-03-08 20:10:37 -080082 FLAGS_prefix.c_str(),
Parker Schuha4e52fb2019-02-24 18:18:15 -080083 59,
84 };
85
Parker Schuh9e1d1692019-02-24 14:34:04 -080086 ::aos::logging::Init();
87 ::aos::logging::AddImplementation(
88 new ::aos::logging::StreamLogImplementation(stderr));
89
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080090 TargetFinder target_finder;
Parker Schuh9e1d1692019-02-24 14:34:04 -080091
92 ceres::Problem problem;
93
94 struct Sample {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080095 ::std::unique_ptr<double[]> extrinsics;
Parker Schuh9e1d1692019-02-24 14:34:04 -080096 int i;
97 };
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080098 ::std::vector<Sample> all_extrinsics;
Parker Schuh9e1d1692019-02-24 14:34:04 -080099 double intrinsics[IntrinsicParams::kNumParams];
100 IntrinsicParams().set(&intrinsics[0]);
101
Parker Schuha4e52fb2019-02-24 18:18:15 -0800102 double geometry[CameraGeometry::kNumParams];
Austin Schuh9bb57fe2019-03-08 20:23:47 -0800103 {
104 // Assume the camera is near the center of the robot, and start by pointing
105 // it from the center of the robot to the location of the first target.
106 CameraGeometry camera_geometry;
107 const double x =
108 info.to_tape_measure_start[0] +
109 info.beginning_tape_measure_reading * info.tape_measure_direction[0];
110 const double y =
111 info.to_tape_measure_start[1] +
112 info.beginning_tape_measure_reading * info.tape_measure_direction[1];
113 camera_geometry.heading = ::std::atan2(y, x);
114 printf("Inital heading is %f\n", camera_geometry.heading);
115 camera_geometry.set(&geometry[0]);
116 }
Parker Schuh9e1d1692019-02-24 14:34:04 -0800117
118 Solver::Options options;
119 options.minimizer_progress_to_stdout = false;
120 Solver::Summary summary;
121
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800122 ::std::cout << summary.BriefReport() << "\n";
Austin Schuhcacc6922019-03-06 20:44:30 -0800123 IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]);
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800124 ::std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";\n";
125 ::std::cout << "fl = " << intrinsics_.focal_length << ";\n";
126 ::std::cout << "error = " << summary.final_cost << ";\n";
Parker Schuh9e1d1692019-02-24 14:34:04 -0800127
Parker Schuha4e52fb2019-02-24 18:18:15 -0800128 for (int i = 0; i < info.num_images; ++i) {
Austin Schuh3b815f12019-03-08 20:10:37 -0800129 ::aos::vision::DatasetFrame frame =
130 aos::vision::LoadFile(FLAGS_prefix + std::to_string(i) + ".yuyv");
Parker Schuh9e1d1692019-02-24 14:34:04 -0800131
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800132 const ::aos::vision::ImageFormat fmt{640, 480};
133 ::aos::vision::BlobList imgs =
134 ::aos::vision::FindBlobs(aos::vision::DoThresholdYUYV(
Austin Schuh2851e7f2019-03-06 20:45:19 -0800135 fmt, frame.data.data(), TargetFinder::GetThresholdValue()));
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800136 target_finder.PreFilter(&imgs);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800137
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800138 constexpr bool verbose = false;
139 ::std::vector<std::vector<Segment<2>>> raw_polys;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800140 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800141 // Convert blobs to contours in the corrected space.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800142 ContourNode *contour = target_finder.GetContour(blob);
143 target_finder.UnWarpContour(contour);
144 const ::std::vector<Segment<2>> polygon =
145 target_finder.FillPolygon(contour, verbose);
146 if (!polygon.empty()) {
Parker Schuh9e1d1692019-02-24 14:34:04 -0800147 raw_polys.push_back(polygon);
148 }
149 }
150
151 // Calculate each component side of a possible target.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800152 const ::std::vector<TargetComponent> target_component_list =
153 target_finder.FillTargetComponentList(raw_polys);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800154
155 // Put the compenents together into targets.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800156 const ::std::vector<Target> target_list =
157 target_finder.FindTargetsFromComponents(target_component_list, verbose);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800158
Austin Schuh0fd1cef2019-03-08 20:05:14 -0800159 // Now, iterate over the targets (in pixel space). Generate a residual
160 // block for each valid target which compares the actual pixel locations
161 // with the expected pixel locations given the intrinsics and extrinsics.
Parker Schuh9e1d1692019-02-24 14:34:04 -0800162 for (const Target &target : target_list) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800163 const ::std::array<::aos::vision::Vector<2>, 8> target_value =
Austin Schuh2894e902019-03-03 21:12:46 -0800164 target.ToPointList();
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800165 const ::std::array<::aos::vision::Vector<2>, 8> template_value =
166 target_finder.GetTemplateTarget().ToPointList();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800167
Austin Schuh0fd1cef2019-03-08 20:05:14 -0800168 all_extrinsics.push_back(
169 {::std::unique_ptr<double[]>(new double[ExtrinsicParams::kNumParams]),
170 i});
171 double *extrinsics = all_extrinsics.back().extrinsics.get();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800172 ExtrinsicParams().set(extrinsics);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800173
174 for (size_t j = 0; j < 8; ++j) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800175 // Template target in target space as documented by GetTemplateTarget()
176 const ::aos::vision::Vector<2> template_point = template_value[j];
177 // Target in pixel space.
178 const ::aos::vision::Vector<2> target_point = target_value[j];
Parker Schuh9e1d1692019-02-24 14:34:04 -0800179
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800180 // Now build up the residual block.
181 auto ftor = [template_point, target_point, i](
182 const double *const intrinsics, const double *const extrinsics,
183 double *residual) {
184 const IntrinsicParams intrinsic_params =
185 IntrinsicParams::get(intrinsics);
186 const ExtrinsicParams extrinsic_params =
187 ExtrinsicParams::get(extrinsics);
188 // Project the target location into pixel coordinates.
189 const ::aos::vision::Vector<2> projected_point =
190 Project(template_point, intrinsic_params, extrinsic_params);
191 const ::aos::vision::Vector<2> residual_point =
192 target_point - projected_point;
193 residual[0] = residual_point.x();
194 residual[1] = residual_point.y();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800195 return true;
196 };
197 problem.AddResidualBlock(
198 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 2,
199 IntrinsicParams::kNumParams,
200 ExtrinsicParams::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800201 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800202 NULL, &intrinsics[0], extrinsics);
203 }
204
Austin Schuh6cac52c2019-03-08 20:03:33 -0800205 // Now, compare the estimated location of the target that we just solved
206 // for with the extrinsic params above with the known location of the
207 // target.
Parker Schuha4e52fb2019-02-24 18:18:15 -0800208 auto ftor = [&info, i](const double *const extrinsics,
209 const double *const geometry, double *residual) {
Austin Schuh6cac52c2019-03-08 20:03:33 -0800210 const ::std::array<double, 3> err =
211 GetError(info, extrinsics, geometry, i);
212 // We care a lot more about geometry than pixels. Especially since
213 // pixels are small and meters are big, so there's a small penalty to
214 // being off by meters.
215 residual[0] = err[0] * 1259.0;
216 residual[1] = err[1] * 1259.0;
217 residual[2] = err[2] * 1259.0;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800218 return true;
219 };
220
221 problem.AddResidualBlock(
222 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 3,
223 ExtrinsicParams::kNumParams,
Parker Schuha4e52fb2019-02-24 18:18:15 -0800224 CameraGeometry::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800225 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800226 NULL, extrinsics, &geometry[0]);
227 }
228 }
229 // TODO: Debug solver convergence?
230 Solve(options, &problem, &summary);
231 Solve(options, &problem, &summary);
232 Solve(options, &problem, &summary);
233
234 {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800235 ::std::cout << summary.BriefReport() << ::std::endl;
Austin Schuhcacc6922019-03-06 20:44:30 -0800236 IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]);
237 CameraGeometry geometry_ = CameraGeometry::get(&geometry[0]);
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800238 ::std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";"
239 << ::std::endl;
240 ::std::cout << "fl = " << intrinsics_.focal_length << ";" << ::std::endl;
241 ::std::cout << "error = " << summary.final_cost << ";" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800242
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800243 ::std::cout << "camera_angle = " << geometry_.heading * 180 / M_PI
244 << ::std::endl;
245 ::std::cout << "camera_x = " << geometry_.location[0] << ::std::endl;
246 ::std::cout << "camera_y = " << geometry_.location[1] << ::std::endl;
247 ::std::cout << "camera_z = " << geometry_.location[2] << ::std::endl;
248 ::std::cout << "camera_barrel = " << intrinsics_.barrel_mount * 180.0 / M_PI
249 << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800250
Austin Schuhcacc6922019-03-06 20:44:30 -0800251 for (const Sample &sample : all_extrinsics) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800252 const int i = sample.i;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800253 double *data = sample.extrinsics.get();
254
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800255 const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(data);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800256
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800257 const ::std::array<double, 3> error =
258 GetError(info, data, &geometry[0], i);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800259
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800260 ::std::cout << i << ", ";
261 ::std::cout << extrinsic_params.z << "m, ";
262 ::std::cout << extrinsic_params.y << "m, ";
263 ::std::cout << extrinsic_params.r1 * 180 / M_PI << ", ";
264 ::std::cout << extrinsic_params.r2 * 180 / M_PI << ", ";
Parker Schuh9e1d1692019-02-24 14:34:04 -0800265 // TODO: Methodology problem: This should really have a separate solve for
266 // extrinsics...
Austin Schuh6cac52c2019-03-08 20:03:33 -0800267 ::std::cout << error[0] << "m, ";
268 ::std::cout << error[1] << "m, ";
269 ::std::cout << error[2] << "m" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800270 }
271 }
Parker Schuha4e52fb2019-02-24 18:18:15 -0800272
273 CameraCalibration results;
274 results.dataset = info;
275 results.intrinsics = IntrinsicParams::get(&intrinsics[0]);
276 results.geometry = CameraGeometry::get(&geometry[0]);
James Kuszmaule2c71ea2019-03-04 08:14:21 -0800277 DumpCameraConstants("y2019/vision/constants.cc", info.camera_id, results);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800278}
279
280} // namespace y2019
281} // namespace vision
282
283int main(int argc, char **argv) { y2019::vision::main(argc, argv); }