47 #define MAX_COUNT_PT 50
49 #define MIN_SAMPLES_FIT 3
72 float parameters_u[3], parameters_v[3], min_error_u, min_error_v;
73 fit_linear_flow_field(vectors, count, n_iterations, error_threshold,
n_samples, parameters_u, parameters_v, &info->
fit_error, &min_error_u, &min_error_v, &info->
n_inliers_u, &info->
n_inliers_v);
103 void fit_linear_flow_field(
struct flow_t *vectors,
int count,
float error_threshold,
int n_iterations,
int n_samples,
float *parameters_u,
float *parameters_v,
float *fit_error,
float *min_error_u,
float *min_error_v,
int *n_inliers_u,
int *n_inliers_v)
112 int sam,
p, i_rand, si, add_si;
123 float _bu_all[count][1];
125 float _bv_all[count][1];
127 for (sam = 0; sam < count; sam++) {
128 AA[sam][0] = (float) vectors[sam].pos.x;
129 AA[sam][1] = (
float) vectors[sam].
pos.
y;
131 bu_all[sam][0] = (float) vectors[sam].flow_x;
132 bv_all[sam][0] = (float) vectors[sam].flow_y;
160 float PU[n_iterations * 3];
161 float PV[n_iterations * 3];
162 float errors_pu[n_iterations];
164 float errors_pv[n_iterations];
166 int n_inliers_pu[n_iterations];
167 int n_inliers_pv[n_iterations];
169 for (it = 0; it < n_iterations; it++) {
178 for (ii = 0; ii < i_rand; ii++) {
179 if (sample_indices[ii] == si) { add_si = 0; }
182 sample_indices[i_rand] = si;
189 A[sam][0] = (float) vectors[sample_indices[sam]].pos.x;
190 A[sam][1] = (
float) vectors[sample_indices[sam]].
pos.
y;
192 bu[sam][0] = (float) vectors[sample_indices[sam]].flow_x;
193 bv[sam][0] = (float) vectors[sample_indices[sam]].flow_y;
204 PU[it * 3] = pu[0][0];
205 PU[it * 3 + 1] = pu[1][0];
206 PU[it * 3 + 2] = pu[2][0];
210 PV[it * 3] = pv[0][0];
211 PV[it * 3 + 1] = pv[1][0];
212 PV[it * 3 + 2] = pv[2][0];
217 n_inliers_pu[it] = 0;
218 n_inliers_pv[it] = 0;
224 MAT_SUB(count, 1, C, bb, bu_all);
226 for (
p = 0;
p < count;
p++) {
227 C[
p][0] = abs(C[
p][0]);
228 if (C[
p][0] < error_threshold) {
229 errors_pu[it] += C[
p][0];
232 errors_pu[it] += error_threshold;
240 MAT_SUB(count, 1, C, bb, bv_all);
242 for (
p = 0;
p < count;
p++) {
243 C[
p][0] = abs(C[
p][0]);
244 if (C[
p][0] < error_threshold) {
245 errors_pv[it] += C[
p][0];
248 errors_pv[it] += error_threshold;
258 *min_error_u = (float)errors_pu[0];
259 for (it = 1; it < n_iterations; it++) {
260 if (errors_pu[it] < *min_error_u) {
261 *min_error_u = (float)errors_pu[it];
265 for (param = 0; param < 3; param++) {
266 parameters_u[param] = PU[min_ind * 3 + param];
268 *n_inliers_u = n_inliers_pu[min_ind];
272 *min_error_v = (float)errors_pv[0];
273 for (it = 0; it < n_iterations; it++) {
274 if (errors_pv[it] < *min_error_v) {
275 *min_error_v = (float)errors_pv[it];
279 for (param = 0; param < 3; param++) {
280 parameters_v[param] = PV[min_ind * 3 + param];
282 *n_inliers_v = n_inliers_pv[min_ind];
288 MAT_SUB(count, 1, C, bb, bu_all);
290 for (
p = 0;
p < count;
p++) {
291 *min_error_u += abs(C[
p][0]);
296 MAT_SUB(count, 1, C, bb, bv_all);
298 for (
p = 0;
p < count;
p++) {
299 *min_error_v += abs(C[
p][0]);
301 *fit_error = (*min_error_u + *min_error_v) / (2 * count);
326 info->
relative_velocity_x = -(parameters_u[2] + (im_width / 2.0f) * parameters_u[0] + (im_height / 2.0
f) * parameters_u[1]);
327 info->
relative_velocity_y = -(parameters_v[2] + (im_width / 2.0f) * parameters_v[0] + (im_height / 2.0
f) * parameters_v[1]);
333 float threshold_slope = 1.0;
336 if (abs(parameters_v[1]) < eta && arv_y < threshold_slope && arv_x >= 2 * threshold_slope) {
339 }
else if (arv_y >= 2 * threshold_slope) {
348 if (abs(parameters_u[0]) < eta && arv_x < threshold_slope && arv_y >= 2 * threshold_slope) {
351 }
else if (arv_x >= 2 * threshold_slope) {
364 float denominator = parameters_v[0] * parameters_u[1] - parameters_u[0] * parameters_v[1];
365 if (abs(denominator) > 1
E-5) {
366 info->
focus_of_expansion_x = ((parameters_u[2] * parameters_v[1] - parameters_v[2] * parameters_u[1]) / denominator);
369 denominator = parameters_u[1];
370 if (abs(denominator) > 1
E-5) {