Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. # Import library
  2.  
  3. import numpy as np
  4. import cv2
  5. from matplotlib import pyplot as plt
  6. import time
  7.  
  8.  
  9. def degeneracyCheckPass(first_points, second_points, rot, trans):
  10. rot_inv = rot
  11. for first, second in zip(first_points, second_points):
  12. first_z = np.dot(rot[0, :] - second[0]*rot[2, :], trans) / np.dot(rot[0, :] - second[0]*rot[2, :], second)
  13. first_3d_point = np.array([first[0] * first_z, second[0] * first_z, first_z])
  14. second_3d_point = np.dot(rot.T, first_3d_point) - np.dot(rot.T, trans)
  15.  
  16. if first_3d_point[2] < 0 or second_3d_point[2] < 0:
  17. return False
  18.  
  19. return True
  20.  
  21. start = time.time();
  22. # Load a pair of images
  23. img1 = cv2.imread('1.jpg',1)
  24. img2 = cv2.imread('2.jpg',1)
  25.  
  26. #camera_matrix:
  27.  
  28. #cmat is K
  29. zero = np.float32([[0.0], [0.0], [0.0]])
  30. cmat = np.float32([[2610.9791576918024, 0.0, 1035.3907882371566], [0.0, 2611.5091416583214, 479.1873404639389], [0.0, 0.0, 1.0]])
  31. cmat_inv = np.linalg.inv(cmat)
  32. #dist_coefs:
  33. dist = np.matrix('0.10783691621695163 0.0979247249215728 -0.010931168141218273 0.009561950912198006 -1.745839718546563')
  34.  
  35.  
  36. # Initiate ORB detector
  37. orb = cv2.ORB_create()
  38.  
  39. # Find the keypoints and descriptors with ORB
  40. kp1, des1 = orb.detectAndCompute(img1,None)
  41. kp2, des2 = orb.detectAndCompute(img2,None)
  42.  
  43. # Create BFMatcher object
  44. bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
  45.  
  46. # Match descriptors.
  47. matches = bf.match(des1,des2)
  48.  
  49.  
  50. #Ratio test as per Lowe's paper
  51. good = []
  52. pts1 = []
  53. pts2 = []
  54.  
  55. dist = [m.distance for m in matches]
  56. thres_dist = (sum(dist) / len(dist)) * 0.8
  57. for m in matches:
  58. if m.distance < thres_dist:
  59. good.append(m)
  60. pts2.append(kp2[m.trainIdx].pt)
  61. pts1.append(kp1[m.queryIdx].pt)
  62.  
  63. # Draw matches.
  64. img3 = cv2.drawMatches(img1,kp1,img2,kp2,good, None, flags=2)
  65. plt.imshow(img3),plt.show()
  66.  
  67. # Get the list of best matches from both the images
  68. pts1 = np.int32(pts1)
  69. pts2 = np.int32(pts2)
  70.  
  71. # Find fundamental Matrix
  72. F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_LMEDS)
  73.  
  74. # Select inlier points
  75. pts1 = pts1[mask.ravel()==1]
  76. pts2 = pts2[mask.ravel()==1]
  77.  
  78. # Find essential matrix
  79. #E = K'^T . F . K
  80. E = cmat.T.dot(F).dot(cmat)
  81.  
  82.  
  83. # Compute camera pose
  84.  
  85. U, S, Vt = np.linalg.svd(E)
  86. W = np.array([0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]).reshape(3, 3)
  87. first_inliers = []
  88. second_inliers = []
  89.  
  90.  
  91. for i in range(len(pts1)):
  92. first_inliers.append(cmat_inv.dot([pts1[i][0], pts1[i][1], 1.0]))
  93. second_inliers.append(cmat_inv.dot([pts2[i][0], pts2[i][1], 1.0]))
  94.  
  95.  
  96. # First choice: R = U * W * Vt, T = u_3
  97. R = U.dot(W).dot(Vt)
  98. T = U[:, 2]
  99.  
  100. # Start degeneracy checks
  101. if not degeneracyCheckPass(first_inliers, second_inliers, R, T):
  102. # Second choice: R = U * W * Vt, T = -u_3
  103. T = - U[:, 2]
  104. if not degeneracyCheckPass(first_inliers, second_inliers, R, T):
  105. # Third choice: R = U * Wt * Vt, T = u_3
  106. R = U.dot(W.T).dot(Vt)
  107. T = U[:, 2]
  108. if not degeneracyCheckPass(first_inliers, second_inliers, R, T):
  109. # Fourth choice: R = U * Wt * Vt, T = -u_3
  110. T = - U[:, 2]
  111.  
  112. T_r = T.reshape((T.shape[0],-1))
  113.  
  114. # Reconstruct 3D location of matched points
  115. # Get camera projection matrix
  116.  
  117.  
  118. P1 = np.hstack((cmat, zero))
  119. P2 = np.dot(cmat, np.hstack((R,T_r)))
  120.  
  121.  
  122. pts1t = pts1.transpose()
  123. pts2t = pts2.transpose()
  124.  
  125.  
  126. floatpst1t = np.array([pts1t[0,:].astype(np.float) / 1944.0, pts1t[1,:].astype(np.float) / 2592.0])
  127.  
  128.  
  129. floatpst2t = np.array([pts2t[0,:].astype(np.float) / 1944.0, pts2t[1,:].astype(np.float) / 2592.0])
  130.  
  131.  
  132. #Demo projection matrix
  133. #P1 = np.eye(4)
  134. #P2 = np.array([[ 0.878, -0.01 , 0.479, -1.995],
  135. # [ 0.01 , 1. , 0.002, -0.226],
  136. # [-0.479, 0.002, 0.878, 0.615],
  137. # [ 0. , 0. , 0. , 1. ]])
  138. X = cv2.triangulatePoints(P1[:4], P2[:4], floatpst1t, floatpst2t)
  139. print X
  140. #here I will divide the first three parameters with the fourth one to get the 3d coordinate
  141.  
  142. finish = time.time();
  143. print(finish - start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement