algebraic lens distortion
|
00001 /* lens_distortion.c */ 00011 #ifndef _LENS_DISTORTION_A_H_ 00012 #define _LENS_DISTORTION_A_H_ 00013 00014 #include <stdio.h> 00015 #include <stdlib.h> 00016 #include <math.h> 00017 #include <malloc.h> 00018 #include <string.h> 00019 #include <time.h> 00020 #include <vector> 00021 #include "ami_pol.h" 00022 #include "point2d.h" 00023 #include "image.h" 00024 #ifdef AMI_OMP_H 00025 #include <omp.h> 00026 #endif 00027 00028 #define PI 3.1415927 00029 #define ABS(x) (((x)>0)?(x):-(x)) 00030 #define Normalize(x) ((x)<0?0:((x)>255?255:x)) 00031 #define Max(a,b) ((a>b)?a:b) 00032 #define ami_abs(x) ((x)>0?(x):(-(x))) 00033 #define ami_calloc2d(address,datatype,height,width) {int ml,mk; \ 00034 address=(datatype **) malloc(sizeof(datatype *)*(height)); \ 00035 address[0]=(datatype *)malloc(sizeof(datatype)*(width)*(height)); \ 00036 for(ml=0;ml<(height);ml++) address[ml]=&(address[0][ml*(width)]); \ 00037 for(ml=0;ml<height;ml++) for(mk=0;mk<width;mk++) address[ml][mk]=0; \ 00038 } 00039 #define ami_malloc2d(address,datatype,height,width) {int ml; \ 00040 address=(datatype **) malloc(sizeof(datatype *)*(height)); \ 00041 address[0]=(datatype *)malloc(sizeof(datatype)*(width)*(height)); \ 00042 for(ml=0;ml<(height);ml++) address[ml]=&(address[0][ml*(width)]);\ 00043 } 00044 #define ami_malloc2d_pointers(address,simple_pointer,datatype,height,width) {int ml; \ 00045 address=(datatype **) malloc(sizeof(datatype *)*(height)); \ 00046 address[0]=(datatype *) simple_pointer; \ 00047 for(ml=0;ml<(height);ml++) address[ml]=&(address[0][ml*(width)]);\ 00048 } 00049 #define ami_free2d(address) { free(address[0]); free(address); } 00050 #define ami_free2d_pointers(address) { free(address); } 00051 #define ami_malloc1d(address,datatype,size) {address=(datatype *) malloc(sizeof(datatype)*(size));} 00052 #define ami_calloc1d(address,datatype,size) {int ml; address=(datatype *) malloc(sizeof(datatype)*(size)); \ 00053 for(ml=0;ml<size;ml++) address[ml]=0;\ 00054 } 00055 00060 #define ami_max_iter 1000 00061 #define ami_tol 0.0000001 00062 00068 #define line_length 80 /* LENGTH OF LINE (TO READ A LINE FOR A FILE) */ 00069 00070 00074 #define max_itera 100 /* MAXIMUM NUMBER OF GRADIENT ITERATIONS */ 00075 #define delta 1.0e-10 /* DERIVATIVE STEP (FINITE DIFFERENCES) */ 00076 #define max_itera_lambda 10 /* MAXIMUM NUMBER OF ITERATIONS IN UNIDIMENSIONAL SEARCH */ 00077 #define tol_f 1.0e-6 /* TOLERANCE TO STOP THE GRADIENT ITERATIONS */ 00078 #define tol_norma 1.0e-16 /* NORM OF GRADIENT TO STOP THE GRADIENT ALGORITHM */ 00079 00087 #define patch_size 20 00088 #define max_itera_patch 20 /* MAXIMUM NUMBER OF SEARCH-OF-DISTORTION-CENTER ITERATIONS */ 00089 00094 int test_compatibility_lens_distortion_model(double *a,int Na,double max_radius); 00095 int ami_line2d_calculation(double line[3], double **Points2D, int N); 00096 int ami_lens_distortion_polynomial_update_distance_2v(double *x, double *y, int Np, 00097 double *a, int Na, double x0, double y0, int k1, int k2, double **pol, double alpha); 00098 double ami_lens_distortion_estimation_2v(double **x, double **y, int Nl, int *Np, 00099 double x0, double y0, double *a, int Na, int k1, int k2, double alpha, 00100 double max_radius); 00101 int ami_lens_distortion_model_update_2v(double *a, int Na, int k1, int k2, double **pol, 00102 double max_radius); 00103 int ami_lens_distortion_polynomial_update_2v(double *x, double *y, int Np, double *a, 00104 int Na, double x0, double y0, int k1, int k2, double **pol); 00105 void ami_2v_polynom_derivatives(double **p, int N, double **p_x, double **p_y); 00106 void ami_polynom_determinant(double p[6][6][19], int Np, int Nd, double *q); 00107 double ami_2v_polynom_evaluation(double **p1, int N1, double x, double y); 00108 void ami_2v_polynom_to_1v_polynom(double **p1, int N1, double *p3, double z, int flat); 00109 double *ami_1v_polynom_multiplication(double *p1, int N1, double *p2, int N2, double *p3); 00110 void ami_2v_polynom_multiplication(double **p1, int N1, double **p2, int N2, double **p3); 00111 int ami_RootCubicPolynomial(double *a, int N, double *x); 00112 double ami_polynomial_evaluation(double *a, int Na, double x); 00113 int ami_lens_distortion_polynomial_update(double *x, double *y, int Np, double *a, 00114 int Na, double x0, double y0, int k, double *pol); 00115 int ami_lens_distortion_model_update(double *a, int Na, int k, double *pol); 00116 double ami_LensDistortionEnergyError(double *x, double *y, int Np, double x0, 00117 double y0, double *a, int Na); 00118 double ami_LensDistortionEnergyError_Vmin(double *x, double *y, int Np, 00119 double x0, double y0, double *a, int Na); 00120 double ami_lens_distortion_estimation(double **x, double **y, int Nl, int *Np, 00121 double x0, double y0, double *a, int Na, int k, double alpha); 00122 void ami_lens_distortion_zoom_normalization(double **x, double **y, int Nl, 00123 int *Np,double *solution, int Na); 00124 int calculate_points(double *amin, double **points_2D_modified, int N, int Na, 00125 double x0, double y0); 00126 double distance_function(double *solution, double **x, double **y, int Nl, 00127 int *Np, int Na); 00128 double find_lambda(double lambda1, double lambda2, double lambda3, double f_1, 00129 double f_2, double f_3, double *amin_copy, double *amin, 00130 double **x, double **y, int Nl, int *Np,int Na,double *grad_f, int *change_k); 00131 double minimize_cuadratic_polynom(double lambda1, double lambda2, double lambda3, 00132 double f_1, double f_2, double f_3, double *amin_copy, double *amin, 00133 double **x, double **y, int Nl, int *Np,int Na,double *grad_f, int *change_k); 00134 double cuadratic_fitting(double *amin_copy, double *amin, double **x, double **y, 00135 int Nl,int *Np,int Na, double lambda1, double lambda2, double lambda3, 00136 double f_1, double f_2, double f_3, double *grad_f, int *change_k); 00137 double minimize_lambda(double *amin, double **x, double **y, int Nl, int *Np, 00138 int Na,double *grad_f, double f, int *change_k); 00139 double gradient_method(double *solution, double **x, double **y, int Nl, int *Np, 00140 int Na,int *change_k, int zoom, int optimize_center); 00141 double calculate_factor_n(double **x, double**y,int Nl, int *Np,double x0,double y0); 00142 int optimize(double *solution, double **x, double **y, double **xx, double **yy, 00143 int Nl, int *Np, int Na,double factor_n, int zoom, FILE *fp1, 00144 int optimize_center); 00145 int algebraic_method_pre_gradient(int Nl, int *Np, double *a, double **x, double **y, 00146 double **xx, double **yy, double factor_n, int zoom, FILE *fp1, 00147 int optimize_center,double max_radius); 00148 int trivial_solution(int Nl, int *Np,double *a,double **xx, double **yy, double factor_n, 00149 FILE *fp1, double *trivial, int optimize_center); 00150 int read_line_primitives(char filename[300], int *Nl, int **Np, double ***x, double ***y); 00151 int search_for_best_center(int N, int *Np, double *a, double **xx, double **yy, 00152 int width, int height, double max_radius); 00153 void ami_lens_distortion_model_evaluation(double *a,int Na, double xc,double yc, 00154 double x_input,double y_input,double *x_output,double *y_output); 00155 double ami_inverse_lens_distortion_newton_raphson(double x,double y, double x0, 00156 double y0, double *xt,double *yt, double *a, int Na); 00157 int ami_inverse_lens_distortion_fast(double x,double y,double x0,double y0, double *xt, 00158 double *yt, double *a, int Na,double dl1r); 00159 int build_l1r_vector(std::vector<double> &l1r,ami::point2d<double> &dc, double max_distance_corner, 00160 int Na, double *a); 00161 ami::image<unsigned char> undistort_image_inverse_fast(ami::image<unsigned char> input_image, 00162 int Na, double *a,ami::point2d<double> dc,const double &image_amplification_factor); 00163 00164 00165 #endif