Main Content

estimateFundamentalMatrix

Estimate fundamental matrix from corresponding points in stereo images

Description

example

estimateFundamentalMatrix从correspondin估计基本矩阵g points in stereo images. This function can be configured to use all corresponding points or to exclude outliers. You can exclude outliers by using a robust estimation technique such as random-sample consensus (RANSAC). When you use robust estimation, results may not be identical between runs because of the randomized nature of the algorithm.

example

F= estimateFundamentalMatrix(matchedPoints1,matchedPoints2)returns the 3-by-3 fundamental matrix,F, using the least median of squares (LMedS) method from matched feature points in stereo images.

[F,inliersIndex] = estimateFundamentalMatrix(matchedPoints1,matchedPoints2)additionally returns logical indices,inliersIndex,内围层用于计算的基础matrix. TheinliersIndexoutput is anM-by-1 vector. The function sets the elements of the vector totruewhen the corresponding point was used to compute the fundamental matrix. The elements are set tofalseif they are not used.

[F,inliersIndex,status] = estimateFundamentalMatrix(matchedPoints1,matchedPoints2)additionally returns a status code.

[F,inliersIndex,status] = estimateFundamentalMatrix(matchedPoints1,matchedPoints2,Name=Value)specifies options using one or more name-value arguments in addition to any combination of arguments from previous syntaxes. For example,estimateFundamentalMatrix(matchedPoints1,matchedPoints2,Method="MSAC")specifies MSAC as the method to compute the fundamental matrix.

Examples

collapse all

Use the random sample consensus (RANSAC) method to compute the fundamental matrix. The RANSAC method requires that the input points are putatively matched. You can use the matchFeatures function to return these matched points. Outlier points which may still be contained within putatively matched points are further eliminated by using the RANSAC algorithm.

Load stereo points into the workspace.

loadstereoPointPairs

Estimate the fundamental matrix.

fRANSAC = estimateFundamentalMatrix(matchedPoints1,...matchedPoints2,Method="RANSAC",...NumTrials=2000,DistanceThreshold=1e-4)
fRANSAC =3×30.0000 -0.0004 0.0348 0.0004 0.0000 -0.0937 -0.0426 0.0993 0.9892

Load the putatively matched points into the workspace.

loadstereoPointPairs[fLMedS,inliers] = estimateFundamentalMatrix(matchedPoints1,matchedPoints2,NumTrials=2000)
fLMedS =3×30.0000 -0.0004 0.0349 0.0004 0.0000 -0.0938 -0.0426 0.0994 0.9892
inliers =18x1 logical array1 1 1 1 1 1 0 1 0 0 ⋮

Load the stereo images.

I1 = imread("viprectification_deskLeft.png"); I2 = imread("viprectification_deskRight.png");

Show the putatively matched points.

figure; showMatchedFeatures(I1,I2,matchedPoints1,matchedPoints2,"montage",PlotOptions=["ro","go","y--"]); title("Putative Point Matches");

Figure contains an axes object. The axes object with title Putative Point Matches contains 4 objects of type image, line. One or more of the lines displays its values using only markers

Show the inlier points.

figure; showMatchedFeatures(I1,I2,matchedPoints1(inliers,:),matchedPoints2(inliers,:),"montage",PlotOptions=["ro","go","y--"]); title("Point Matches After Outliers Are Removed");

Figure contains an axes object. The axes object with title Point Matches After Outliers Are Removed contains 4 objects of type image, line. One or more of the lines displays its values using only markers

Load the stereo point pairs into the workspace.

loadstereoPointPairs

Compute the fundamental matrix for input points which do not contain any outliers.

inlierPts1 = matchedPoints1(knownInliers,:); inlierPts2 = matchedPoints2(knownInliers,:); fNorm8Point = estimateFundamentalMatrix(inlierPts1,inlierPts2,Method="Norm8Point")
fNorm8Point =3×30.0000 -0.0004 0.0348 0.0004 0.0000 -0.0937 -0.0426 0.0993 0.9892

Input Arguments

collapse all

Coordinates of corresponding points in image one, specified as anM-by-2 matrix ofMnumber of [x y] coordinates, or as one of the point feature objects described inPoint Feature Types. ThematchedPoints1input must contain points which do not lie on a single planar surface, (e.g., a wall, table, or book) and are putatively matched by using a function such asmatchFeatures.

Coordinates of corresponding points in image one, specified as anM-by-2 matrix ofMnumber of [x y] coordinates, or as one of the point feature objects described inPoint Feature Types. ThematchedPoints1input must contain points which do not lie on a single planar surface, (e.g., a wall, table, or book) and are putatively matched by using a function such asmatchFeatures.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:estimateFundamentalMatrix(matchedPoints1,matchedPoints2,Method="MSAC")specifies MSAC as the method to compute the fundamental matrix.

Method used to compute the fundamental matrix, specified as one of the following:

"LMedS" Least Median of Squares. Use this method if at least 50% of the points inmatchedPoints1andmatchedPoints2are inliers. The function stops the search for inliers and the estimation of the fundamental matrix after 50% of the inliers are found.
"MSAC" M-estimator SAmple Consensus. Select the M-estimator SAmple Consensus method if you would like to set the distance threshold for the inliers. Generally, the MSAC method converges more quickly than the RANSAC method.
"Norm8Point" Normalized eight-point algorithm[1]. To produce reliable results, the inputs,matchedPoints1andmatchedPoints2must match precisely.

To produce reliable results using theNorm8Pointalgorithm, the inputs,matchedPoints1andmatchedPoints2, must match precisely. The other methods can tolerate outliers and therefore only require putatively matched input points. You can obtain putatively matched points by using thematchFeaturesfunction.

Fundamental matrix class, specified as"double"or"single". Use this property to specify the class for the fundamental matrix and for the function's internal computations.

Number of random trials for finding the outliers, specified as an integer. This argument applies when you set theMethodtoLMedSorMSAC.

  • LMedS— The function uses the actual number of trials as the parameter value.

  • MSAC— The function uses the maximum number of trials as the parameter value.

The actual number of trials depends onmatchedPoints1,matchedPoints2, and the value of theConfidenceparameter. Select the number of random trials to optimize speed and accuracy.

Distance threshold for finding outliers, specified as a positive scalar. This parameter applies when you set theMethodtoMSAC.

Desired confidence for finding maximum number of inliers, specified as a percentage scalar in the range(0 100). This argument applies when you setMethodtoMSAC.

Report runtime error, specified as a logical value. Set this parameter totrueto report run-time errors when the function cannot compute the fundamental matrix frommatchedPoints1andmatchedPoints2. When you set this parameter tofalse, you can check thestatusoutput to verify validity of the fundamental matrix.

Output Arguments

collapse all

Fundamental matrix, returned as a 3-by-3 matrix that is computed from the points in the inputsmatchedPoints1andmatchedPoints2.

[ P 2 1 ] * F u n d a m e n t a l M a t r i x * [ P 1 1 ] ' = 0

P1, the point inmatchedPoints1of image 1 in pixels, corresponds to the point, P2, the point inmatchedPoints2in image 2.

In computer vision, the fundamental matrix is a 3-by-3 matrix which relates corresponding points in stereo images. When two cameras view a 3-D scene from two distinct positions, there are a number of geometric relations between the 3-D points and their projections onto the 2-D images that lead to constraints between the image points. Two images of the same scene are related by epipolar geometry.

Inliers index, returned as anM-by-1 logical index vector. An element set totruemeans that the corresponding indexed matched points inmatchedPoints1andmatchedPoints2were used to compute the fundamental matrix. An element set tofalsemeans the indexed points were not used for the computation.

Data Types:logical

Status code, returned as one of the following possible values:

status Value
0: No error.
1: matchedPoints1andmatchedPoints2do not contain enough points. TheNorm8PointandMSACmethods require at least 8 points, and theLMedSmethod requires 16 points.
2: Not enough inliers found.

Data Types:int32

Tips

UseestimateEssentialMatrixwhen you know the camera intrinsics. You can obtain the intrinsics using theCamera Calibratorapp. Otherwise, you can use theestimateFundamentalMatrixfunction that does not require camera intrinsics. Note that the fundamental matrix cannot be estimated from coplanar world points.

Algorithms

collapse all

Computing the Fundamental Matrix

When you choose theNorm8Pointmethod, the function uses all points inmatchedPoints1andmatchedPoints2to compute the fundamental matrix. When you choose any other method, the function uses the following algorithm to exclude outliers and compute the fundamental matrix from inliers:

  1. Initialize the fundamental matrix,F, to a 3-by-3 matrix of zeros.

  2. Set the loop countern, to zero, and the number of loopsN, to the number of random trials specified.

  3. Loop through the following steps whilen<N:

    1. Randomly select 8 pairs of points frommatchedPoints1andmatchedPoints2.

    2. 使用所选的8点来计算基础matrix,f, by using the normalized 8-point algorithm.

    3. Compute the fitness offfor all points inmatchedPoints1andmatchedPoints2.

    4. If the fitness offis better thanF, replaceFwithf.

      For theMSACmethod, updateN.

    5. n=n+ 1

Number of Random Samplings for the MSAC Method

The MSAC method updates the number of random trialsNfor every iteration in the algorithm loop. The function resetsN, according to the following:

N= min(N, log ( 1 p ) log ( 1 r 8 ) ).
Where,prepresents the confidence parameter you specified, andris calculated as follows:
i N sgn ( d u i , v i ) , t ) / N , where sgn ( a , b ) = 1 if a b and 0 otherwise.
When you use theMSACmethod, results may not be identical between runs because of the randomized nature of the algorithm.

Sampson Distance

The function uses the Sampson distance to measure the distance of a pair of points according to a fundamental matrix. The equation below can be used to calculate the Sampson distance. In the equation,urepresentsmatchedPoints1andvrepresentsmatchedPoints2.

d ( u i , v i ) = ( v i F u i T ) 2 [ 1 ( F u i T ) 1 2 + ( F u i T ) 2 2 + 1 ( v i F ) 1 2 + ( v i F ) 2 2 ]

where i represents the index of the corresponding points, and ( F u i T ) j 2 , the square of thej-th entry of the vector F u i T .

Fitness of Fundamental Matrix for Corresponding Points

The following table summarizes how each method determines the fitness of the computed fundamental matrix:

Method Measure of Fitness
LMedS m e d i a n ( d ( u i , v i ) ; i = 1 : N ) , the number of input points. The smaller the value, the better the fitness.
MSAC i N min ( d ( u i , v i ) , t ) . The smaller the value, the better the fitness.

References

[1] Hartley, R., A. Zisserman,Multiple View Geometry in Computer Vision, Cambridge University Press, 2003.

[2] Rousseeuw, P., A. Leroy,Robust Regression and Outlier Detection, John Wiley & Sons, 1987.

[3] Torr, P. H. S., and A. Zisserman,MLESAC: A New Robust Estimator with Application to Estimating Image Geometry, Computer Vision and Image Understanding, 2000.

Extended Capabilities

Version History

Introduced in R2012b