Wrong rank in Fundamental Matrix

asked 2018-11-26 11:04:12 -0600

Lucas Amparo Barbosa gravatar image

Hi guys,

I'm using the OpenCV for Python3 and, based on the Mastering OpenCV Book, try to compute the epipoles from many images (Structure from Motion algorithm).

In many books, they say which Fundamental Matrix has rank 2. But, the OpenCV function returns a rank 3 matrix.

How can I make this right?

  orb = cv2.ORB_create()

  # find the keypoints and descriptors with ORB
  kp1, des1 = orb.detectAndCompute(img1,None)
  kp2, des2 = orb.detectAndCompute(img2,None)

  # create BFMatcher object
  bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)

  # Match descriptors.
  matches = bf.match(des1,des2)

  # Sort them in the order of their distance.
  matches = sorted(matches, key = lambda x:x.distance)

  pts1 = []
  pts2 = []
  for m in matches:
      pts2.append(kp2[m.trainIdx].pt)
      pts1.append(kp1[m.queryIdx].pt)

  F, mask = cv2.findFundamentalMat(pts1, pt2,cv2.FM_RANSAC)
  pts1 = match['leftPts'][mask.ravel()==1]
  pts2 = match['rightPts'][mask.ravel()==1]
  # F is the Fundamental Matrix

From that code, the output are like

Processing image 0 and image 1
rank of F: 3
Processing image 0 and image 2
rank of F: 3
Processing image 0 and image 3
rank of F: 3
Processing image 0 and image 4
rank of F: 2
[...]

Someone could help me? Someone have any functional code for SfM using OpenCV? Thanks in advance.

edit retag flag offensive close merge delete

Comments

Can you show one example of a Fundamentalmatrix and how do you determine the rank?

Grillteller gravatar imageGrillteller ( 2018-11-27 02:50:42 -0600 )edit

[[-2.29390496e-08 8.55007340e-07 -1.07108724e-03] [ 2.87605686e-08 -2.30601171e-06 2.86179300e-03] [ 2.94004722e-05 -7.92971149e-04 1.00000000e+00]] Rank of F: 3

Lucas Amparo Barbosa gravatar imageLucas Amparo Barbosa ( 2018-11-27 06:25:55 -0600 )edit

[[ 1.95163442e-08 1.34608880e-07 -3.20936720e-04] [ 4.76676164e-08 5.75973164e-07 -1.02011608e-03] [-5.95314437e-05 -4.32612338e-04 1.00000000e+00]] Rank of F: 3

Lucas Amparo Barbosa gravatar imageLucas Amparo Barbosa ( 2018-11-27 06:26:32 -0600 )edit

computed by numpy.linalg.matrix_rank function

Lucas Amparo Barbosa gravatar imageLucas Amparo Barbosa ( 2018-11-27 06:26:45 -0600 )edit