blob: e39fe73e1903f7be03e5474b57400e4597652886 [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) {
106 return aos::vision::DoThreshold(image, [&](aos::vision::PixelRef &px) {
107 if (px.g > 88) {
108 return true;
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 });
118}
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.
161 double regressed_y_center;
162
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;
194 if ((best_error < 0 || error < best_error) && !isnan(error)) {
195 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.
206 if (best_error < 0 || best_error > 300.0 || isnan(best_error)) {
207 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
237} // namespace vision
238} // namespace y2017