blob: 91e801e0235434fed34b63b7fa81b74e838cc263 [file] [log] [blame]
Parker Schuhf7481be2017-03-04 18:24:33 -08001#include "y2017/vision/target_finder.h"
2
3#include <math.h>
4
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;
71 int count = t_img.mini();
72 for (const auto &row : t_img) {
73 if (row.size() == 1) {
74 total++;
75 } else if (row.size() == 2) {
76 split++;
77 }
78 count++;
79 }
80 return (double)split / total;
81}
82
83std::vector<TargetComponent> TargetFinder::FillTargetComponentList(
84 const BlobList &blobs) {
85 std::vector<TargetComponent> list;
86 TargetComponent newTarg;
87 for (std::size_t i = 0; i < blobs.size(); ++i) {
88 double split_ratio;
89 if ((split_ratio = DetectConnectedTarget(blobs[i])) > 0.50) {
90 // Split type blob, do it two parts.
91 ComputeXShiftPolynomial(1, blobs[i], &newTarg);
92 list.emplace_back(newTarg);
93 ComputeXShiftPolynomial(2, blobs[i], &newTarg);
94 list.emplace_back(newTarg);
95 } else {
96 // normal type blob.
97 ComputeXShiftPolynomial(0, blobs[i], &newTarg);
98 list.emplace_back(newTarg);
99 }
100 }
101
102 return list;
103}
104
105aos::vision::RangeImage TargetFinder::Threshold(aos::vision::ImagePtr image) {
Brian Silverman37b15b32019-03-10 13:30:18 -0700106 return aos::vision::ThresholdImageWithFunction(
107 image, [&](aos::vision::PixelRef px) {
108 if (px.g > 88) {
109 uint8_t min = std::min(px.b, px.r);
110 uint8_t max = std::max(px.b, px.r);
111 if (min >= px.g || max >= px.g) return false;
112 uint8_t a = px.g - min;
113 uint8_t b = px.g - max;
114 return (a > 10 && b > 10);
115 }
116 return false;
117 });
Parker Schuhf7481be2017-03-04 18:24:33 -0800118}
119
120void TargetFinder::PreFilter(BlobList &imgs) {
121 imgs.erase(std::remove_if(imgs.begin(), imgs.end(),
122 [](RangeImage &img) {
123 // We can drop images with a small number of
124 // pixels, but images
125 // must be over 20px or the math will have issues.
126 return (img.npixels() < 100 || img.height() < 25);
127 }),
128 imgs.end());
129}
130
131bool TargetFinder::FindTargetFromComponents(
132 std::vector<TargetComponent> component_list, Target *final_target) {
133 using namespace aos::vision;
134 if (component_list.size() < 2 || final_target == NULL) {
135 // We don't enough parts for a traget.
136 return false;
137 }
138
139 // A0 * c + A1*s = b
140 Eigen::MatrixXf A = Eigen::MatrixXf::Zero(4, 2);
141 // A0: Offset component will be constant across all equations.
142 A(0, 0) = 1;
143 A(1, 0) = 1;
144 A(2, 0) = 1;
145 A(3, 0) = 1;
146
147 // A1: s defines the scaling and defines an expexted target.
148 // So these are the center heights of the top and bottom of the two targets.
149 A(0, 1) = -1;
150 A(1, 1) = 0;
151 A(2, 1) = 2;
152 A(3, 1) = 4;
153
154 // Track which pair is the best fit.
155 double best_error = -1;
156 double best_offset = -1;
157 Eigen::VectorXf best_v;
158 // Write down the two indicies.
159 std::pair<int, int> selected;
160 // We are regressing the combined estimated center, might write that down.
Austin Schuh218a7552017-03-22 21:15:28 -0700161 double regressed_y_center = 0;
Parker Schuhf7481be2017-03-04 18:24:33 -0800162
163 Eigen::VectorXf b = Eigen::VectorXf::Zero(4);
164 for (size_t i = 0; i < component_list.size(); i++) {
165 for (size_t j = 0; j < component_list.size(); j++) {
166 if (i == j) {
167 continue;
168 } else {
169 if (component_list[i].a < 0.0 || component_list[j].a < 0.0) {
170 // one of the targets is upside down (ie curved up), this can't
171 // happen.
172 continue;
173 }
174 // b is the target offests.
175 b(0) = component_list[j].EvalMinTop();
176 b(1) = component_list[j].EvalMinBot();
177 b(2) = component_list[i].EvalMinTop();
178 b(3) = component_list[i].EvalMinBot();
179 }
180
181 Eigen::VectorXf sol =
182 A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
183
184 Eigen::VectorXf base = A * sol;
185 Eigen::VectorXf error_v = b - base;
186 double error = error_v.dot(error_v);
187 // offset in scrren x of the two targets.
188 double offset = std::abs(component_list[i].CenterPolyOne() -
189 component_list[j].CenterPolyOne());
190 // How much do we care about offset. As far as I've seen, error is like
191 // 5-20, offset are around 10. Value selected for worst garbage can image.
192 const double offsetWeight = 2.1;
193 error += offsetWeight * offset;
Tyler Chatow63b91ec2017-10-15 13:19:21 -0700194 if ((best_error < 0 || error < best_error) && !::std::isnan(error)) {
Parker Schuhf7481be2017-03-04 18:24:33 -0800195 best_error = error;
196 best_offset = offset;
197 best_v = error_v;
198 selected.first = i;
199 selected.second = j;
200 regressed_y_center = sol(0);
201 }
202 }
203 }
204
205 // If we missed or the error is ridiculous just give up here.
Tyler Chatow63b91ec2017-10-15 13:19:21 -0700206 if (best_error < 0 || best_error > 300.0 || ::std::isnan(best_error)) {
Parker Schuhf7481be2017-03-04 18:24:33 -0800207 fprintf(stderr, "Bogus target dude (%f).\n", best_error);
208 return false;
209 }
210
211 fprintf(stderr,
212 "Selected (%d, %d):\n\t"
213 "err(%.2f, %.2f, %.2f, %.2f)(%.2f)(%.2f).\n\t"
214 "c00(%.2f, %.2f)(%.2f)\n",
215 selected.first, selected.second, best_v(0), best_v(1), best_v(2),
216 best_v(3), best_error, best_offset,
217 component_list[selected.first].CenterPolyOne(),
218 component_list[selected.second].CenterPolyOne(),
219 component_list[selected.first].CenterPolyOne() -
220 component_list[selected.second].CenterPolyOne());
221
222 double avgOff = (component_list[selected.first].mini +
223 component_list[selected.second].mini) /
224 2.0;
225 double avgOne = (component_list[selected.first].CenterPolyOne() +
226 component_list[selected.second].CenterPolyOne()) /
227 2.0;
228
229 final_target->screen_coord.x(avgOne + avgOff);
230 final_target->screen_coord.y(regressed_y_center);
231 final_target->comp1 = component_list[selected.first];
232 final_target->comp2 = component_list[selected.second];
233
234 return true;
235}
236
Parker Schuhabb6b6c2017-03-11 16:31:24 -0800237namespace {
238
239constexpr double kInchesToMeters = 0.0254;
240
241} // namespace
242
243void RotateAngle(aos::vision::Vector<2> vec, double angle, double *rx,
244 double *ry) {
245 double cos_ang = std::cos(angle);
246 double sin_ang = std::sin(angle);
247 *rx = vec.x() * cos_ang - vec.y() * sin_ang;
248 *ry = vec.x() * sin_ang + vec.y() * cos_ang;
249}
250
251void TargetFinder::GetAngleDist(const aos::vision::Vector<2>& target,
252 double down_angle, double *dist,
253 double *angle) {
254 // TODO(ben): Will put all these numbers in a config file before
255 // the first competition. I hope.
256 double focal_length = 1418.6;
257 double mounted_angle_deg = 33.5;
258 double camera_angle = mounted_angle_deg * M_PI / 180.0 - down_angle;
259 double window_height = 960.0;
260 double window_width = 1280.0;
261
262 double target_height = 78.0;
263 double camera_height = 21.5;
264 double tape_width = 2;
265 double world_height = tape_width + target_height - camera_height;
266
267 double target_to_boiler = 9.5;
268 double robot_to_camera = 9.5;
269 double added_dist = target_to_boiler + robot_to_camera;
270
271 double px = target.x() - window_width / 2.0;
272 double py = target.y() - window_height / 2.0;
273 double pz = focal_length;
274 RotateAngle(aos::vision::Vector<2>(pz, -py), camera_angle, &pz, &py);
275 double pl = std::sqrt(pz * pz + px * px);
276
277 *dist = kInchesToMeters * (world_height * pl / py - added_dist);
Austin Schuh218a7552017-03-22 21:15:28 -0700278 *angle = -std::atan2(px, pz);
Parker Schuhabb6b6c2017-03-11 16:31:24 -0800279}
280
Parker Schuhf7481be2017-03-04 18:24:33 -0800281} // namespace vision
282} // namespace y2017