blob: bb3259ea8bd70c2300cec7278daf33dd6ee70e83 [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
Parker Schuh9e1d1692019-02-24 14:34:04 -080014// CERES Clashes with logging symbols...
15#include "ceres/ceres.h"
16
Austin Schuhe623f9f2019-03-03 12:24:03 -080017DEFINE_int32(camera_id, -1, "The camera ID to calibrate");
Austin Schuh3b815f12019-03-08 20:10:37 -080018DEFINE_string(prefix, "", "The image filename prefix");
Austin Schuhe623f9f2019-03-03 12:24:03 -080019
Austin Schuhd6cc1e92019-03-08 21:01:51 -080020DEFINE_string(constants, "y2019/vision/constants.cc",
21 "Path to the constants file to update");
22
Austin Schuhd2e48da2019-03-08 20:52:32 -080023DEFINE_double(beginning_tape_measure_reading, 11,
Philipp Schrader790cb542023-07-05 21:06:52 -070024 "The tape measure measurement (in inches) of the first image.");
Austin Schuhd2e48da2019-03-08 20:52:32 -080025DEFINE_int32(image_count, 75, "The number of images to capture");
Philipp Schrader790cb542023-07-05 21:06:52 -070026DEFINE_double(tape_start_x, -12.5,
27 "The starting location of the tape measure in x relative to the "
28 "CG in inches.");
29DEFINE_double(tape_start_y, -0.5,
30 "The starting location of the tape measure in y relative to the "
31 "CG in inches.");
Austin Schuhd2e48da2019-03-08 20:52:32 -080032
33DEFINE_double(
34 tape_direction_x, -1.0,
35 "The x component of \"1\" inch along the tape measure in meters.");
36DEFINE_double(
37 tape_direction_y, 0.0,
38 "The y component of \"1\" inch along the tape measure in meters.");
39
Philipp Schrader790cb542023-07-05 21:06:52 -070040using ::aos::monotonic_clock;
Parker Schuh9e1d1692019-02-24 14:34:04 -080041using ::aos::events::DataSocket;
42using ::aos::events::RXUdpSocket;
43using ::aos::events::TCPServer;
44using ::aos::vision::DataRef;
45using ::aos::vision::Int32Codec;
Parker Schuh9e1d1692019-02-24 14:34:04 -080046using aos::vision::Segment;
47
Parker Schuh9e1d1692019-02-24 14:34:04 -080048using ceres::CENTRAL;
49using ceres::CostFunction;
Philipp Schrader790cb542023-07-05 21:06:52 -070050using ceres::NumericDiffCostFunction;
Parker Schuh9e1d1692019-02-24 14:34:04 -080051using ceres::Problem;
Parker Schuh9e1d1692019-02-24 14:34:04 -080052using ceres::Solve;
Philipp Schrader790cb542023-07-05 21:06:52 -070053using ceres::Solver;
Parker Schuh9e1d1692019-02-24 14:34:04 -080054
Stephan Pleinesf63bde82024-01-13 15:59:33 -080055namespace y2019::vision {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080056namespace {
Parker Schuh9e1d1692019-02-24 14:34:04 -080057
58constexpr double kInchesToMeters = 0.0254;
59
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080060} // namespace
Parker Schuh9e1d1692019-02-24 14:34:04 -080061
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080062::std::array<double, 3> GetError(const DatasetInfo &info,
63 const double *const extrinsics,
64 const double *const geometry, int i) {
65 const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(extrinsics);
66 const CameraGeometry geo = CameraGeometry::get(geometry);
Parker Schuh9e1d1692019-02-24 14:34:04 -080067
Parker Schuha4e52fb2019-02-24 18:18:15 -080068 const double s = sin(geo.heading + extrinsic_params.r2);
69 const double c = cos(geo.heading + extrinsic_params.r2);
Parker Schuh9e1d1692019-02-24 14:34:04 -080070
Parker Schuha4e52fb2019-02-24 18:18:15 -080071 // Take the tape measure starting point (this will be at the perimeter of the
72 // robot), add the offset to the first sample, and then add the per sample
73 // offset.
Austin Schuh6cac52c2019-03-08 20:03:33 -080074 const double dx = (info.to_tape_measure_start[0] +
75 (info.beginning_tape_measure_reading + i) *
76 info.tape_measure_direction[0]) -
77 (geo.location[0] + c * extrinsic_params.z);
78 const double dy = (info.to_tape_measure_start[1] +
79 (info.beginning_tape_measure_reading + i) *
80 info.tape_measure_direction[1]) -
81 (geo.location[1] + s * extrinsic_params.z);
Parker Schuha4e52fb2019-02-24 18:18:15 -080082
Austin Schuh6cac52c2019-03-08 20:03:33 -080083 constexpr double kCalibrationTargetHeight = 28.5 * kInchesToMeters;
84 const double dz =
85 kCalibrationTargetHeight - (geo.location[2] + extrinsic_params.y);
Parker Schuh9e1d1692019-02-24 14:34:04 -080086 return {{dx, dy, dz}};
87}
88
89void main(int argc, char **argv) {
Parker Schuh9e1d1692019-02-24 14:34:04 -080090 using namespace y2019::vision;
Austin Schuh3b815f12019-03-08 20:10:37 -080091 ::gflags::ParseCommandLineFlags(&argc, &argv, false);
Parker Schuha4e52fb2019-02-24 18:18:15 -080092
93 DatasetInfo info = {
Austin Schuhe623f9f2019-03-03 12:24:03 -080094 FLAGS_camera_id,
Austin Schuhd2e48da2019-03-08 20:52:32 -080095 {{FLAGS_tape_start_x * kInchesToMeters,
96 FLAGS_tape_start_y * kInchesToMeters}},
97 {{FLAGS_tape_direction_x * kInchesToMeters,
98 FLAGS_tape_direction_y * kInchesToMeters}},
99 FLAGS_beginning_tape_measure_reading,
Austin Schuh3b815f12019-03-08 20:10:37 -0800100 FLAGS_prefix.c_str(),
Austin Schuhd2e48da2019-03-08 20:52:32 -0800101 FLAGS_image_count,
Parker Schuha4e52fb2019-02-24 18:18:15 -0800102 };
103
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800104 TargetFinder target_finder;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800105
106 ceres::Problem problem;
107
108 struct Sample {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800109 ::std::unique_ptr<double[]> extrinsics;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800110 int i;
111 };
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800112 ::std::vector<Sample> all_extrinsics;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800113 double intrinsics[IntrinsicParams::kNumParams];
114 IntrinsicParams().set(&intrinsics[0]);
115
Parker Schuha4e52fb2019-02-24 18:18:15 -0800116 double geometry[CameraGeometry::kNumParams];
Austin Schuh9bb57fe2019-03-08 20:23:47 -0800117 {
118 // Assume the camera is near the center of the robot, and start by pointing
119 // it from the center of the robot to the location of the first target.
120 CameraGeometry camera_geometry;
121 const double x =
122 info.to_tape_measure_start[0] +
123 info.beginning_tape_measure_reading * info.tape_measure_direction[0];
124 const double y =
125 info.to_tape_measure_start[1] +
126 info.beginning_tape_measure_reading * info.tape_measure_direction[1];
127 camera_geometry.heading = ::std::atan2(y, x);
128 printf("Inital heading is %f\n", camera_geometry.heading);
129 camera_geometry.set(&geometry[0]);
130 }
Parker Schuh9e1d1692019-02-24 14:34:04 -0800131
132 Solver::Options options;
133 options.minimizer_progress_to_stdout = false;
134 Solver::Summary summary;
135
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800136 ::std::cout << summary.BriefReport() << "\n";
Austin Schuhcacc6922019-03-06 20:44:30 -0800137 IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]);
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800138 ::std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";\n";
139 ::std::cout << "fl = " << intrinsics_.focal_length << ";\n";
140 ::std::cout << "error = " << summary.final_cost << ";\n";
Parker Schuh9e1d1692019-02-24 14:34:04 -0800141
Parker Schuha4e52fb2019-02-24 18:18:15 -0800142 for (int i = 0; i < info.num_images; ++i) {
Austin Schuh3b815f12019-03-08 20:10:37 -0800143 ::aos::vision::DatasetFrame frame =
144 aos::vision::LoadFile(FLAGS_prefix + std::to_string(i) + ".yuyv");
Parker Schuh9e1d1692019-02-24 14:34:04 -0800145
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800146 const ::aos::vision::ImageFormat fmt{640, 480};
147 ::aos::vision::BlobList imgs =
Brian Silverman37b15b32019-03-10 13:30:18 -0700148 ::aos::vision::FindBlobs(aos::vision::SlowYuyvYThreshold(
Austin Schuh2851e7f2019-03-06 20:45:19 -0800149 fmt, frame.data.data(), TargetFinder::GetThresholdValue()));
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800150 target_finder.PreFilter(&imgs);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800151
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800152 constexpr bool verbose = false;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700153 ::std::vector<Polygon> raw_polys;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800154 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800155 // Convert blobs to contours in the corrected space.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800156 ContourNode *contour = target_finder.GetContour(blob);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700157 ::std::vector<::Eigen::Vector2f> unwarped_contour =
Austin Schuhe5015972019-03-09 17:47:34 -0800158 target_finder.UnWarpContour(contour);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700159 const Polygon polygon =
160 target_finder.FindPolygon(::std::move(unwarped_contour), verbose);
161 if (!polygon.segments.empty()) {
Parker Schuh9e1d1692019-02-24 14:34:04 -0800162 raw_polys.push_back(polygon);
163 }
164 }
165
166 // Calculate each component side of a possible target.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800167 const ::std::vector<TargetComponent> target_component_list =
Austin Schuh32ffac22019-03-09 22:42:02 -0800168 target_finder.FillTargetComponentList(raw_polys, verbose);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800169
170 // Put the compenents together into targets.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800171 const ::std::vector<Target> target_list =
172 target_finder.FindTargetsFromComponents(target_component_list, verbose);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800173
Austin Schuh0fd1cef2019-03-08 20:05:14 -0800174 // Now, iterate over the targets (in pixel space). Generate a residual
175 // block for each valid target which compares the actual pixel locations
176 // with the expected pixel locations given the intrinsics and extrinsics.
Parker Schuh9e1d1692019-02-24 14:34:04 -0800177 for (const Target &target : target_list) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800178 const ::std::array<::aos::vision::Vector<2>, 8> target_value =
Austin Schuh2894e902019-03-03 21:12:46 -0800179 target.ToPointList();
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800180 const ::std::array<::aos::vision::Vector<2>, 8> template_value =
181 target_finder.GetTemplateTarget().ToPointList();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800182
Austin Schuh0fd1cef2019-03-08 20:05:14 -0800183 all_extrinsics.push_back(
184 {::std::unique_ptr<double[]>(new double[ExtrinsicParams::kNumParams]),
185 i});
186 double *extrinsics = all_extrinsics.back().extrinsics.get();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800187 ExtrinsicParams().set(extrinsics);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800188
189 for (size_t j = 0; j < 8; ++j) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800190 // Template target in target space as documented by GetTemplateTarget()
191 const ::aos::vision::Vector<2> template_point = template_value[j];
192 // Target in pixel space.
193 const ::aos::vision::Vector<2> target_point = target_value[j];
Parker Schuh9e1d1692019-02-24 14:34:04 -0800194
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800195 // Now build up the residual block.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800196 auto ftor = [template_point, target_point](
Philipp Schrader790cb542023-07-05 21:06:52 -0700197 const double *const intrinsics,
198 const double *const extrinsics, double *residual) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800199 const IntrinsicParams intrinsic_params =
200 IntrinsicParams::get(intrinsics);
201 const ExtrinsicParams extrinsic_params =
202 ExtrinsicParams::get(extrinsics);
203 // Project the target location into pixel coordinates.
204 const ::aos::vision::Vector<2> projected_point =
205 Project(template_point, intrinsic_params, extrinsic_params);
206 const ::aos::vision::Vector<2> residual_point =
207 target_point - projected_point;
208 residual[0] = residual_point.x();
209 residual[1] = residual_point.y();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800210 return true;
211 };
212 problem.AddResidualBlock(
213 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 2,
214 IntrinsicParams::kNumParams,
215 ExtrinsicParams::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800216 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800217 NULL, &intrinsics[0], extrinsics);
218 }
219
Austin Schuh6cac52c2019-03-08 20:03:33 -0800220 // Now, compare the estimated location of the target that we just solved
221 // for with the extrinsic params above with the known location of the
222 // target.
Parker Schuha4e52fb2019-02-24 18:18:15 -0800223 auto ftor = [&info, i](const double *const extrinsics,
224 const double *const geometry, double *residual) {
Austin Schuh6cac52c2019-03-08 20:03:33 -0800225 const ::std::array<double, 3> err =
226 GetError(info, extrinsics, geometry, i);
227 // We care a lot more about geometry than pixels. Especially since
228 // pixels are small and meters are big, so there's a small penalty to
229 // being off by meters.
230 residual[0] = err[0] * 1259.0;
231 residual[1] = err[1] * 1259.0;
232 residual[2] = err[2] * 1259.0;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800233 return true;
234 };
235
236 problem.AddResidualBlock(
237 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 3,
238 ExtrinsicParams::kNumParams,
Parker Schuha4e52fb2019-02-24 18:18:15 -0800239 CameraGeometry::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800240 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800241 NULL, extrinsics, &geometry[0]);
242 }
243 }
244 // TODO: Debug solver convergence?
245 Solve(options, &problem, &summary);
246 Solve(options, &problem, &summary);
247 Solve(options, &problem, &summary);
248
249 {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800250 ::std::cout << summary.BriefReport() << ::std::endl;
Austin Schuhcacc6922019-03-06 20:44:30 -0800251 IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]);
252 CameraGeometry geometry_ = CameraGeometry::get(&geometry[0]);
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800253 ::std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";"
254 << ::std::endl;
255 ::std::cout << "fl = " << intrinsics_.focal_length << ";" << ::std::endl;
256 ::std::cout << "error = " << summary.final_cost << ";" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800257
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800258 ::std::cout << "camera_angle = " << geometry_.heading * 180 / M_PI
259 << ::std::endl;
260 ::std::cout << "camera_x = " << geometry_.location[0] << ::std::endl;
261 ::std::cout << "camera_y = " << geometry_.location[1] << ::std::endl;
262 ::std::cout << "camera_z = " << geometry_.location[2] << ::std::endl;
263 ::std::cout << "camera_barrel = " << intrinsics_.barrel_mount * 180.0 / M_PI
264 << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800265
Austin Schuhcacc6922019-03-06 20:44:30 -0800266 for (const Sample &sample : all_extrinsics) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800267 const int i = sample.i;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800268 double *data = sample.extrinsics.get();
269
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800270 const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(data);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800271
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800272 const ::std::array<double, 3> error =
273 GetError(info, data, &geometry[0], i);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800274
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800275 ::std::cout << i << ", ";
276 ::std::cout << extrinsic_params.z << "m, ";
277 ::std::cout << extrinsic_params.y << "m, ";
278 ::std::cout << extrinsic_params.r1 * 180 / M_PI << ", ";
279 ::std::cout << extrinsic_params.r2 * 180 / M_PI << ", ";
Parker Schuh9e1d1692019-02-24 14:34:04 -0800280 // TODO: Methodology problem: This should really have a separate solve for
281 // extrinsics...
Austin Schuh6cac52c2019-03-08 20:03:33 -0800282 ::std::cout << error[0] << "m, ";
283 ::std::cout << error[1] << "m, ";
284 ::std::cout << error[2] << "m" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800285 }
286 }
Parker Schuha4e52fb2019-02-24 18:18:15 -0800287
288 CameraCalibration results;
289 results.dataset = info;
290 results.intrinsics = IntrinsicParams::get(&intrinsics[0]);
291 results.geometry = CameraGeometry::get(&geometry[0]);
Austin Schuhd6cc1e92019-03-08 21:01:51 -0800292 DumpCameraConstants(FLAGS_constants.c_str(), info.camera_id, results);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800293}
294
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800295} // namespace y2019::vision
Parker Schuh9e1d1692019-02-24 14:34:04 -0800296
297int main(int argc, char **argv) { y2019::vision::main(argc, argv); }