blob: d269db5d0811f631b7f1a5e064f5639bc2f3fed2 [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");
21
Parker Schuh9e1d1692019-02-24 14:34:04 -080022using ::aos::events::DataSocket;
23using ::aos::events::RXUdpSocket;
24using ::aos::events::TCPServer;
25using ::aos::vision::DataRef;
26using ::aos::vision::Int32Codec;
27using ::aos::monotonic_clock;
28using aos::vision::Segment;
29
30using ceres::NumericDiffCostFunction;
31using ceres::CENTRAL;
32using ceres::CostFunction;
33using ceres::Problem;
34using ceres::Solver;
35using ceres::Solve;
36
37namespace y2019 {
38namespace vision {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080039namespace {
Parker Schuh9e1d1692019-02-24 14:34:04 -080040
41constexpr double kInchesToMeters = 0.0254;
42
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080043} // namespace
Parker Schuh9e1d1692019-02-24 14:34:04 -080044
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080045::std::array<double, 3> GetError(const DatasetInfo &info,
46 const double *const extrinsics,
47 const double *const geometry, int i) {
48 const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(extrinsics);
49 const CameraGeometry geo = CameraGeometry::get(geometry);
Parker Schuh9e1d1692019-02-24 14:34:04 -080050
Parker Schuha4e52fb2019-02-24 18:18:15 -080051 const double s = sin(geo.heading + extrinsic_params.r2);
52 const double c = cos(geo.heading + extrinsic_params.r2);
Parker Schuh9e1d1692019-02-24 14:34:04 -080053
Parker Schuha4e52fb2019-02-24 18:18:15 -080054 // Take the tape measure starting point (this will be at the perimeter of the
55 // robot), add the offset to the first sample, and then add the per sample
56 // offset.
Austin Schuh6cac52c2019-03-08 20:03:33 -080057 const double dx = (info.to_tape_measure_start[0] +
58 (info.beginning_tape_measure_reading + i) *
59 info.tape_measure_direction[0]) -
60 (geo.location[0] + c * extrinsic_params.z);
61 const double dy = (info.to_tape_measure_start[1] +
62 (info.beginning_tape_measure_reading + i) *
63 info.tape_measure_direction[1]) -
64 (geo.location[1] + s * extrinsic_params.z);
Parker Schuha4e52fb2019-02-24 18:18:15 -080065
Austin Schuh6cac52c2019-03-08 20:03:33 -080066 constexpr double kCalibrationTargetHeight = 28.5 * kInchesToMeters;
67 const double dz =
68 kCalibrationTargetHeight - (geo.location[2] + extrinsic_params.y);
Parker Schuh9e1d1692019-02-24 14:34:04 -080069 return {{dx, dy, dz}};
70}
71
72void main(int argc, char **argv) {
Parker Schuh9e1d1692019-02-24 14:34:04 -080073 using namespace y2019::vision;
Austin Schuhe623f9f2019-03-03 12:24:03 -080074 gflags::ParseCommandLineFlags(&argc, &argv, false);
Parker Schuha4e52fb2019-02-24 18:18:15 -080075
Parker Schuha4e52fb2019-02-24 18:18:15 -080076 const char *base_directory = "/home/parker/data/frc/2019_calibration/";
77
78 DatasetInfo info = {
Austin Schuhe623f9f2019-03-03 12:24:03 -080079 FLAGS_camera_id,
Parker Schuha4e52fb2019-02-24 18:18:15 -080080 {{12.5 * kInchesToMeters, 0.5 * kInchesToMeters}},
81 {{kInchesToMeters, 0.0}},
82 26,
83 "cam5_0/debug_viewer_jpeg_",
84 59,
85 };
86
Parker Schuh9e1d1692019-02-24 14:34:04 -080087 ::aos::logging::Init();
88 ::aos::logging::AddImplementation(
89 new ::aos::logging::StreamLogImplementation(stderr));
90
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080091 TargetFinder target_finder;
Parker Schuh9e1d1692019-02-24 14:34:04 -080092
93 ceres::Problem problem;
94
95 struct Sample {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080096 ::std::unique_ptr<double[]> extrinsics;
Parker Schuh9e1d1692019-02-24 14:34:04 -080097 int i;
98 };
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080099 ::std::vector<Sample> all_extrinsics;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800100 double intrinsics[IntrinsicParams::kNumParams];
101 IntrinsicParams().set(&intrinsics[0]);
102
Parker Schuha4e52fb2019-02-24 18:18:15 -0800103 double geometry[CameraGeometry::kNumParams];
104 CameraGeometry().set(&geometry[0]);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800105
106 Solver::Options options;
107 options.minimizer_progress_to_stdout = false;
108 Solver::Summary summary;
109
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800110 ::std::cout << summary.BriefReport() << "\n";
Austin Schuhcacc6922019-03-06 20:44:30 -0800111 IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]);
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800112 ::std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";\n";
113 ::std::cout << "fl = " << intrinsics_.focal_length << ";\n";
114 ::std::cout << "error = " << summary.final_cost << ";\n";
Parker Schuh9e1d1692019-02-24 14:34:04 -0800115
Parker Schuha4e52fb2019-02-24 18:18:15 -0800116 for (int i = 0; i < info.num_images; ++i) {
117 auto frame = aos::vision::LoadFile(std::string(base_directory) +
118 info.filename_prefix +
119 std::to_string(i) + ".yuyv");
Parker Schuh9e1d1692019-02-24 14:34:04 -0800120
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800121 const ::aos::vision::ImageFormat fmt{640, 480};
122 ::aos::vision::BlobList imgs =
123 ::aos::vision::FindBlobs(aos::vision::DoThresholdYUYV(
Austin Schuh2851e7f2019-03-06 20:45:19 -0800124 fmt, frame.data.data(), TargetFinder::GetThresholdValue()));
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800125 target_finder.PreFilter(&imgs);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800126
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800127 constexpr bool verbose = false;
128 ::std::vector<std::vector<Segment<2>>> raw_polys;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800129 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800130 // Convert blobs to contours in the corrected space.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800131 ContourNode *contour = target_finder.GetContour(blob);
132 target_finder.UnWarpContour(contour);
133 const ::std::vector<Segment<2>> polygon =
134 target_finder.FillPolygon(contour, verbose);
135 if (!polygon.empty()) {
Parker Schuh9e1d1692019-02-24 14:34:04 -0800136 raw_polys.push_back(polygon);
137 }
138 }
139
140 // Calculate each component side of a possible target.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800141 const ::std::vector<TargetComponent> target_component_list =
142 target_finder.FillTargetComponentList(raw_polys);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800143
144 // Put the compenents together into targets.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800145 const ::std::vector<Target> target_list =
146 target_finder.FindTargetsFromComponents(target_component_list, verbose);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800147
Austin Schuh0fd1cef2019-03-08 20:05:14 -0800148 // Now, iterate over the targets (in pixel space). Generate a residual
149 // block for each valid target which compares the actual pixel locations
150 // with the expected pixel locations given the intrinsics and extrinsics.
Parker Schuh9e1d1692019-02-24 14:34:04 -0800151 for (const Target &target : target_list) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800152 const ::std::array<::aos::vision::Vector<2>, 8> target_value =
Austin Schuh2894e902019-03-03 21:12:46 -0800153 target.ToPointList();
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800154 const ::std::array<::aos::vision::Vector<2>, 8> template_value =
155 target_finder.GetTemplateTarget().ToPointList();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800156
Austin Schuh0fd1cef2019-03-08 20:05:14 -0800157 all_extrinsics.push_back(
158 {::std::unique_ptr<double[]>(new double[ExtrinsicParams::kNumParams]),
159 i});
160 double *extrinsics = all_extrinsics.back().extrinsics.get();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800161 ExtrinsicParams().set(extrinsics);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800162
163 for (size_t j = 0; j < 8; ++j) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800164 // Template target in target space as documented by GetTemplateTarget()
165 const ::aos::vision::Vector<2> template_point = template_value[j];
166 // Target in pixel space.
167 const ::aos::vision::Vector<2> target_point = target_value[j];
Parker Schuh9e1d1692019-02-24 14:34:04 -0800168
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800169 // Now build up the residual block.
170 auto ftor = [template_point, target_point, i](
171 const double *const intrinsics, const double *const extrinsics,
172 double *residual) {
173 const IntrinsicParams intrinsic_params =
174 IntrinsicParams::get(intrinsics);
175 const ExtrinsicParams extrinsic_params =
176 ExtrinsicParams::get(extrinsics);
177 // Project the target location into pixel coordinates.
178 const ::aos::vision::Vector<2> projected_point =
179 Project(template_point, intrinsic_params, extrinsic_params);
180 const ::aos::vision::Vector<2> residual_point =
181 target_point - projected_point;
182 residual[0] = residual_point.x();
183 residual[1] = residual_point.y();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800184 return true;
185 };
186 problem.AddResidualBlock(
187 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 2,
188 IntrinsicParams::kNumParams,
189 ExtrinsicParams::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800190 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800191 NULL, &intrinsics[0], extrinsics);
192 }
193
Austin Schuh6cac52c2019-03-08 20:03:33 -0800194 // Now, compare the estimated location of the target that we just solved
195 // for with the extrinsic params above with the known location of the
196 // target.
Parker Schuha4e52fb2019-02-24 18:18:15 -0800197 auto ftor = [&info, i](const double *const extrinsics,
198 const double *const geometry, double *residual) {
Austin Schuh6cac52c2019-03-08 20:03:33 -0800199 const ::std::array<double, 3> err =
200 GetError(info, extrinsics, geometry, i);
201 // We care a lot more about geometry than pixels. Especially since
202 // pixels are small and meters are big, so there's a small penalty to
203 // being off by meters.
204 residual[0] = err[0] * 1259.0;
205 residual[1] = err[1] * 1259.0;
206 residual[2] = err[2] * 1259.0;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800207 return true;
208 };
209
210 problem.AddResidualBlock(
211 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 3,
212 ExtrinsicParams::kNumParams,
Parker Schuha4e52fb2019-02-24 18:18:15 -0800213 CameraGeometry::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800214 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800215 NULL, extrinsics, &geometry[0]);
216 }
217 }
218 // TODO: Debug solver convergence?
219 Solve(options, &problem, &summary);
220 Solve(options, &problem, &summary);
221 Solve(options, &problem, &summary);
222
223 {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800224 ::std::cout << summary.BriefReport() << ::std::endl;
Austin Schuhcacc6922019-03-06 20:44:30 -0800225 IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]);
226 CameraGeometry geometry_ = CameraGeometry::get(&geometry[0]);
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800227 ::std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";"
228 << ::std::endl;
229 ::std::cout << "fl = " << intrinsics_.focal_length << ";" << ::std::endl;
230 ::std::cout << "error = " << summary.final_cost << ";" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800231
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800232 ::std::cout << "camera_angle = " << geometry_.heading * 180 / M_PI
233 << ::std::endl;
234 ::std::cout << "camera_x = " << geometry_.location[0] << ::std::endl;
235 ::std::cout << "camera_y = " << geometry_.location[1] << ::std::endl;
236 ::std::cout << "camera_z = " << geometry_.location[2] << ::std::endl;
237 ::std::cout << "camera_barrel = " << intrinsics_.barrel_mount * 180.0 / M_PI
238 << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800239
Austin Schuhcacc6922019-03-06 20:44:30 -0800240 for (const Sample &sample : all_extrinsics) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800241 const int i = sample.i;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800242 double *data = sample.extrinsics.get();
243
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800244 const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(data);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800245
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800246 const ::std::array<double, 3> error =
247 GetError(info, data, &geometry[0], i);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800248
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800249 ::std::cout << i << ", ";
250 ::std::cout << extrinsic_params.z << "m, ";
251 ::std::cout << extrinsic_params.y << "m, ";
252 ::std::cout << extrinsic_params.r1 * 180 / M_PI << ", ";
253 ::std::cout << extrinsic_params.r2 * 180 / M_PI << ", ";
Parker Schuh9e1d1692019-02-24 14:34:04 -0800254 // TODO: Methodology problem: This should really have a separate solve for
255 // extrinsics...
Austin Schuh6cac52c2019-03-08 20:03:33 -0800256 ::std::cout << error[0] << "m, ";
257 ::std::cout << error[1] << "m, ";
258 ::std::cout << error[2] << "m" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800259 }
260 }
Parker Schuha4e52fb2019-02-24 18:18:15 -0800261
262 CameraCalibration results;
263 results.dataset = info;
264 results.intrinsics = IntrinsicParams::get(&intrinsics[0]);
265 results.geometry = CameraGeometry::get(&geometry[0]);
James Kuszmaule2c71ea2019-03-04 08:14:21 -0800266 DumpCameraConstants("y2019/vision/constants.cc", info.camera_id, results);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800267}
268
269} // namespace y2019
270} // namespace vision
271
272int main(int argc, char **argv) { y2019::vision::main(argc, argv); }