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)));
}
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)));
}
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;
}
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");
}
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;
}
Aggregations