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;
117 n_samples = (n_samples < count) ? n_samples : count;
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++) {
175 while (i_rand < n_samples) {
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.0f) * parameters_u[1]);
327 info->
relative_velocity_y = -(parameters_v[2] + (im_width / 2.0f) * parameters_v[0] + (im_height / 2.0f) * 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) {
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.
bool 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.
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 in subpixels.
float slope_x
Slope of the surface in x-direction - given sufficient lateral motion.
uint32_t y
The y coordinate of the point.
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 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.
#define MAT_MUL(_i, _k, _j, C, A, B)