def compute_fundamental(x1, x2, method=cv2.FM_RANSAC):
"""
Computes the fundamental matrix from corresponding points x1, x2 using
the 8 point algorithm.
Options:
CV_FM_7POINT for a 7-point algorithm. N = 7
CV_FM_8POINT for an 8-point algorithm. N >= 8
CV_FM_RANSAC for the RANSAC algorithm. N >= 8
CV_FM_LMEDS for the LMedS algorithm. N >= 8"
"""
assert(x1.shape == x2.shape)
if len(x1) < 8:
raise RuntimeError('Fundamental matrix requires N >= 8 pts')
F, mask = cv2.findFundamentalMat(x1, x2, method)
return F, mask
python类findFundamentalMat()的实例源码
def calculate_fundamental_matrix(self, previous_pts, current_pts):
fundamental_matrix, mask = cv2.findFundamentalMat(
previous_pts,
current_pts,
cv2.FM_RANSAC
)
if fundamental_matrix is None or fundamental_matrix.shape == (1, 1):
# dang, no fundamental matrix found
raise Exception('No fundamental matrix found')
elif fundamental_matrix.shape[0] > 3:
# more than one matrix found, just pick the first
fundamental_matrix = fundamental_matrix[0:3, 0:3]
return np.matrix(fundamental_matrix)
def get_fundamental_mat_normalized(p1,p2):
F_, inliers = cv2.findFundamentalMat(p1.astype('float32'), p2.astype('float32'), method=cv2.LMEDS)
return F_,inliers
def old_get_fundamental_mat_normalized(p1,p2,use_ransac=False):
"""
A small wrapper around cv2.getFundamentalMat, with normalization of
point locations.
"""
mu1 = p1.mean(axis=0)
std1 = p1.std(axis=0)
mu2 = p2.mean(axis=0)
std2 = p2.std(axis=0)
p1_ = (p1 - mu1) / std1
p2_ = (p2 - mu2) / std2
if use_ransac:
F_, inliers = cv2.findFundamentalMat(p1_,p2_,method=cv2.FM_RANSAC,param1=4 * 2.0/(std1+std2).mean())
else:
F_,inliers = cv2.findFundamentalMat(p1_,p2_,method=cv2.FM_LMEDS)
A1 = np.array([[1.0/std1[0], 0.0, -mu1[0]/std1[0]],
[0, 1.0/std1[1], -mu1[1]/std1[1]],
[0,0,1.0]])
A2 = np.array([[1.0/std2[0], 0.0, -mu2[0]/std2[0]],
[0, 1.0/std2[1], -mu2[1]/std2[1]],
[0,0,1.0]])
F = A2.T.dot(F_).dot(A1)
#F = A2inv.dot(H_).dot(A1)
return F,inliers
#
# Normalization functions
#