Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 1 | #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 Schuh | e623f9f | 2019-03-03 12:24:03 -0800 | [diff] [blame] | 20 | DEFINE_int32(camera_id, -1, "The camera ID to calibrate"); |
| 21 | |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 22 | using ::aos::events::DataSocket; |
| 23 | using ::aos::events::RXUdpSocket; |
| 24 | using ::aos::events::TCPServer; |
| 25 | using ::aos::vision::DataRef; |
| 26 | using ::aos::vision::Int32Codec; |
| 27 | using ::aos::monotonic_clock; |
| 28 | using aos::vision::Segment; |
| 29 | |
| 30 | using ceres::NumericDiffCostFunction; |
| 31 | using ceres::CENTRAL; |
| 32 | using ceres::CostFunction; |
| 33 | using ceres::Problem; |
| 34 | using ceres::Solver; |
| 35 | using ceres::Solve; |
| 36 | |
| 37 | namespace y2019 { |
| 38 | namespace vision { |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 39 | namespace { |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 40 | |
| 41 | constexpr double kInchesToMeters = 0.0254; |
| 42 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 43 | } // namespace |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 45 | ::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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 50 | |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 51 | const double s = sin(geo.heading + extrinsic_params.r2); |
| 52 | const double c = cos(geo.heading + extrinsic_params.r2); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 53 | |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 54 | // 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 Schuh | 6cac52c | 2019-03-08 20:03:33 -0800 | [diff] [blame^] | 57 | 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 Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 65 | |
Austin Schuh | 6cac52c | 2019-03-08 20:03:33 -0800 | [diff] [blame^] | 66 | constexpr double kCalibrationTargetHeight = 28.5 * kInchesToMeters; |
| 67 | const double dz = |
| 68 | kCalibrationTargetHeight - (geo.location[2] + extrinsic_params.y); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 69 | return {{dx, dy, dz}}; |
| 70 | } |
| 71 | |
| 72 | void main(int argc, char **argv) { |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 73 | using namespace y2019::vision; |
Austin Schuh | e623f9f | 2019-03-03 12:24:03 -0800 | [diff] [blame] | 74 | gflags::ParseCommandLineFlags(&argc, &argv, false); |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 75 | |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 76 | const char *base_directory = "/home/parker/data/frc/2019_calibration/"; |
| 77 | |
| 78 | DatasetInfo info = { |
Austin Schuh | e623f9f | 2019-03-03 12:24:03 -0800 | [diff] [blame] | 79 | FLAGS_camera_id, |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 80 | {{12.5 * kInchesToMeters, 0.5 * kInchesToMeters}}, |
| 81 | {{kInchesToMeters, 0.0}}, |
| 82 | 26, |
| 83 | "cam5_0/debug_viewer_jpeg_", |
| 84 | 59, |
| 85 | }; |
| 86 | |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 87 | ::aos::logging::Init(); |
| 88 | ::aos::logging::AddImplementation( |
| 89 | new ::aos::logging::StreamLogImplementation(stderr)); |
| 90 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 91 | TargetFinder target_finder; |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 92 | |
| 93 | ceres::Problem problem; |
| 94 | |
| 95 | struct Sample { |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 96 | ::std::unique_ptr<double[]> extrinsics; |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 97 | int i; |
| 98 | }; |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 99 | ::std::vector<Sample> all_extrinsics; |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 100 | double intrinsics[IntrinsicParams::kNumParams]; |
| 101 | IntrinsicParams().set(&intrinsics[0]); |
| 102 | |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 103 | double geometry[CameraGeometry::kNumParams]; |
| 104 | CameraGeometry().set(&geometry[0]); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 105 | |
| 106 | Solver::Options options; |
| 107 | options.minimizer_progress_to_stdout = false; |
| 108 | Solver::Summary summary; |
| 109 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 110 | ::std::cout << summary.BriefReport() << "\n"; |
Austin Schuh | cacc692 | 2019-03-06 20:44:30 -0800 | [diff] [blame] | 111 | IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]); |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 112 | ::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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 115 | |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 116 | 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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 120 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 121 | const ::aos::vision::ImageFormat fmt{640, 480}; |
| 122 | ::aos::vision::BlobList imgs = |
| 123 | ::aos::vision::FindBlobs(aos::vision::DoThresholdYUYV( |
Austin Schuh | 2851e7f | 2019-03-06 20:45:19 -0800 | [diff] [blame] | 124 | fmt, frame.data.data(), TargetFinder::GetThresholdValue())); |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 125 | target_finder.PreFilter(&imgs); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 126 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 127 | constexpr bool verbose = false; |
| 128 | ::std::vector<std::vector<Segment<2>>> raw_polys; |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 129 | for (const RangeImage &blob : imgs) { |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 130 | // Convert blobs to contours in the corrected space. |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 131 | 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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 136 | raw_polys.push_back(polygon); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Calculate each component side of a possible target. |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 141 | const ::std::vector<TargetComponent> target_component_list = |
| 142 | target_finder.FillTargetComponentList(raw_polys); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 143 | |
| 144 | // Put the compenents together into targets. |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 145 | const ::std::vector<Target> target_list = |
| 146 | target_finder.FindTargetsFromComponents(target_component_list, verbose); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 147 | |
| 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 Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 151 | const ::std::array<::aos::vision::Vector<2>, 8> target_value = |
Austin Schuh | 2894e90 | 2019-03-03 21:12:46 -0800 | [diff] [blame] | 152 | target.ToPointList(); |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 153 | const ::std::array<::aos::vision::Vector<2>, 8> template_value = |
| 154 | target_finder.GetTemplateTarget().ToPointList(); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 155 | |
Austin Schuh | 2894e90 | 2019-03-03 21:12:46 -0800 | [diff] [blame] | 156 | // TODO(austin): Memory leak below, fix. |
| 157 | double *extrinsics = new double[ExtrinsicParams::kNumParams]; |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 158 | 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 Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 162 | // 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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 166 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 167 | // 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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 182 | return true; |
| 183 | }; |
| 184 | problem.AddResidualBlock( |
| 185 | new NumericDiffCostFunction<decltype(ftor), CENTRAL, 2, |
| 186 | IntrinsicParams::kNumParams, |
| 187 | ExtrinsicParams::kNumParams>( |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 188 | new decltype(ftor)(::std::move(ftor))), |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 189 | NULL, &intrinsics[0], extrinsics); |
| 190 | } |
| 191 | |
Austin Schuh | 6cac52c | 2019-03-08 20:03:33 -0800 | [diff] [blame^] | 192 | // 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 Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 195 | auto ftor = [&info, i](const double *const extrinsics, |
| 196 | const double *const geometry, double *residual) { |
Austin Schuh | 6cac52c | 2019-03-08 20:03:33 -0800 | [diff] [blame^] | 197 | 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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 205 | return true; |
| 206 | }; |
| 207 | |
| 208 | problem.AddResidualBlock( |
| 209 | new NumericDiffCostFunction<decltype(ftor), CENTRAL, 3, |
| 210 | ExtrinsicParams::kNumParams, |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 211 | CameraGeometry::kNumParams>( |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 212 | new decltype(ftor)(::std::move(ftor))), |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 213 | 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 Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 222 | ::std::cout << summary.BriefReport() << ::std::endl; |
Austin Schuh | cacc692 | 2019-03-06 20:44:30 -0800 | [diff] [blame] | 223 | IntrinsicParams intrinsics_ = IntrinsicParams::get(&intrinsics[0]); |
| 224 | CameraGeometry geometry_ = CameraGeometry::get(&geometry[0]); |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 225 | ::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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 229 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 230 | ::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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 237 | |
Austin Schuh | cacc692 | 2019-03-06 20:44:30 -0800 | [diff] [blame] | 238 | for (const Sample &sample : all_extrinsics) { |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 239 | const int i = sample.i; |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 240 | double *data = sample.extrinsics.get(); |
| 241 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 242 | const ExtrinsicParams extrinsic_params = ExtrinsicParams::get(data); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 243 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 244 | const ::std::array<double, 3> error = |
| 245 | GetError(info, data, &geometry[0], i); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 246 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 247 | ::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 Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 252 | // TODO: Methodology problem: This should really have a separate solve for |
| 253 | // extrinsics... |
Austin Schuh | 6cac52c | 2019-03-08 20:03:33 -0800 | [diff] [blame^] | 254 | ::std::cout << error[0] << "m, "; |
| 255 | ::std::cout << error[1] << "m, "; |
| 256 | ::std::cout << error[2] << "m" << ::std::endl; |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 259 | |
| 260 | CameraCalibration results; |
| 261 | results.dataset = info; |
| 262 | results.intrinsics = IntrinsicParams::get(&intrinsics[0]); |
| 263 | results.geometry = CameraGeometry::get(&geometry[0]); |
James Kuszmaul | e2c71ea | 2019-03-04 08:14:21 -0800 | [diff] [blame] | 264 | DumpCameraConstants("y2019/vision/constants.cc", info.camera_id, results); |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | } // namespace y2019 |
| 268 | } // namespace vision |
| 269 | |
| 270 | int main(int argc, char **argv) { y2019::vision::main(argc, argv); } |