47 #define MAX_COUNT_PT 50
49 #define MIN_SAMPLES_FIT 3
74 float parameters_u[3], parameters_v[3], min_error_u, min_error_v;
75 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);
105 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)
114 int sam,
p, i_rand, si, add_si;
119 n_samples = (n_samples < count) ? n_samples : count;
125 float _bu_all[count][1];
127 float _bv_all[count][1];
129 for (sam = 0; sam < count; sam++) {
130 AA[sam][0] = (float) vectors[sam].pos.x;
131 AA[sam][1] = (
float) vectors[sam].
pos.
y;
133 bu_all[sam][0] = (float) vectors[sam].flow_x;
134 bv_all[sam][0] = (float) vectors[sam].flow_y;
148 float _A[n_samples][3];
150 float _bu[n_samples][1];
152 float _bv[n_samples][1];
154 float w[n_samples], _v[3][3];
162 float PU[n_iterations * 3];
163 float PV[n_iterations * 3];
164 float errors_pu[n_iterations];
166 float errors_pv[n_iterations];
168 int n_inliers_pu[n_iterations];
169 int n_inliers_pv[n_iterations];
171 for (it = 0; it < n_iterations; it++) {
173 int sample_indices[n_samples];
177 while (i_rand < n_samples) {
180 for (ii = 0; ii < i_rand; ii++) {
181 if (sample_indices[ii] == si) { add_si = 0; }
184 sample_indices[i_rand] = si;
190 for (sam = 0; sam < n_samples; sam++) {
191 A[sam][0] = (float) vectors[sample_indices[sam]].pos.x;
192 A[sam][1] = (
float) vectors[sample_indices[sam]].
pos.
y;
194 bu[sam][0] = (float) vectors[sample_indices[sam]].flow_x;
195 bv[sam][0] = (float) vectors[sample_indices[sam]].flow_y;
206 PU[it * 3] = pu[0][0];
207 PU[it * 3 + 1] = pu[1][0];
208 PU[it * 3 + 2] = pu[2][0];
212 PV[it * 3] = pv[0][0];
213 PV[it * 3 + 1] = pv[1][0];
214 PV[it * 3 + 2] = pv[2][0];
219 n_inliers_pu[it] = 0;
220 n_inliers_pv[it] = 0;
226 MAT_SUB(count, 1, C, bb, bu_all);
228 for (p = 0; p < count; p++) {
229 C[
p][0] = abs(C[p][0]);
230 if (C[p][0] < error_threshold) {
231 errors_pu[it] += C[
p][0];
234 errors_pu[it] += error_threshold;
242 MAT_SUB(count, 1, C, bb, bv_all);
244 for (p = 0; p < count; p++) {
245 C[
p][0] = abs(C[p][0]);
246 if (C[p][0] < error_threshold) {
247 errors_pv[it] += C[
p][0];
250 errors_pv[it] += error_threshold;
260 *min_error_u = (float)errors_pu[0];
261 for (it = 1; it < n_iterations; it++) {
262 if (errors_pu[it] < *min_error_u) {
263 *min_error_u = (float)errors_pu[it];
267 for (param = 0; param < 3; param++) {
268 parameters_u[param] = PU[min_ind * 3 + param];
270 *n_inliers_u = n_inliers_pu[min_ind];
274 *min_error_v = (float)errors_pv[0];
275 for (it = 0; it < n_iterations; it++) {
276 if (errors_pv[it] < *min_error_v) {
277 *min_error_v = (float)errors_pv[it];
281 for (param = 0; param < 3; param++) {
282 parameters_v[param] = PV[min_ind * 3 + param];
284 *n_inliers_v = n_inliers_pv[min_ind];
290 MAT_SUB(count, 1, C, bb, bu_all);
292 for (p = 0; p < count; p++) {
293 *min_error_u += abs(C[p][0]);
298 MAT_SUB(count, 1, C, bb, bv_all);
300 for (p = 0; p < count; p++) {
301 *min_error_v += abs(C[p][0]);
303 *fit_error = (*min_error_u + *min_error_v) / (2 * count);
328 info->
relative_velocity_x = -(parameters_u[2] + (im_width / 2.0f) * parameters_u[0] + (im_height / 2.0f) * parameters_u[1]);
329 info->
relative_velocity_y = -(parameters_v[2] + (im_width / 2.0f) * parameters_v[0] + (im_height / 2.0f) * parameters_v[1]);
335 float threshold_slope = 1.0;
338 if (abs(parameters_v[1]) < eta && arv_y < threshold_slope && arv_x >= 2 * threshold_slope) {
341 }
else if (arv_y >= 2 * threshold_slope) {
350 if (abs(parameters_u[0]) < eta && arv_x < threshold_slope && arv_y >= 2 * threshold_slope) {
353 }
else if (arv_x >= 2 * threshold_slope) {
366 float denominator = parameters_v[0] * parameters_u[1] - parameters_u[0] * parameters_v[1];
367 if (abs(denominator) > 1
E-5) {
368 info->
focus_of_expansion_x = ((parameters_u[2] * parameters_v[1] - parameters_v[2] * parameters_u[1]) / denominator);
371 denominator = parameters_u[1];
372 if (abs(denominator) > 1
E-5) {
float relative_velocity_y
Relative velocity in y-direction, i.e., vy / z, where z is the depth in direction of the camera's pri...
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)
Analyze a linear flow field, retrieving information such as divergence, surface roughness, focus of expansion, etc.
Simple matrix helper macros.
#define MAKE_MATRIX_PTR(_ptr, _mat, _rows)
Make a pointer to a matrix of _rows lines.
float fit_error
Error of the fit (same as surface roughness)
int n_inliers_u
Number of inliers in the horizontal flow fit.
float relative_velocity_z
Relative velocity in z-direction, i.e., vz / z, where z is the depth in direction of the camera's pri...
float time_to_contact
Basically, 1 / relative_velocity_z.
float focus_of_expansion_x
Image x-coordinate of the focus of expansion (contraction)
float focus_of_expansion_y
Image y-coordinate of the focus of expansion (contraction)
Paparazzi floating point algebra.
float slope_y
Slope of the surface in y-direction - given sufficient lateral motion.
void pprz_svd_solve_float(float **x, float **u, float *w, float **v, float **b, int m, int n, int l)
SVD based linear solver.
float relative_velocity_x
Relative velocity in x-direction, i.e., vx / z, where z is the depth in direction of the camera's pri...
struct point_t pos
The original position the flow comes from.
float slope_x
Slope of the surface in x-direction - given sufficient lateral motion.
void extract_information_from_parameters(float *parameters_u, float *parameters_v, int im_width, int im_height, struct linear_flow_fit_info *info)
Extract information from the parameters that were fit to the optical flow field.
Matrix decompositions in floating point.
int analyze_linear_flow_field(struct flow_t *vectors, int count, float error_threshold, int n_iterations, int n_samples, int im_width, int im_height, struct linear_flow_fit_info *info)
Analyze a linear flow field, retrieving information such as divergence, surface roughness, focus of expansion, etc.
int n_inliers_v
Number of inliers in the vertical flow fit.
float divergence
Basically, relative_velocity_z. Actual divergence of a 2D flow field is 2 * relative_velocity_z.
int pprz_svd_float(float **a, float *w, float **v, int m, int n)
SVD decomposition.
#define MAT_SUB(_i, _j, C, A, B)
float surface_roughness
The error of the linear fit is a measure of surface roughness.
uint16_t y
The y coordinate of the point.
#define MAT_MUL(_i, _k, _j, C, A, B)