Example Fundamental Matrix

From BoofCV
Revision as of 09:38, 18 September 2012 by Peter (talk | contribs) (→‎Example Code)
Jump to navigationJump to search

Computing the Fundamental Matrix

The Fundamental matrix is a 3 by 3 matrix which describes the geometric (epipolar) relationship between two images. In the example below, features are automatically found and the fundamental matrix computed using two different techniques. The robust technique uses RANSAC to remove incorrect image pairs (see image above) followed by non-linear optimization. The simple technique assumes that all the image pairs are correct (not true in this scenario) and apply a fast to compute linear algorithm.

Example File: ExampleFundamentalMatrix.java

Concepts:

  • Epipolar constraint
  • Fundamental matrix
  • Stereo Vision

Relevant Applets:

Related Examples:

Example Code

/**
 * A Fundamental matrix describes the epipolar relationship between two images.  If two points, one from
 * each image, match, then the inner product around the Fundamental matrix will be zero.  If a fundamental
 * matrix is known, then information about the scene and its structure can be extracted.
 *
 * Below are two examples of how a Fundamental matrix can be computed using different.
 * The robust technique attempts to find the best fit Fundamental matrix to the data while removing noisy
 * matches, The simple version just assumes that all the matches are correct.  Similar techniques can be used
 * to fit various other types of motion or structural models to observations.
 *
 * The input image and associated features are displayed in a window.  In another window only features included
 * in the inlier set of the robust algorithm are shown.  Note how there are clearly incorrect inliers even after
 * the epipolar constraint has been applied?
 *
 * @author Peter Abeles
 */
public class ExampleFundamentalMatrix {

	/**
	 * 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 DenseMatrix64F robustFundamental( List<AssociatedPair> matches ,
													List<AssociatedPair> inliers ) {

		// Select which linear algorithm is to be used.  Try playing with the number of remove ambiguity points
		EpipolarMatrixEstimator estimateF = FactoryEpipolar.computeFundamentalOne(7, true, 20);
		// Wrapper so that this estimator can be used by the robust estimator
		GenerateEpipolarMatrix generateF = new GenerateEpipolarMatrix(estimateF);

		// How the error is measured
		DistanceFromModelResidual<DenseMatrix64F,AssociatedPair> errorMetric =
				new DistanceFromModelResidual<DenseMatrix64F,AssociatedPair>(new FundamentalResidualSampson());

		// Use RANSAC to estimate the Fundamental matrix
		ModelMatcher<DenseMatrix64F,AssociatedPair> robustF =
				new SimpleInlierRansac<DenseMatrix64F, AssociatedPair>(123123,generateF,errorMetric,
						3000,generateF.getMinimumPoints(),20,-1,0.2);

		// 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
		RefineEpipolarMatrix refine = FactoryEpipolar.refineFundamental(1e-8, 400, EpipolarError.SAMPSON);
		if( !refine.process(robustF.getModel(), inliers) )
			throw new IllegalArgumentException("Failed");

		// Return the solution
		return refine.getRefinement();
	}

	/**
	 * If the set of associated features are known to be correct, then the fundamental matrix can
	 * be computed directly with a lot less code.  The down side is that this technique is very
	 * sensitive to noise.
	 */
	public static DenseMatrix64F simpleFundamental( List<AssociatedPair> matches ) {
		// Use the 8-point algorithm since it will work with an arbitrary number of points
		EpipolarMatrixEstimator estimateF = FactoryEpipolar.computeFundamentalOne(8, true, 0);

		if( !estimateF.process(matches) )
			throw new IllegalArgumentException("Failed");

		// while not done here, this initial linear estimate can be refined using non-linear optimization
		// as was done above.

		// Return the solution
		return estimateF.getEpipolarMatrix();
	}

	/**
	 * Use the associate point feature example to create a list of {@link AssociatedPair} for use in computing the
	 * fundamental matrix.
	 */
	public static List<AssociatedPair> computeMatches( BufferedImage left , BufferedImage right ) {
		InterestPointDetector<ImageFloat32> detector = FactoryInterestPoint.fastHessian(1, 2, 200, 1, 9, 4, 4);
		DescribeRegionPoint<ImageFloat32,SurfFeature> describe =
				FactoryDescribeRegionPoint.surf(true, ImageFloat32.class);
		ScoreAssociation<SurfFeature> scorer = FactoryAssociation.scoreEuclidean(SurfFeature.class,true);
		GeneralAssociation<SurfFeature> associate =
				FactoryAssociation.greedy(scorer, 2, -1, true);

		ExampleAssociatePoints<ImageFloat32,SurfFeature> findMatches =
				new ExampleAssociatePoints<ImageFloat32,SurfFeature>
						(detector, describe, associate, ImageFloat32.class);

		findMatches.associate(left,right);

		List<AssociatedPair> matches = new ArrayList<AssociatedPair>();
		FastQueue<AssociatedIndex> matchIndexes = associate.getMatches();

		for( int i = 0; i < matchIndexes.size; i++ ) {
			AssociatedIndex a = matchIndexes.get(i);
			AssociatedPair p = new AssociatedPair(findMatches.pointsA.get(a.src) , findMatches.pointsB.get(a.dst));
			matches.add( p);
		}

		return matches;
	}

	public static void main( String args[] ) {

		String dir = "../data/evaluation/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
		DenseMatrix64F F;
		// List of matches that matched the model
		List<AssociatedPair> inliers = new ArrayList<AssociatedPair>();

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