Search in sources :

Example 1 with NumericalJacobianForward_DDRM

use of org.ddogleg.optimization.derivative.NumericalJacobianForward_DDRM in project BoofCV by lessthanoptimal.

the class RefineDualQuadraticAlgebraicError method refine.

/**
 * Refines the initial parameters to minimize algebraic error.
 *
 * @return true if there are no catastrophic errors
 */
public boolean refine() {
    BoofMiscOps.checkTrue(viewToCamera.get(0) != -1, "You must specify view to camera");
    // Copy the cameras with prior information into the work cameras
    for (int cameraIdx = 0; cameraIdx < priorCameras.size; cameraIdx++) {
        cameras.get(cameraIdx).setTo(priorCameras.get(cameraIdx));
    }
    parameters.resize(function.getNumOfInputsN());
    encodeParameters(priorPlaneAtInfinity, priorCameras, parameters.data);
    // Configure the minimization
    minimizer.setFunction(function, new NumericalJacobianForward_DDRM(new ResidualFunction()));
    minimizer.initialize(parameters.data, converge.ftol, converge.gtol);
    double errorBefore = minimizer.getFunctionValue();
    // Iterate until a final condition has been met
    int iterations;
    for (iterations = 0; iterations < converge.maxIterations; iterations++) {
        if (minimizer.iterate())
            break;
    }
    if (verbose != null)
        verbose.printf("before=%.2e after=%.2e iterations=%d converged=%s\n", errorBefore, minimizer.getFunctionValue(), iterations, minimizer.isConverged());
    // Extract the output
    decodeParameters(minimizer.getParameters(), cameras, planeAtInfinity);
    // Crude sanity check. Things went poorly if there is a non-positive focal length
    for (int i = 0; i < cameras.size; i++) {
        CameraState c = cameras.get(i);
        if (c.fx <= 0 || c.aspectRatio <= 0)
            return false;
    }
    return true;
}
Also used : NumericalJacobianForward_DDRM(org.ddogleg.optimization.derivative.NumericalJacobianForward_DDRM) VerbosePrint(org.ddogleg.struct.VerbosePrint)

Example 2 with NumericalJacobianForward_DDRM

use of org.ddogleg.optimization.derivative.NumericalJacobianForward_DDRM in project BoofCV by lessthanoptimal.

the class RefineTwoViewPinholeRotation method refine.

/**
 * Refines the provided parameters. Inputs are only modified if it returns true. If two views are specified
 * but there's a single view assumption then only the first view is used.
 *
 * @param associatedPixels (Input) Matches image features between the two views. Pixels.
 * @param rotation (Input/Output) Initial estimate of rotation. Results are written to it.
 * @param intrinsic1 (Input/Output) Initial estimate of intrinsic1. Results are written to it.
 * @param intrinsic2 (Input/Output) Initial estimate of intrinsic2. Results are written to it.
 * @return true if it thinks it has a valid solution. False if it knows it failed.
 */
public boolean refine(List<AssociatedPair> associatedPixels, DMatrixRMaj rotation, CameraPinhole intrinsic1, CameraPinhole intrinsic2) {
    this.associatedPixels = associatedPixels;
    this.inputIntrinsic1 = intrinsic1;
    this.inputIntrinsic2 = intrinsic2;
    // Copy inputs over
    ConvertRotation3D_F64.matrixToRodrigues(rotation, function.state.rotation);
    function.state.intrinsic1.setTo(intrinsic1);
    function.state.intrinsic2.setTo(intrinsic2);
    initialParameters.resize(function.getNumOfInputsN());
    function.state.encode(initialParameters.data);
    // Configure the minimization
    minimizer.setFunction(function, new NumericalJacobianForward_DDRM(new ResidualFunction()));
    minimizer.initialize(initialParameters.data, converge.ftol, converge.gtol);
    errorBefore = minimizer.getFunctionValue();
    // Iterate until a final condition has been met
    int iterations;
    for (iterations = 0; iterations < converge.maxIterations; iterations++) {
        if (minimizer.iterate())
            break;
    }
    errorAfter = minimizer.getFunctionValue();
    // Get the refined values
    function.state.decode(minimizer.getParameters());
    if (verbose != null) {
        double theta = UtilAngle.degree(function.state.rotation.theta);
        CameraPinhole found1 = function.state.intrinsic1;
        CameraPinhole found2 = function.state.intrinsic2;
        verbose.printf("before=%.2e after=%.2e iterations=%d converged=%s, " + "view1={fx=%.1f fy=%.1f, cx=%.1f cy=%.1f), " + "view2=(fx=%.1f fy=%.1f, cx=%.1f cy=%.1f) theta=%.2f\n", errorBefore, errorAfter, iterations, minimizer.isConverged(), found1.fx, found1.fy, found1.cx, found1.cy, found2.fx, found2.fy, found2.cx, found2.cy, theta);
    }
    // Copy results
    intrinsic1.setTo(function.state.intrinsic1);
    intrinsic2.setTo(function.state.intrinsic2);
    ConvertRotation3D_F64.rodriguesToMatrix(function.state.rotation, rotation);
    return true;
}
Also used : NumericalJacobianForward_DDRM(org.ddogleg.optimization.derivative.NumericalJacobianForward_DDRM) CameraPinhole(boofcv.struct.calib.CameraPinhole) VerbosePrint(org.ddogleg.struct.VerbosePrint)

Aggregations

NumericalJacobianForward_DDRM (org.ddogleg.optimization.derivative.NumericalJacobianForward_DDRM)2 VerbosePrint (org.ddogleg.struct.VerbosePrint)2 CameraPinhole (boofcv.struct.calib.CameraPinhole)1