Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
opticflow_calculator.c File Reference

Estimate velocity from optic flow. More...

#include "std.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "opticflow_calculator.h"
#include "lib/vision/image.h"
#include "lib/vision/lucas_kanade.h"
#include "lib/vision/fast_rosten.h"
#include "lib/vision/act_fast.h"
#include "lib/vision/edge_flow.h"
#include "lib/vision/undistortion.h"
#include "size_divergence.h"
#include "linear_flow_fit.h"
#include "modules/sonar/agl_dist.h"
#include <BOARD_CONFIG>
#include "filters/median_filter.h"
+ Include dependency graph for opticflow_calculator.c:

Go to the source code of this file.

Macros

#define OPTICFLOW_SHOW_CORNERS   0
 
#define EXHAUSTIVE_FAST   0
 
#define ACT_FAST   1
 
#define SIZE_DIV   1
 
#define LINEAR_FIT   1
 
#define OPTICFLOW_CORNER_METHOD   ACT_FAST
 
#define OPTICFLOW_MAX_TRACK_CORNERS   25
 
#define OPTICFLOW_WINDOW_SIZE   10
 
#define OPTICFLOW_SEARCH_DISTANCE   20
 
#define OPTICFLOW_SUBPIXEL_FACTOR   10
 
#define OPTICFLOW_RESOLUTION_FACTOR   100
 
#define OPTICFLOW_MAX_ITERATIONS   10
 
#define OPTICFLOW_THRESHOLD_VEC   2
 
#define OPTICFLOW_PYRAMID_LEVEL   2
 
#define OPTICFLOW_FAST9_ADAPTIVE   TRUE
 
#define OPTICFLOW_FAST9_THRESHOLD   20
 
#define OPTICFLOW_FAST9_MIN_DISTANCE   10
 
#define OPTICFLOW_FAST9_PADDING   20
 
#define FAST9_LOW_THRESHOLD   5
 
#define FAST9_HIGH_THRESHOLD   60
 
#define OPTICFLOW_METHOD   0
 
#define OPTICFLOW_DEROTATION   TRUE
 
#define OPTICFLOW_DEROTATION_CORRECTION_FACTOR_X   1.0
 
#define OPTICFLOW_DEROTATION_CORRECTION_FACTOR_Y   1.0
 
#define OPTICFLOW_MEDIAN_FILTER   FALSE
 
#define OPTICFLOW_FEATURE_MANAGEMENT   0
 
#define OPTICFLOW_FAST9_REGION_DETECT   1
 
#define OPTICFLOW_FAST9_NUM_REGIONS   9
 
#define OPTICFLOW_ACTFAST_LONG_STEP   10
 
#define OPTICFLOW_ACTFAST_SHORT_STEP   2
 
#define OPTICFLOW_ACTFAST_GRADIENT_METHOD   1
 
#define OPTICFLOW_ACTFAST_MIN_GRADIENT   10
 
#define OPTICFLOW_BODY_TO_CAM_PHI   0
 
#define OPTICFLOW_BODY_TO_CAM_THETA   0
 
#define OPTICFLOW_BODY_TO_CAM_PSI   -M_PI_2
 
#define OPTICFLOW_TRACK_BACK   FALSE
 
#define OPTICFLOW_SHOW_FLOW   FALSE
 

Functions

static uint32_t timeval_diff (struct timeval *starttime, struct timeval *finishtime)
 Calculate the difference from start till finish. More...
 
static int cmp_flow (const void *a, const void *b)
 Compare two flow vectors based on flow distance Used for sorting. More...
 
static int cmp_array (const void *a, const void *b)
 Compare the rows of an integer (uint16_t) 2D array based on the first column. More...
 
static void manage_flow_features (struct image_t *img, struct opticflow_t *opticflow, struct opticflow_result_t *result)
 
static struct flow_tpredict_flow_vectors (struct flow_t *flow_vectors, uint16_t n_points, float phi_diff, float theta_diff, float psi_diff, struct opticflow_t *opticflow)
 
void opticflow_calc_init (struct opticflow_t *opticflow)
 Initialize the opticflow calculator. More...
 
bool calc_fast9_lukas_kanade (struct opticflow_t *opticflow, struct image_t *img, struct opticflow_result_t *result)
 Run the optical flow with fast9 and lukaskanade on a new image frame. More...
 
bool calc_edgeflow_tot (struct opticflow_t *opticflow, struct image_t *img, struct opticflow_result_t *result)
 Run the optical flow with EDGEFLOW on a new image frame. More...
 
bool opticflow_calc_frame (struct opticflow_t *opticflow, struct image_t *img, struct opticflow_result_t *result)
 Run the optical flow on a new image frame. More...
 

Variables

uint16_t n_time_steps = 10
 
uint16_t n_agents = 25
 
struct MedianFilter3Float vel_filt
 
struct FloatRMat body_to_cam
 

Detailed Description

Estimate velocity from optic flow.

Definition in file opticflow_calculator.c.

Macro Definition Documentation

#define ACT_FAST   1

Definition at line 56 of file opticflow_calculator.c.

Referenced by calc_fast9_lukas_kanade().

#define EXHAUSTIVE_FAST   0

Definition at line 55 of file opticflow_calculator.c.

Referenced by calc_fast9_lukas_kanade().

#define FAST9_HIGH_THRESHOLD   60

Definition at line 136 of file opticflow_calculator.c.

Referenced by calc_fast9_lukas_kanade().

#define FAST9_LOW_THRESHOLD   5

Definition at line 135 of file opticflow_calculator.c.

Referenced by calc_fast9_lukas_kanade().

#define LINEAR_FIT   1

Definition at line 66 of file opticflow_calculator.c.

Referenced by calc_fast9_lukas_kanade().

#define OPTICFLOW_ACTFAST_GRADIENT_METHOD   1

Definition at line 193 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_ACTFAST_LONG_STEP   10

Definition at line 183 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_ACTFAST_MIN_GRADIENT   10

Definition at line 198 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_ACTFAST_SHORT_STEP   2

Definition at line 188 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_BODY_TO_CAM_PHI   0

Definition at line 204 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_BODY_TO_CAM_PSI   -M_PI_2

Definition at line 210 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_BODY_TO_CAM_THETA   0

Definition at line 207 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_CORNER_METHOD   ACT_FAST

Definition at line 69 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_DEROTATION   TRUE

Definition at line 148 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_DEROTATION_CORRECTION_FACTOR_X   1.0

Definition at line 153 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_DEROTATION_CORRECTION_FACTOR_Y   1.0

Definition at line 158 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_FAST9_ADAPTIVE   TRUE

Definition at line 115 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_FAST9_MIN_DISTANCE   10

Definition at line 125 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_FAST9_NUM_REGIONS   9

Definition at line 178 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_FAST9_PADDING   20

Definition at line 130 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_FAST9_REGION_DETECT   1

Definition at line 173 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_FAST9_THRESHOLD   20

Definition at line 120 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_FEATURE_MANAGEMENT   0

Definition at line 168 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_MAX_ITERATIONS   10

Definition at line 100 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_MAX_TRACK_CORNERS   25

Definition at line 75 of file opticflow_calculator.c.

Referenced by calc_fast9_lukas_kanade(), and opticflow_calc_init().

#define OPTICFLOW_MEDIAN_FILTER   FALSE

Definition at line 163 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_METHOD   0

Definition at line 139 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_PYRAMID_LEVEL   2

Definition at line 110 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_RESOLUTION_FACTOR   100

Definition at line 95 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_SEARCH_DISTANCE   20

Definition at line 85 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_SHOW_CORNERS   0

Definition at line 53 of file opticflow_calculator.c.

#define OPTICFLOW_SHOW_FLOW   FALSE

Definition at line 223 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_SUBPIXEL_FACTOR   10

Definition at line 90 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_THRESHOLD_VEC   2

Definition at line 105 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_TRACK_BACK   FALSE

Definition at line 216 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define OPTICFLOW_WINDOW_SIZE   10

Definition at line 80 of file opticflow_calculator.c.

Referenced by opticflow_calc_init().

#define SIZE_DIV   1

Definition at line 63 of file opticflow_calculator.c.

Referenced by calc_fast9_lukas_kanade().

Function Documentation

bool calc_edgeflow_tot ( struct opticflow_t opticflow,
struct image_t img,
struct opticflow_result_t result 
)

Run the optical flow with EDGEFLOW on a new image frame.

Parameters
[in]*opticflowThe opticalflow structure that keeps track of previous images
[in]*stateThe state of the drone
[in]*imgThe image frame to calculate the optical flow from
[out]*resultThe optical flow result
computationsuccessful

Definition at line 812 of file opticflow_calculator.c.

References agl_dist_value_filtered, calc_previous_frame_nr(), calculate_edge_displacement(), calculate_edge_histogram(), opticflow_result_t::corner_cnt, opticflow_t::derotation, opticflow_t::derotation_correction_factor_x, opticflow_t::derotation_correction_factor_y, DISP_RANGE_MAX, opticflow_result_t::div_size, edge_flow_t::div_x, edge_flow_t::div_y, opticflow_result_t::divergence, draw_edgeflow_img(), image_t::eulers, edge_hist_t::eulers, FLOAT_EULERS_ZERO, opticflow_result_t::flow_der_x, opticflow_result_t::flow_der_y, opticflow_result_t::flow_x, edge_flow_t::flow_x, opticflow_result_t::flow_y, edge_flow_t::flow_y, opticflow_result_t::fps, edge_hist_t::frame_time, getAmountPeaks(), image_t::h, opticflow_t::just_switched_method, line_fit(), MAX_HORIZON, MAX_WINDOW_SIZE, opticflow_t::median_filter, opticflow_result_t::noise_measurement, FloatEulers::phi, RES, opticflow_t::resolution_factor, opticflow_t::search_distance, opticflow_result_t::surface_roughness, FloatEulers::theta, timeval_diff(), opticflow_result_t::tracked_cnt, image_t::ts, UpdateMedianFilterVect3Float, opticflow_result_t::vel_cam, vel_filt, image_t::w, opticflow_t::window_size, FloatVect3::x, edge_hist_t::x, edgeflow_displacement_t::x, FloatVect3::y, edge_hist_t::y, edgeflow_displacement_t::y, and FloatVect3::z.

Referenced by opticflow_calc_frame().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool calc_fast9_lukas_kanade ( struct opticflow_t opticflow,
struct image_t img,
struct opticflow_result_t result 
)

Run the optical flow with fast9 and lukaskanade on a new image frame.

Parameters
[in]*opticflowThe opticalflow structure that keeps track of previous images
[in]*stateThe state of the drone
[in]*imgThe image frame to calculate the optical flow from
[out]*resultThe optical flow result
Returns
Was optical flow successful

Definition at line 297 of file opticflow_calculator.c.

References act_fast(), ACT_FAST, opticflow_t::actfast_gradient_method, opticflow_t::actfast_long_step, opticflow_t::actfast_min_gradient, opticflow_t::actfast_short_step, agl_dist_value_filtered, analyze_linear_flow_field(), cmp_flow(), opticflow_result_t::corner_cnt, opticflow_t::corner_method, point_t::count, opticflow_t::derotation, opticflow_t::derotation_correction_factor_x, opticflow_t::derotation_correction_factor_y, opticflow_result_t::div_size, linear_flow_fit_info::divergence, opticflow_result_t::divergence, flow_t::error, image_t::eulers, EXHAUSTIVE_FAST, opticflow_t::fast9_adaptive, fast9_detect(), FAST9_HIGH_THRESHOLD, FAST9_LOW_THRESHOLD, opticflow_t::fast9_min_distance, opticflow_t::fast9_padding, opticflow_t::fast9_ret_corners, opticflow_t::fast9_rsize, opticflow_t::fast9_threshold, opticflow_t::feature_management, opticflow_result_t::flow_der_x, opticflow_result_t::flow_der_y, opticflow_result_t::flow_x, flow_t::flow_x, opticflow_result_t::flow_y, flow_t::flow_y, opticflow_result_t::fps, get_size_divergence(), opticflow_t::got_first_img, image_t::h, image_copy(), image_create(), IMAGE_GRAYSCALE, image_show_flow_color(), image_show_points(), image_switch(), image_to_grayscale(), opticflow_t::img_gray, InitMedianFilterVect3Float, opticflow_t::just_switched_method, LARGE_FLOW_ERROR, LINEAR_FIT, manage_flow_features(), opticflow_t::max_iterations, opticflow_t::max_track_corners, MEDIAN_DEFAULT_SIZE, opticflow_t::median_filter, n_agents, n_samples, n_time_steps, opticflow_result_t::noise_measurement, OPTICFLOW_MAX_TRACK_CORNERS, opticFlowLK(), FloatEulers::phi, flow_t::pos, predict_flow_vectors(), opticflow_t::prev_img_gray, FloatEulers::psi, opticflow_t::pyramid_level, opticflow_t::show_flow, SIZE_DIV, opticflow_t::subpixel_factor, linear_flow_fit_info::surface_roughness, opticflow_result_t::surface_roughness, FloatEulers::theta, opticflow_t::threshold_vec, timeval_diff(), opticflow_t::track_back, opticflow_result_t::tracked_cnt, image_t::ts, UpdateMedianFilterVect3Float, VECT3_ASSIGN, opticflow_result_t::vel_body, opticflow_result_t::vel_cam, vel_filt, image_t::w, opticflow_t::window_size, FloatVect3::x, point_t::x, point_t::x_sub, FloatVect3::y, point_t::y, point_t::y_sub, and FloatVect3::z.

Referenced by opticflow_calc_frame().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int cmp_array ( const void *  a,
const void *  b 
)
static

Compare the rows of an integer (uint16_t) 2D array based on the first column.

Used for sorting.

Parameters
[in]*aThe first row (should be *uint16_t)
[in]*bThe second flow vector (should be *uint16_t)
Returns
Negative if a[0] < b[0],0 if a[0] == b[0] and positive if a[0] > b[0]

Definition at line 1051 of file opticflow_calculator.c.

Referenced by manage_flow_features().

+ Here is the caller graph for this function:

static int cmp_flow ( const void *  a,
const void *  b 
)
static

Compare two flow vectors based on flow distance Used for sorting.

Parameters
[in]*aThe first flow vector (should be vect flow_t)
[in]*bThe second flow vector (should be vect flow_t)
Returns
Negative if b has more flow than a, 0 if the same and positive if a has more flow than b

Definition at line 1036 of file opticflow_calculator.c.

References flow_t::flow_x, and flow_t::flow_y.

Referenced by calc_fast9_lukas_kanade().

+ Here is the caller graph for this function:

bool opticflow_calc_frame ( struct opticflow_t opticflow,
struct image_t img,
struct opticflow_result_t result 
)

Run the optical flow on a new image frame.

Parameters
[in]*opticflowThe opticalflow structure that keeps track of previous images
[in]*stateThe state of the drone
[in]*imgThe image frame to calculate the optical flow from
[out]*resultThe optical flow result

Definition at line 982 of file opticflow_calculator.c.

References body_to_cam, calc_edgeflow_tot(), calc_fast9_lukas_kanade(), float_rmat_transp_vmult(), opticflow_t::just_switched_method, opticflow_t::method, opticflow_result_t::vel_body, and opticflow_result_t::vel_cam.

Referenced by opticflow_module_calc().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void opticflow_calc_init ( struct opticflow_t opticflow)

Initialize the opticflow calculator.

Parameters
[out]*opticflowThe new optical flow calculator

Definition at line 247 of file opticflow_calculator.c.

References opticflow_t::actfast_gradient_method, opticflow_t::actfast_long_step, opticflow_t::actfast_min_gradient, opticflow_t::actfast_short_step, body_to_cam, opticflow_t::corner_method, opticflow_t::derotation, opticflow_t::derotation_correction_factor_x, opticflow_t::derotation_correction_factor_y, opticflow_t::fast9_adaptive, opticflow_t::fast9_min_distance, opticflow_t::fast9_num_regions, opticflow_t::fast9_padding, opticflow_t::fast9_region_detect, opticflow_t::fast9_ret_corners, opticflow_t::fast9_rsize, opticflow_t::fast9_threshold, opticflow_t::feature_management, float_rmat_of_eulers, opticflow_t::max_iterations, opticflow_t::max_track_corners, opticflow_t::median_filter, opticflow_t::method, OPTICFLOW_ACTFAST_GRADIENT_METHOD, OPTICFLOW_ACTFAST_LONG_STEP, OPTICFLOW_ACTFAST_MIN_GRADIENT, OPTICFLOW_ACTFAST_SHORT_STEP, OPTICFLOW_BODY_TO_CAM_PHI, OPTICFLOW_BODY_TO_CAM_PSI, OPTICFLOW_BODY_TO_CAM_THETA, OPTICFLOW_CORNER_METHOD, OPTICFLOW_DEROTATION, OPTICFLOW_DEROTATION_CORRECTION_FACTOR_X, OPTICFLOW_DEROTATION_CORRECTION_FACTOR_Y, OPTICFLOW_FAST9_ADAPTIVE, OPTICFLOW_FAST9_MIN_DISTANCE, OPTICFLOW_FAST9_NUM_REGIONS, OPTICFLOW_FAST9_PADDING, OPTICFLOW_FAST9_REGION_DETECT, OPTICFLOW_FAST9_THRESHOLD, OPTICFLOW_FEATURE_MANAGEMENT, OPTICFLOW_MAX_ITERATIONS, OPTICFLOW_MAX_TRACK_CORNERS, OPTICFLOW_MEDIAN_FILTER, OPTICFLOW_METHOD, OPTICFLOW_PYRAMID_LEVEL, OPTICFLOW_RESOLUTION_FACTOR, OPTICFLOW_SEARCH_DISTANCE, OPTICFLOW_SHOW_FLOW, OPTICFLOW_SUBPIXEL_FACTOR, OPTICFLOW_THRESHOLD_VEC, OPTICFLOW_TRACK_BACK, OPTICFLOW_WINDOW_SIZE, opticflow_t::pyramid_level, opticflow_t::resolution_factor, opticflow_t::search_distance, opticflow_t::show_flow, opticflow_t::subpixel_factor, opticflow_t::threshold_vec, opticflow_t::track_back, and opticflow_t::window_size.

Referenced by opticflow_module_init().

+ Here is the caller graph for this function:

static struct flow_t * predict_flow_vectors ( struct flow_t flow_vectors,
uint16_t  n_points,
float  phi_diff,
float  theta_diff,
float  psi_diff,
struct opticflow_t opticflow 
)
static

Definition at line 627 of file opticflow_calculator.c.

References A, B, distorted_pixels_to_normalized_coords(), flow_t::error, flow_t::flow_x, flow_t::flow_y, K, LARGE_FLOW_ERROR, normalized_coords_to_distorted_pixels(), flow_t::pos, opticflow_t::subpixel_factor, point_t::x, and point_t::y.

Referenced by calc_fast9_lukas_kanade().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static uint32_t timeval_diff ( struct timeval *  starttime,
struct timeval *  finishtime 
)
static

Calculate the difference from start till finish.

Parameters
[in]*starttimeThe start time to calculate the difference from
[in]*finishtimeThe finish time to calculate the difference from
Returns
The difference in milliseconds

Definition at line 1021 of file opticflow_calculator.c.

Referenced by calc_edgeflow_tot(), and calc_fast9_lukas_kanade().

+ Here is the caller graph for this function:

Variable Documentation

struct FloatRMat body_to_cam

Definition at line 232 of file opticflow_calculator.c.

Referenced by opticflow_calc_frame(), and opticflow_calc_init().

uint16_t n_agents = 25

Definition at line 59 of file opticflow_calculator.c.

Referenced by act_fast(), and calc_fast9_lukas_kanade().

uint16_t n_time_steps = 10

Definition at line 58 of file opticflow_calculator.c.

Referenced by act_fast(), and calc_fast9_lukas_kanade().

struct MedianFilter3Float vel_filt

Definition at line 231 of file opticflow_calculator.c.

Referenced by calc_edgeflow_tot(), and calc_fast9_lukas_kanade().