Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic convergence #163

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions liboptv/include/orientation.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ double point_position(vec2d targets[], int num_cams, mm_np *multimed_pars,
double weighted_dumbbell_precision(vec2d** targets, int num_targs, int num_cams,
mm_np *multimed_pars, Calibration* cals[], int db_length, double db_weight);

double* orient(Calibration* cal, control_par *cpar, int nfix, vec3d fix[], target pix[],
orient_par *flags, double sigmabeta[20]);
double* orient (Calibration* cal_in, control_par *cpar, int nfix, vec3d fix[],
target pix[], orient_par *flags, double sigmabeta[20], int num_iter,
double convergence);
int raw_orient(Calibration* cal, control_par *cpar, int nfix, vec3d fix[], target pix[]);

int read_man_ori_fix(vec3d fix4[4], char* calblock_filename, char* man_ori_filename,
Expand Down
15 changes: 10 additions & 5 deletions liboptv/src/orientation.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include <string.h>
#include <math.h>

#define NUM_ITER 80
// #define num_iter 80
#define POS_INF 1E20
#define CONVERGENCE 0.00001
// #define convergence 0.00001

/* skew_midpoint() finds the middle of the minimal distance segment between
skew rays. Undefined for parallel rays.
Expand Down Expand Up @@ -239,6 +239,9 @@ void num_deriv_exterior(Calibration* cal, control_par *cpar, double dpos, double
the calibration structure, cal.
double sigmabeta[] - array of deviations for each of the interior and
exterior parameters and glass interface vector (19 in total).
int num_iter - (typical 80 - 200) number of iterations for convergence of the orientation routine
double convergence - (0.000001-0.01) a threshold that defines if the change is
sufficiently small and the result has converged.

Returns:
On success, a pointer to an array of residuals. For each observation point
Expand All @@ -247,9 +250,11 @@ void num_deriv_exterior(Calibration* cal, control_par *cpar, double dpos, double
between initial guess and final solution for internal and distortion
parameters, which are also part of the G-M model and described in it.
On failure returns NULL.

*/
double* orient (Calibration* cal_in, control_par *cpar, int nfix, vec3d fix[],
target pix[], orient_par *flags, double sigmabeta[20])
target pix[], orient_par *flags, double sigmabeta[20], int num_iter,
double convergence)
{
int i,j,n, itnum, stopflag, n_obs=0, maxsize;

Expand Down Expand Up @@ -341,7 +346,7 @@ double* orient (Calibration* cal_in, control_par *cpar, int nfix, vec3d fix[],

itnum = 0;
stopflag = 0;
while ((stopflag == 0) && (itnum < NUM_ITER)) {
while ((stopflag == 0) && (itnum < num_iter)) {
itnum++;

for (i = 0, n = 0; i < nfix; i++) {
Expand Down Expand Up @@ -516,7 +521,7 @@ double* orient (Calibration* cal_in, control_par *cpar, int nfix, vec3d fix[],

stopflag = 1;
for (i = 0; i < numbers; i++) {
if (fabs (beta[i]) > CONVERGENCE) stopflag = 0;
if (fabs (beta[i]) > convergence) stopflag = 0;
}

if ( ! flags->ccflag) beta[6] = 0.0;
Expand Down
6 changes: 4 additions & 2 deletions liboptv/tests/check_orientation.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ START_TEST(test_orient)
int nfix, i, k=0, pnr, ix, iy, iz, pt_id;
int eps, correct_eps = 25;
double xp, yp;
int num_iter = 100;
double convergence = 0.000001;
FILE *fpp;

pt_id = 0;
Expand Down Expand Up @@ -131,7 +133,7 @@ START_TEST(test_orient)
cal->ext_par.phi += 0.5;
cal->ext_par.kappa += 0.5;

fail_if((resi = orient (cal, cpar, 64, fix, pix, opar, sigmabeta)) == NULL);
fail_if((resi = orient (cal, cpar, 64, fix, pix, opar, sigmabeta, num_iter, convergence)) == NULL);
free(resi);
fail_if((org_cal = read_calibration(ori_file, add_file, NULL)) == NULL);
fail_unless (fabs(cal->ext_par.x0 - org_cal->ext_par.x0) +
Expand All @@ -155,7 +157,7 @@ START_TEST(test_orient)
opar->ccflag = 1;
opar->xhflag = 1;

fail_if((resi = orient (cal, cpar, 64, fix, pix, opar, sigmabeta)) == NULL);
fail_if((resi = orient (cal, cpar, 64, fix, pix, opar, sigmabeta, num_iter, convergence)) == NULL);
free(resi);
fail_unless (fabs(fabs(cal->ext_par.x0 - org_cal->ext_par.x0) +
fabs(cal->ext_par.y0 - org_cal->ext_par.y0) +
Expand Down
3 changes: 2 additions & 1 deletion py_bind/optv/orientation.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ cdef extern from "optv/orientation.h":
int raw_orient(calibration* cal, control_par *cpar, int nfix, vec3d fix[],
target pix[]);
double* orient (calibration* cal_in, control_par *cpar, int nfix,
vec3d fix[], target pix[], orient_par *flags, double sigmabeta[20])
vec3d fix[], target pix[], orient_par *flags, double sigmabeta[20], int num_iter,
double correspondence)
orient_par* read_orient_par(char *filename)
double weighted_dumbbell_precision(vec2d** targets, int num_targs,
int num_cams, mm_np *multimed_pars, calibration* cals[],
Expand Down
34 changes: 29 additions & 5 deletions py_bind/optv/orientation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def match_detection_to_ref(Calibration cal,
TargetArray holding the sorted targets.
"""

if len(img_pts) < len(ref_pts):
raise ValueError('Must have at least as many targets as ref. points.')
# if len(img_pts) < len(ref_pts):
# raise ValueError('Must have at least as many targets as ref. points.')

cdef:
vec3d *ref_coord
Expand Down Expand Up @@ -151,7 +151,8 @@ def external_calibration(Calibration cal,

def full_calibration(Calibration cal,
np.ndarray[ndim=2, dtype=pos_t] ref_pts, TargetArray img_pts,
ControlParams cparam, list flags=[]):
ControlParams cparam, list flags=[], int num_iter = 80,
double convergence = 0.00001):
"""
Performs a full calibration, affecting all calibration structs.

Expand Down Expand Up @@ -216,14 +217,37 @@ def full_calibration(Calibration cal,

err_est = np.empty((NPAR + 1) * sizeof(double))
residuals = orient(cal._calibration, cparam._control_par, len(ref_pts),
ref_coord, img_pts._tarr, orip, <double *>err_est.data)
ref_coord, img_pts._tarr, orip, <double *>err_est.data, num_iter,
convergence)

free(orip)

'''
if residuals == NULL:
free(residuals)
raise ValueError("Orientation iteration failed, need better setup.")
'''
while residuals == NULL and num_iter < 500:
num_iter += 100
print("number of iterations increased to %f\n" % num_iter)
residuals = orient(cal._calibration, cparam._control_par, len(ref_pts),
ref_coord, img_pts._tarr, orip, <double *>err_est.data, num_iter,
convergence)
# free(residuals)

while residuals == NULL and convergence < .5:
convergence *= 10
print("convergence threshold increased tenfold to %f\n" % convergence)
residuals = orient(cal._calibration, cparam._control_par, len(ref_pts),
ref_coord, img_pts._tarr, orip, <double *>err_est.data, num_iter,
convergence)
# free(residuals)

if residuals == NULL:
free(residuals)

free(orip)


ret = np.empty((len(img_pts), 2))
used = np.empty(len(img_pts), dtype=np.int_)

Expand Down