Search in sources :

Example 1 with AssociatedPair

use of boofcv.struct.geo.AssociatedPair in project BoofCV by lessthanoptimal.

the class BenchmarkStabilityFundamental method evaluateMinimal.

public void evaluateMinimal(GeoModelEstimatorN<DMatrixRMaj, AssociatedPair> estimatorN) {
    DistanceEpipolarConstraint distance = new DistanceEpipolarConstraint();
    Estimate1ofEpipolar estimator = new EstimateNto1ofEpipolar(estimatorN, distance, 1);
    scores = new ArrayList<>();
    int failed = 0;
    int numSamples = estimator.getMinimumPoints();
    Random rand = new Random(234);
    DMatrixRMaj F = new DMatrixRMaj(3, 3);
    for (int i = 0; i < 50; i++) {
        List<AssociatedPair> pairs = new ArrayList<>();
        // create a unique set of pairs
        while (pairs.size() < numSamples) {
            AssociatedPair p = observations.get(rand.nextInt(observations.size()));
            if (!pairs.contains(p)) {
                pairs.add(p);
            }
        }
        if (!estimator.process(pairs, F)) {
            failed++;
            continue;
        }
        // normalize the scale of F
        CommonOps_DDRM.scale(1.0 / CommonOps_DDRM.elementMaxAbs(F), F);
        double totalScore = 0;
        // score against all observations
        for (AssociatedPair p : observations) {
            double score = Math.abs(GeometryMath_F64.innerProd(p.p2, F, p.p1));
            if (Double.isNaN(score))
                System.out.println("Score is NaN");
            scores.add(score);
            totalScore += score;
        }
    // System.out.println("  score["+i+"] = "+totalScore);
    }
    Collections.sort(scores);
    System.out.printf(" Failures %3d  Score:  50%% = %6.3e  95%% = %6.3e\n", failed, scores.get(scores.size() / 2), scores.get((int) (scores.size() * 0.95)));
}
Also used : AssociatedPair(boofcv.struct.geo.AssociatedPair) DistanceEpipolarConstraint(boofcv.alg.geo.f.DistanceEpipolarConstraint) Random(java.util.Random) Estimate1ofEpipolar(boofcv.abst.geo.Estimate1ofEpipolar) DMatrixRMaj(org.ejml.data.DMatrixRMaj) ArrayList(java.util.ArrayList) EstimateNto1ofEpipolar(boofcv.abst.geo.f.EstimateNto1ofEpipolar) DistanceEpipolarConstraint(boofcv.alg.geo.f.DistanceEpipolarConstraint)

Example 2 with AssociatedPair

use of boofcv.struct.geo.AssociatedPair in project BoofCV by lessthanoptimal.

the class BenchmarkStabilityFundamental method evaluateAll.

public void evaluateAll(GeoModelEstimator1<DMatrixRMaj, AssociatedPair> estimator) {
    scores = new ArrayList<>();
    int failed = 0;
    DMatrixRMaj F = new DMatrixRMaj(3, 3);
    for (int i = 0; i < 50; i++) {
        if (!estimator.process(observations, F)) {
            failed++;
            continue;
        }
        // normalize the scale of F
        CommonOps_DDRM.scale(1.0 / CommonOps_DDRM.elementMaxAbs(F), F);
        // score against all observations
        for (AssociatedPair p : observations) {
            double score = Math.abs(GeometryMath_F64.innerProd(p.p2, F, p.p1));
            if (Double.isNaN(score))
                System.out.println("Score is NaN");
            scores.add(score);
        }
    }
    Collections.sort(scores);
    System.out.printf(" Failures %3d  Score:  50%% = %6.3e  95%% = %6.3e\n", failed, scores.get(scores.size() / 2), scores.get((int) (scores.size() * 0.95)));
}
Also used : AssociatedPair(boofcv.struct.geo.AssociatedPair) DMatrixRMaj(org.ejml.data.DMatrixRMaj) DistanceEpipolarConstraint(boofcv.alg.geo.f.DistanceEpipolarConstraint)

Example 3 with AssociatedPair

use of boofcv.struct.geo.AssociatedPair in project BoofCV by lessthanoptimal.

the class ExampleFundamentalMatrix method robustFundamental.

/**
 * Given a set of noisy observations, compute the Fundamental matrix while removing
 * the noise.
 *
 * @param matches List of associated features between the two images
 * @param inliers List of feature pairs that were determined to not be noise.
 * @return The found fundamental matrix.
 */
public static DMatrixRMaj robustFundamental(List<AssociatedPair> matches, List<AssociatedPair> inliers) {
    // used to create and copy new instances of the fit model
    ModelManager<DMatrixRMaj> managerF = new ModelManagerEpipolarMatrix();
    // Select which linear algorithm is to be used.  Try playing with the number of remove ambiguity points
    Estimate1ofEpipolar estimateF = FactoryMultiView.computeFundamental_1(EnumFundamental.LINEAR_7, 2);
    // Wrapper so that this estimator can be used by the robust estimator
    GenerateEpipolarMatrix generateF = new GenerateEpipolarMatrix(estimateF);
    // How the error is measured
    DistanceFromModelResidual<DMatrixRMaj, AssociatedPair> errorMetric = new DistanceFromModelResidual<>(new FundamentalResidualSampson());
    // Use RANSAC to estimate the Fundamental matrix
    ModelMatcher<DMatrixRMaj, AssociatedPair> robustF = new Ransac<>(123123, managerF, generateF, errorMetric, 6000, 0.1);
    // Estimate the fundamental matrix while removing outliers
    if (!robustF.process(matches))
        throw new IllegalArgumentException("Failed");
    // save the set of features that were used to compute the fundamental matrix
    inliers.addAll(robustF.getMatchSet());
    // Improve the estimate of the fundamental matrix using non-linear optimization
    DMatrixRMaj F = new DMatrixRMaj(3, 3);
    ModelFitter<DMatrixRMaj, AssociatedPair> refine = FactoryMultiView.refineFundamental(1e-8, 400, EpipolarError.SAMPSON);
    if (!refine.fitModel(inliers, robustF.getModelParameters(), F))
        throw new IllegalArgumentException("Failed");
    // Return the solution
    return F;
}
Also used : GenerateEpipolarMatrix(boofcv.abst.geo.fitting.GenerateEpipolarMatrix) AssociatedPair(boofcv.struct.geo.AssociatedPair) Estimate1ofEpipolar(boofcv.abst.geo.Estimate1ofEpipolar) DistanceFromModelResidual(boofcv.abst.geo.fitting.DistanceFromModelResidual) DMatrixRMaj(org.ejml.data.DMatrixRMaj) FundamentalResidualSampson(boofcv.alg.geo.f.FundamentalResidualSampson) Ransac(org.ddogleg.fitting.modelset.ransac.Ransac) ModelManagerEpipolarMatrix(boofcv.abst.geo.fitting.ModelManagerEpipolarMatrix)

Example 4 with AssociatedPair

use of boofcv.struct.geo.AssociatedPair in project BoofCV by lessthanoptimal.

the class ExampleFundamentalMatrix method main.

public static void main(String[] args) {
    String dir = UtilIO.pathExample("structure/");
    BufferedImage imageA = UtilImageIO.loadImage(dir, "undist_cyto_01.jpg");
    BufferedImage imageB = UtilImageIO.loadImage(dir, "undist_cyto_02.jpg");
    List<AssociatedPair> matches = computeMatches(imageA, imageB);
    // Where the fundamental matrix is stored
    DMatrixRMaj F;
    // List of matches that matched the model
    List<AssociatedPair> inliers = new ArrayList<>();
    // estimate and print the results using a robust and simple estimator
    // The results should be difference since there are many false associations in the simple model
    // Also note that the fundamental matrix is only defined up to a scale factor.
    F = robustFundamental(matches, inliers);
    System.out.println("Robust");
    F.print();
    F = simpleFundamental(matches);
    System.out.println("Simple");
    F.print();
    // display the inlier matches found using the robust estimator
    AssociationPanel panel = new AssociationPanel(20);
    panel.setAssociation(inliers);
    panel.setImages(imageA, imageB);
    ShowImages.showWindow(panel, "Inlier Pairs");
}
Also used : AssociatedPair(boofcv.struct.geo.AssociatedPair) DMatrixRMaj(org.ejml.data.DMatrixRMaj) ArrayList(java.util.ArrayList) AssociationPanel(boofcv.gui.feature.AssociationPanel) BufferedImage(java.awt.image.BufferedImage)

Example 5 with AssociatedPair

use of boofcv.struct.geo.AssociatedPair in project BoofCV by lessthanoptimal.

the class ExampleStereoTwoViewsOneCamera method convertToNormalizedCoordinates.

/**
 * Convert a set of associated point features from pixel coordinates into normalized image coordinates.
 */
public static List<AssociatedPair> convertToNormalizedCoordinates(List<AssociatedPair> matchedFeatures, CameraPinholeRadial intrinsic) {
    Point2Transform2_F64 p_to_n = LensDistortionOps.narrow(intrinsic).undistort_F64(true, false);
    List<AssociatedPair> calibratedFeatures = new ArrayList<>();
    for (AssociatedPair p : matchedFeatures) {
        AssociatedPair c = new AssociatedPair();
        p_to_n.compute(p.p1.x, p.p1.y, c.p1);
        p_to_n.compute(p.p2.x, p.p2.y, c.p2);
        calibratedFeatures.add(c);
    }
    return calibratedFeatures;
}
Also used : AssociatedPair(boofcv.struct.geo.AssociatedPair) Point2Transform2_F64(boofcv.struct.distort.Point2Transform2_F64) ArrayList(java.util.ArrayList)

Aggregations

AssociatedPair (boofcv.struct.geo.AssociatedPair)179 Test (org.junit.jupiter.api.Test)48 ArrayList (java.util.ArrayList)47 DMatrixRMaj (org.ejml.data.DMatrixRMaj)38 Point2D_F64 (georegression.struct.point.Point2D_F64)35 Se3_F64 (georegression.struct.se.Se3_F64)31 Point3D_F64 (georegression.struct.point.Point3D_F64)25 Test (org.junit.Test)14 ClosestPoint3D_F64 (georegression.metric.ClosestPoint3D_F64)10 DetectDescribePoint (boofcv.abst.feature.detdesc.DetectDescribePoint)8 Estimate1ofEpipolar (boofcv.abst.geo.Estimate1ofEpipolar)7 ConfigRansac (boofcv.factory.geo.ConfigRansac)7 Point2Transform2_F64 (boofcv.struct.distort.Point2Transform2_F64)6 AssociatedIndex (boofcv.struct.feature.AssociatedIndex)6 ScaleTranslate2D (boofcv.struct.geo.ScaleTranslate2D)6 ScaleTranslate2D (boofcv.struct.sfm.ScaleTranslate2D)6 Homography2D_F64 (georegression.struct.homography.Homography2D_F64)5 BufferedImage (java.awt.image.BufferedImage)5 ConfigFastHessian (boofcv.abst.feature.detect.interest.ConfigFastHessian)4 DistanceEpipolarConstraint (boofcv.alg.geo.f.DistanceEpipolarConstraint)4