Difference between revisions of "Example Fundamental Matrix"
From BoofCV
Jump to navigationJump to searchm |
|||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<center> | <center> | ||
<gallery heights=150 widths=407 > | <gallery heights=150 widths=407 > | ||
Line 10: | Line 8: | ||
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. | 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: [https://github.com/lessthanoptimal/BoofCV/blob/v0. | Example File: [https://github.com/lessthanoptimal/BoofCV/blob/v0.40/examples/src/main/java/boofcv/examples/sfm/ExampleComputeFundamentalMatrix.java ExampleComputeFundamentalMatrix.java] | ||
Concepts: | Concepts: | ||
Line 16: | Line 14: | ||
* Fundamental matrix | * Fundamental matrix | ||
* Stereo Vision | * Stereo Vision | ||
Related Examples: | Related Examples: | ||
* [[Example_Remove_Lens_Distortion| Removing Lens Distortion]] | * [[Example_Remove_Lens_Distortion| Removing Lens Distortion]] | ||
= Example Code = | = Example Code = | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
/** | /** | ||
* A Fundamental matrix describes the epipolar relationship between two images. | * 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. | * 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. | * 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. | * 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 | * 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. | * 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. | * to fit various other types of motion or structural models to observations. | ||
* | * | ||
* The input image and associated features are displayed in a window. | * The input image and associated features are displayed in a window. In another window, inlier features | ||
* | * from robust model fitting are shown. | ||
* | * | ||
* @author Peter Abeles | * @author Peter Abeles | ||
*/ | */ | ||
public class | public class ExampleComputeFundamentalMatrix { | ||
/** | /** | ||
* Given a set of noisy observations, compute the Fundamental matrix while removing | * 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 matches List of associated features between the two images | ||
Line 51: | Line 44: | ||
* @return The found fundamental matrix. | * @return The found fundamental matrix. | ||
*/ | */ | ||
public static | public static DMatrixRMaj robustFundamental( List<AssociatedPair> matches, | ||
List<AssociatedPair> inliers, double inlierThreshold ) { | |||
var configRansac = new ConfigRansac(); | |||
configRansac.inlierThreshold = inlierThreshold; | |||
configRansac.iterations = 1000; | |||
ConfigFundamental configFundamental = new ConfigFundamental(); | |||
configFundamental.which = EnumFundamental.LINEAR_7; | |||
configFundamental.numResolve = 2; | |||
configFundamental.errorModel = ConfigFundamental.ErrorModel.GEOMETRIC; | |||
// geometric error is the most accurate error metric, but also the slowest to compute. See how the | |||
// results change if you switch to sampson and how much faster it is. You also should adjust | |||
// the inlier threshold. | |||
ModelMatcher<DMatrixRMaj, AssociatedPair> ransac = | |||
FactoryMultiViewRobust.fundamentalRansac(configFundamental, configRansac); | |||
ModelMatcher< | |||
// Estimate the fundamental matrix while removing outliers | // Estimate the fundamental matrix while removing outliers | ||
if( ! | if (!ransac.process(matches)) | ||
throw new IllegalArgumentException("Failed"); | throw new IllegalArgumentException("Failed"); | ||
// save the set of features that were used to compute the fundamental matrix | // save the set of features that were used to compute the fundamental matrix | ||
inliers.addAll( | inliers.addAll(ransac.getMatchSet()); | ||
// Improve the estimate of the fundamental matrix using non-linear optimization | // Improve the estimate of the fundamental matrix using non-linear optimization | ||
var F = new DMatrixRMaj(3, 3); | |||
ModelFitter<DMatrixRMaj, AssociatedPair> refine = | |||
FactoryMultiView. | FactoryMultiView.fundamentalRefine(1e-8, 400, EpipolarError.SAMPSON); | ||
if( !refine. | if (!refine.fitModel(inliers, ransac.getModelParameters(), F)) | ||
throw new IllegalArgumentException("Failed"); | throw new IllegalArgumentException("Failed"); | ||
Line 87: | Line 80: | ||
/** | /** | ||
* If the set of associated features are known to be correct, then the fundamental matrix can | * If the set of associated features are known to be correct, then the fundamental matrix can | ||
* be computed directly with a lot less code. | * be computed directly with a lot less code. The down side is that this technique is very | ||
* sensitive to noise. | * sensitive to noise. | ||
*/ | */ | ||
public static | public static DMatrixRMaj simpleFundamental( List<AssociatedPair> matches ) { | ||
// Use the 8-point algorithm since it will work with an arbitrary number of points | // Use the 8-point algorithm since it will work with an arbitrary number of points | ||
Estimate1ofEpipolar estimateF = FactoryMultiView. | Estimate1ofEpipolar estimateF = FactoryMultiView.fundamental_1(EnumFundamental.LINEAR_8, 0); | ||
var F = new DMatrixRMaj(3, 3); | |||
if( !estimateF.process(matches,F) ) | if (!estimateF.process(matches, F)) | ||
throw new IllegalArgumentException("Failed"); | throw new IllegalArgumentException("Failed"); | ||
Line 107: | Line 100: | ||
* fundamental matrix. | * fundamental matrix. | ||
*/ | */ | ||
public static List<AssociatedPair> computeMatches( BufferedImage left , BufferedImage right ) { | public static List<AssociatedPair> computeMatches( BufferedImage left, BufferedImage right ) { | ||
DetectDescribePoint detDesc = FactoryDetectDescribe. | DetectDescribePoint<GrayF32, TupleDesc_F64> detDesc = FactoryDetectDescribe.surfStable( | ||
// DetectDescribePoint detDesc = FactoryDetectDescribe.sift( | new ConfigFastHessian(0, 2, 400, 1, 9, 4, 4), null, null, GrayF32.class); | ||
// DetectDescribePoint detDesc = FactoryDetectDescribe.sift(null,new ConfigSiftDetector(2,0,200,5),null,null); | |||
ScoreAssociation< | ScoreAssociation<TupleDesc_F64> scorer = FactoryAssociation.scoreEuclidean(TupleDesc_F64.class, true); | ||
AssociateDescription<TupleDesc_F64> associate = FactoryAssociation.greedy(new ConfigAssociateGreedy(true, 0.1), scorer); | |||
var findMatches = new ExampleAssociatePoints<>(detDesc, associate, GrayF32.class); | |||
findMatches.associate(left,right); | findMatches.associate(left, right); | ||
List<AssociatedPair> matches = new ArrayList< | List<AssociatedPair> matches = new ArrayList<>(); | ||
FastAccess<AssociatedIndex> matchIndexes = associate.getMatches(); | |||
for( int i = 0; i < matchIndexes.size; i++ ) { | for (int i = 0; i < matchIndexes.size; i++) { | ||
AssociatedIndex a = matchIndexes.get(i); | AssociatedIndex a = matchIndexes.get(i); | ||
var p = new AssociatedPair(findMatches.pointsA.get(a.src), findMatches.pointsB.get(a.dst)); | |||
matches.add( p); | matches.add(p); | ||
} | } | ||
Line 133: | Line 124: | ||
} | } | ||
public static void main( String | public static void main( String[] args ) { | ||
String dir = UtilIO.pathExample("structure/"); | |||
String dir = " | |||
BufferedImage imageA = UtilImageIO.loadImage(dir | BufferedImage imageA = UtilImageIO.loadImage(dir, "undist_cyto_01.jpg"); | ||
BufferedImage imageB = UtilImageIO.loadImage(dir | BufferedImage imageB = UtilImageIO.loadImage(dir, "undist_cyto_02.jpg"); | ||
List<AssociatedPair> matches = computeMatches(imageA,imageB); | List<AssociatedPair> matches = computeMatches(imageA, imageB); | ||
// Where the fundamental matrix is stored | // Where the fundamental matrix is stored | ||
DMatrixRMaj F; | |||
// List of matches that matched the model | // List of matches that matched the model | ||
List<AssociatedPair> inliers = new ArrayList< | List<AssociatedPair> inliers = new ArrayList<>(); | ||
// estimate and print the results using a robust and simple estimator | // 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 | // 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. | // Also note that the fundamental matrix is only defined up to a scale factor. | ||
F = robustFundamental(matches, inliers); | F = robustFundamental(matches, inliers, 0.5); | ||
System.out.println("Robust"); | System.out.println("Robust"); | ||
CommonOps_DDRM.divide(F, NormOps_DDRM.normF(F)); // scale to make comparision easier | |||
F.print(); | F.print(); | ||
F = simpleFundamental(matches); | F = simpleFundamental(matches); | ||
System.out.println("Simple"); | System.out.println("Simple"); | ||
CommonOps_DDRM.divide(F, NormOps_DDRM.normF(F)); | |||
F.print(); | F.print(); | ||
// display the inlier matches found using the robust estimator | // display the inlier matches found using the robust estimator | ||
var panel = new AssociationPanel(20); | |||
panel.setAssociation(inliers); | panel.setAssociation(inliers); | ||
panel.setImages(imageA,imageB); | panel.setImages(imageA, imageB); | ||
ShowImages.showWindow(panel, "Inlier Pairs"); | ShowImages.showWindow(panel, "Inlier Pairs"); |
Latest revision as of 16:31, 17 January 2022
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: ExampleComputeFundamentalMatrix.java
Concepts:
- Epipolar constraint
- Fundamental matrix
- Stereo Vision
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, inlier features
* from robust model fitting are shown.
*
* @author Peter Abeles
*/
public class ExampleComputeFundamentalMatrix {
/**
* 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, double inlierThreshold ) {
var configRansac = new ConfigRansac();
configRansac.inlierThreshold = inlierThreshold;
configRansac.iterations = 1000;
ConfigFundamental configFundamental = new ConfigFundamental();
configFundamental.which = EnumFundamental.LINEAR_7;
configFundamental.numResolve = 2;
configFundamental.errorModel = ConfigFundamental.ErrorModel.GEOMETRIC;
// geometric error is the most accurate error metric, but also the slowest to compute. See how the
// results change if you switch to sampson and how much faster it is. You also should adjust
// the inlier threshold.
ModelMatcher<DMatrixRMaj, AssociatedPair> ransac =
FactoryMultiViewRobust.fundamentalRansac(configFundamental, configRansac);
// Estimate the fundamental matrix while removing outliers
if (!ransac.process(matches))
throw new IllegalArgumentException("Failed");
// save the set of features that were used to compute the fundamental matrix
inliers.addAll(ransac.getMatchSet());
// Improve the estimate of the fundamental matrix using non-linear optimization
var F = new DMatrixRMaj(3, 3);
ModelFitter<DMatrixRMaj, AssociatedPair> refine =
FactoryMultiView.fundamentalRefine(1e-8, 400, EpipolarError.SAMPSON);
if (!refine.fitModel(inliers, ransac.getModelParameters(), F))
throw new IllegalArgumentException("Failed");
// Return the solution
return F;
}
/**
* 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 DMatrixRMaj simpleFundamental( List<AssociatedPair> matches ) {
// Use the 8-point algorithm since it will work with an arbitrary number of points
Estimate1ofEpipolar estimateF = FactoryMultiView.fundamental_1(EnumFundamental.LINEAR_8, 0);
var F = new DMatrixRMaj(3, 3);
if (!estimateF.process(matches, F))
throw new IllegalArgumentException("Failed");
// while not done here, this initial linear estimate can be refined using non-linear optimization
// as was done above.
return F;
}
/**
* 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 ) {
DetectDescribePoint<GrayF32, TupleDesc_F64> detDesc = FactoryDetectDescribe.surfStable(
new ConfigFastHessian(0, 2, 400, 1, 9, 4, 4), null, null, GrayF32.class);
// DetectDescribePoint detDesc = FactoryDetectDescribe.sift(null,new ConfigSiftDetector(2,0,200,5),null,null);
ScoreAssociation<TupleDesc_F64> scorer = FactoryAssociation.scoreEuclidean(TupleDesc_F64.class, true);
AssociateDescription<TupleDesc_F64> associate = FactoryAssociation.greedy(new ConfigAssociateGreedy(true, 0.1), scorer);
var findMatches = new ExampleAssociatePoints<>(detDesc, associate, GrayF32.class);
findMatches.associate(left, right);
List<AssociatedPair> matches = new ArrayList<>();
FastAccess<AssociatedIndex> matchIndexes = associate.getMatches();
for (int i = 0; i < matchIndexes.size; i++) {
AssociatedIndex a = matchIndexes.get(i);
var 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 = 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, 0.5);
System.out.println("Robust");
CommonOps_DDRM.divide(F, NormOps_DDRM.normF(F)); // scale to make comparision easier
F.print();
F = simpleFundamental(matches);
System.out.println("Simple");
CommonOps_DDRM.divide(F, NormOps_DDRM.normF(F));
F.print();
// display the inlier matches found using the robust estimator
var panel = new AssociationPanel(20);
panel.setAssociation(inliers);
panel.setImages(imageA, imageB);
ShowImages.showWindow(panel, "Inlier Pairs");
}
}