blob: 840a2c26319a3994cc6a85623e429f9983d8c770 [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
148 // Use the solver to generate an intermediate version of our results.
149 std::vector<IntermediateResult> results;
150 for (const Target &target : target_list) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800151 const ::std::array<::aos::vision::Vector<2>, 8> target_value =
Austin Schuh2894e902019-03-03 21:12:46 -0800152 target.ToPointList();
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800153 const ::std::array<::aos::vision::Vector<2>, 8> template_value =
154 target_finder.GetTemplateTarget().ToPointList();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800155
Austin Schuh2894e902019-03-03 21:12:46 -0800156 // TODO(austin): Memory leak below, fix.
157 double *extrinsics = new double[ExtrinsicParams::kNumParams];
Parker Schuh9e1d1692019-02-24 14:34:04 -0800158 ExtrinsicParams().set(extrinsics);
159 all_extrinsics.push_back({std::unique_ptr<double[]>(extrinsics), i});
160
161 for (size_t j = 0; j < 8; ++j) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800162 // Template target in target space as documented by GetTemplateTarget()
163 const ::aos::vision::Vector<2> template_point = template_value[j];
164 // Target in pixel space.
165 const ::aos::vision::Vector<2> target_point = target_value[j];
Parker Schuh9e1d1692019-02-24 14:34:04 -0800166
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800167 // Now build up the residual block.
168 auto ftor = [template_point, target_point, i](
169 const double *const intrinsics, const double *const extrinsics,
170 double *residual) {
171 const IntrinsicParams intrinsic_params =
172 IntrinsicParams::get(intrinsics);
173 const ExtrinsicParams extrinsic_params =
174 ExtrinsicParams::get(extrinsics);
175 // Project the target location into pixel coordinates.
176 const ::aos::vision::Vector<2> projected_point =
177 Project(template_point, intrinsic_params, extrinsic_params);
178 const ::aos::vision::Vector<2> residual_point =
179 target_point - projected_point;
180 residual[0] = residual_point.x();
181 residual[1] = residual_point.y();
Parker Schuh9e1d1692019-02-24 14:34:04 -0800182 return true;
183 };
184 problem.AddResidualBlock(
185 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 2,
186 IntrinsicParams::kNumParams,
187 ExtrinsicParams::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800188 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800189 NULL, &intrinsics[0], extrinsics);
190 }
191
Austin Schuh6cac52c2019-03-08 20:03:33 -0800192 // Now, compare the estimated location of the target that we just solved
193 // for with the extrinsic params above with the known location of the
194 // target.
Parker Schuha4e52fb2019-02-24 18:18:15 -0800195 auto ftor = [&info, i](const double *const extrinsics,
196 const double *const geometry, double *residual) {
Austin Schuh6cac52c2019-03-08 20:03:33 -0800197 const ::std::array<double, 3> err =
198 GetError(info, extrinsics, geometry, i);
199 // We care a lot more about geometry than pixels. Especially since
200 // pixels are small and meters are big, so there's a small penalty to
201 // being off by meters.
202 residual[0] = err[0] * 1259.0;
203 residual[1] = err[1] * 1259.0;
204 residual[2] = err[2] * 1259.0;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800205 return true;
206 };
207
208 problem.AddResidualBlock(
209 new NumericDiffCostFunction<decltype(ftor), CENTRAL, 3,
210 ExtrinsicParams::kNumParams,
Parker Schuha4e52fb2019-02-24 18:18:15 -0800211 CameraGeometry::kNumParams>(
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800212 new decltype(ftor)(::std::move(ftor))),
Parker Schuh9e1d1692019-02-24 14:34:04 -0800213 NULL, extrinsics, &geometry[0]);
214 }
215 }
216 // TODO: Debug solver convergence?
217 Solve(options, &problem, &summary);
218 Solve(options, &problem, &summary);
219 Solve(options, &problem, &summary);
220
221 {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800222 ::std::cout << summary.BriefReport() << ::std::endl;
Austin Schuhcacc6922019-03-06 20:44:30 -0800223 IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]);
224 CameraGeometry geometry_ = CameraGeometry::get(&geometry[0]);
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800225 ::std::cout << "rup = " << intrinsics_.mount_angle * 180 / M_PI << ";"
226 << ::std::endl;
227 ::std::cout << "fl = " << intrinsics_.focal_length << ";" << ::std::endl;
228 ::std::cout << "error = " << summary.final_cost << ";" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800229
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800230 ::std::cout << "camera_angle = " << geometry_.heading * 180 / M_PI
231 << ::std::endl;
232 ::std::cout << "camera_x = " << geometry_.location[0] << ::std::endl;
233 ::std::cout << "camera_y = " << geometry_.location[1] << ::std::endl;
234 ::std::cout << "camera_z = " << geometry_.location[2] << ::std::endl;
235 ::std::cout << "camera_barrel = " << intrinsics_.barrel_mount * 180.0 / M_PI
236 << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800237
Austin Schuhcacc6922019-03-06 20:44:30 -0800238 for (const Sample &sample : all_extrinsics) {
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800239 const int i = sample.i;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800240 double *data = sample.extrinsics.get();
241
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800242 const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(data);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800243
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800244 const ::std::array<double, 3> error =
245 GetError(info, data, &geometry[0], i);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800246
Austin Schuh4d6e9bd2019-03-08 19:54:17 -0800247 ::std::cout << i << ", ";
248 ::std::cout << extrinsic_params.z << "m, ";
249 ::std::cout << extrinsic_params.y << "m, ";
250 ::std::cout << extrinsic_params.r1 * 180 / M_PI << ", ";
251 ::std::cout << extrinsic_params.r2 * 180 / M_PI << ", ";
Parker Schuh9e1d1692019-02-24 14:34:04 -0800252 // TODO: Methodology problem: This should really have a separate solve for
253 // extrinsics...
Austin Schuh6cac52c2019-03-08 20:03:33 -0800254 ::std::cout << error[0] << "m, ";
255 ::std::cout << error[1] << "m, ";
256 ::std::cout << error[2] << "m" << ::std::endl;
Parker Schuh9e1d1692019-02-24 14:34:04 -0800257 }
258 }
Parker Schuha4e52fb2019-02-24 18:18:15 -0800259
260 CameraCalibration results;
261 results.dataset = info;
262 results.intrinsics = IntrinsicParams::get(&intrinsics[0]);
263 results.geometry = CameraGeometry::get(&geometry[0]);
James Kuszmaule2c71ea2019-03-04 08:14:21 -0800264 DumpCameraConstants("y2019/vision/constants.cc", info.camera_id, results);
Parker Schuh9e1d1692019-02-24 14:34:04 -0800265}
266
267} // namespace y2019
268} // namespace vision
269
270int main(int argc, char **argv) { y2019::vision::main(argc, argv); }