blob: e917b2e3395949f6ee2d38326434176c51bba34b [file] [log] [blame]
Parker Schuhf7481be2017-03-04 18:24:33 -08001#include "y2017/vision/target_finder.h"
2
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cmath>
Parker Schuhf7481be2017-03-04 18:24:33 -08004
5namespace y2017 {
6namespace vision {
7
8// Blobs now come in three types:
9// 0) normal blob.
10// 1) first part of a split blob.
11// 2) second part of a split blob.
12void ComputeXShiftPolynomial(int type, const RangeImage &img,
13 TargetComponent *target) {
14 target->img = &img;
15 RangeImage t_img = Transpose(img);
16 int spacing = 10;
17 int n = t_img.size() - spacing * 2;
18 target->n = n;
19 if (n <= 0) {
20 printf("Empty blob aborting (%d).\n", n);
21 return;
22 }
23 Eigen::MatrixXf A = Eigen::MatrixXf::Zero(n * 2, 4);
24 Eigen::VectorXf b = Eigen::VectorXf::Zero(n * 2);
25 int i = 0;
26 for (const auto &row : t_img) {
27 // We decided this was a split target, but this is not a split row.
28 if (i >= spacing && i - spacing < n) {
29 int j = (i - spacing) * 2;
30 // normal blob or the first part of a split.
31 if (type == 0 || type == 1) {
32 b(j) = row[0].st;
33 } else {
34 b(j) = row[1].st;
35 }
36 A(j, 0) = (i) * (i);
37 A(j, 1) = (i);
38 A(j, 2) = 1;
39 ++j;
40 // normal target or the second part of a split.
41 if (type == 0 || type == 2) {
42 b(j) = row[row.size() - 1].ed;
43 } else {
44 b(j) = row[0].ed;
45 }
46 A(j, 0) = i * i;
47 A(j, 1) = i;
48 A(j, 3) = 1;
49 }
50 ++i;
51 }
52 Eigen::VectorXf sol =
53 A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
54 target->a = sol(0);
55 target->b = sol(1);
56 target->c_0 = sol(2);
57 target->c_1 = sol(3);
58
59 target->mini = t_img.min_y();
60
61 Eigen::VectorXf base = A * sol;
62 Eigen::VectorXf error_v = b - base;
63 target->fit_error = error_v.dot(error_v);
64}
65
66double TargetFinder::DetectConnectedTarget(const RangeImage &img) {
67 using namespace aos::vision;
68 RangeImage t_img = Transpose(img);
69 int total = 0;
70 int split = 0;
Parker Schuhf7481be2017-03-04 18:24:33 -080071 for (const auto &row : t_img) {
72 if (row.size() == 1) {
73 total++;
74 } else if (row.size() == 2) {
75 split++;
76 }
Parker Schuhf7481be2017-03-04 18:24:33 -080077 }
78 return (double)split / total;
79}
80
81std::vector<TargetComponent> TargetFinder::FillTargetComponentList(
82 const BlobList &blobs) {
83 std::vector<TargetComponent> list;
84 TargetComponent newTarg;
85 for (std::size_t i = 0; i < blobs.size(); ++i) {
86 double split_ratio;
87 if ((split_ratio = DetectConnectedTarget(blobs[i])) > 0.50) {
88 // Split type blob, do it two parts.
89 ComputeXShiftPolynomial(1, blobs[i], &newTarg);
90 list.emplace_back(newTarg);
91 ComputeXShiftPolynomial(2, blobs[i], &newTarg);
92 list.emplace_back(newTarg);
93 } else {
94 // normal type blob.
95 ComputeXShiftPolynomial(0, blobs[i], &newTarg);
96 list.emplace_back(newTarg);
97 }
98 }
99
100 return list;
101}
102
103aos::vision::RangeImage TargetFinder::Threshold(aos::vision::ImagePtr image) {
Brian Silverman37b15b32019-03-10 13:30:18 -0700104 return aos::vision::ThresholdImageWithFunction(
105 image, [&](aos::vision::PixelRef px) {
106 if (px.g > 88) {
107 uint8_t min = std::min(px.b, px.r);
108 uint8_t max = std::max(px.b, px.r);
109 if (min >= px.g || max >= px.g) return false;
110 uint8_t a = px.g - min;
111 uint8_t b = px.g - max;
112 return (a > 10 && b > 10);
113 }
114 return false;
115 });
Parker Schuhf7481be2017-03-04 18:24:33 -0800116}
117
118void TargetFinder::PreFilter(BlobList &imgs) {
119 imgs.erase(std::remove_if(imgs.begin(), imgs.end(),
120 [](RangeImage &img) {
121 // We can drop images with a small number of
122 // pixels, but images
123 // must be over 20px or the math will have issues.
124 return (img.npixels() < 100 || img.height() < 25);
125 }),
126 imgs.end());
127}
128
129bool TargetFinder::FindTargetFromComponents(
130 std::vector<TargetComponent> component_list, Target *final_target) {
131 using namespace aos::vision;
132 if (component_list.size() < 2 || final_target == NULL) {
133 // We don't enough parts for a traget.
134 return false;
135 }
136
137 // A0 * c + A1*s = b
138 Eigen::MatrixXf A = Eigen::MatrixXf::Zero(4, 2);
139 // A0: Offset component will be constant across all equations.
140 A(0, 0) = 1;
141 A(1, 0) = 1;
142 A(2, 0) = 1;
143 A(3, 0) = 1;
144
145 // A1: s defines the scaling and defines an expexted target.
146 // So these are the center heights of the top and bottom of the two targets.
147 A(0, 1) = -1;
148 A(1, 1) = 0;
149 A(2, 1) = 2;
150 A(3, 1) = 4;
151
152 // Track which pair is the best fit.
153 double best_error = -1;
154 double best_offset = -1;
155 Eigen::VectorXf best_v;
156 // Write down the two indicies.
157 std::pair<int, int> selected;
158 // We are regressing the combined estimated center, might write that down.
Austin Schuh218a7552017-03-22 21:15:28 -0700159 double regressed_y_center = 0;
Parker Schuhf7481be2017-03-04 18:24:33 -0800160
161 Eigen::VectorXf b = Eigen::VectorXf::Zero(4);
162 for (size_t i = 0; i < component_list.size(); i++) {
163 for (size_t j = 0; j < component_list.size(); j++) {
164 if (i == j) {
165 continue;
166 } else {
167 if (component_list[i].a < 0.0 || component_list[j].a < 0.0) {
168 // one of the targets is upside down (ie curved up), this can't
169 // happen.
170 continue;
171 }
172 // b is the target offests.
173 b(0) = component_list[j].EvalMinTop();
174 b(1) = component_list[j].EvalMinBot();
175 b(2) = component_list[i].EvalMinTop();
176 b(3) = component_list[i].EvalMinBot();
177 }
178
179 Eigen::VectorXf sol =
180 A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
181
182 Eigen::VectorXf base = A * sol;
183 Eigen::VectorXf error_v = b - base;
184 double error = error_v.dot(error_v);
185 // offset in scrren x of the two targets.
186 double offset = std::abs(component_list[i].CenterPolyOne() -
187 component_list[j].CenterPolyOne());
188 // How much do we care about offset. As far as I've seen, error is like
189 // 5-20, offset are around 10. Value selected for worst garbage can image.
190 const double offsetWeight = 2.1;
191 error += offsetWeight * offset;
Tyler Chatow63b91ec2017-10-15 13:19:21 -0700192 if ((best_error < 0 || error < best_error) && !::std::isnan(error)) {
Parker Schuhf7481be2017-03-04 18:24:33 -0800193 best_error = error;
194 best_offset = offset;
195 best_v = error_v;
196 selected.first = i;
197 selected.second = j;
198 regressed_y_center = sol(0);
199 }
200 }
201 }
202
203 // If we missed or the error is ridiculous just give up here.
Tyler Chatow63b91ec2017-10-15 13:19:21 -0700204 if (best_error < 0 || best_error > 300.0 || ::std::isnan(best_error)) {
Parker Schuhf7481be2017-03-04 18:24:33 -0800205 fprintf(stderr, "Bogus target dude (%f).\n", best_error);
206 return false;
207 }
208
209 fprintf(stderr,
210 "Selected (%d, %d):\n\t"
211 "err(%.2f, %.2f, %.2f, %.2f)(%.2f)(%.2f).\n\t"
212 "c00(%.2f, %.2f)(%.2f)\n",
213 selected.first, selected.second, best_v(0), best_v(1), best_v(2),
214 best_v(3), best_error, best_offset,
215 component_list[selected.first].CenterPolyOne(),
216 component_list[selected.second].CenterPolyOne(),
217 component_list[selected.first].CenterPolyOne() -
218 component_list[selected.second].CenterPolyOne());
219
220 double avgOff = (component_list[selected.first].mini +
221 component_list[selected.second].mini) /
222 2.0;
223 double avgOne = (component_list[selected.first].CenterPolyOne() +
224 component_list[selected.second].CenterPolyOne()) /
225 2.0;
226
227 final_target->screen_coord.x(avgOne + avgOff);
228 final_target->screen_coord.y(regressed_y_center);
229 final_target->comp1 = component_list[selected.first];
230 final_target->comp2 = component_list[selected.second];
231
232 return true;
233}
234
Parker Schuhabb6b6c2017-03-11 16:31:24 -0800235namespace {
236
237constexpr double kInchesToMeters = 0.0254;
238
239} // namespace
240
241void RotateAngle(aos::vision::Vector<2> vec, double angle, double *rx,
242 double *ry) {
243 double cos_ang = std::cos(angle);
244 double sin_ang = std::sin(angle);
245 *rx = vec.x() * cos_ang - vec.y() * sin_ang;
246 *ry = vec.x() * sin_ang + vec.y() * cos_ang;
247}
248
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700249void TargetFinder::GetAngleDist(const aos::vision::Vector<2> &target,
Parker Schuhabb6b6c2017-03-11 16:31:24 -0800250 double down_angle, double *dist,
251 double *angle) {
252 // TODO(ben): Will put all these numbers in a config file before
253 // the first competition. I hope.
254 double focal_length = 1418.6;
255 double mounted_angle_deg = 33.5;
256 double camera_angle = mounted_angle_deg * M_PI / 180.0 - down_angle;
257 double window_height = 960.0;
258 double window_width = 1280.0;
259
260 double target_height = 78.0;
261 double camera_height = 21.5;
262 double tape_width = 2;
263 double world_height = tape_width + target_height - camera_height;
264
265 double target_to_boiler = 9.5;
266 double robot_to_camera = 9.5;
267 double added_dist = target_to_boiler + robot_to_camera;
268
269 double px = target.x() - window_width / 2.0;
270 double py = target.y() - window_height / 2.0;
271 double pz = focal_length;
272 RotateAngle(aos::vision::Vector<2>(pz, -py), camera_angle, &pz, &py);
273 double pl = std::sqrt(pz * pz + px * px);
274
275 *dist = kInchesToMeters * (world_height * pl / py - added_dist);
Austin Schuh218a7552017-03-22 21:15:28 -0700276 *angle = -std::atan2(px, pz);
Parker Schuhabb6b6c2017-03-11 16:31:24 -0800277}
278
Parker Schuhf7481be2017-03-04 18:24:33 -0800279} // namespace vision
280} // namespace y2017