Advertisement
Guest User

chessboard.py

a guest
Jun 11th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.75 KB | None | 0 0
  1. #Forces code to use OPENCV install in virtual environment
  2. import sys
  3. sys.path.remove('/usr/local/lib/python2.7/site-packages')
  4.  
  5. import numpy as np
  6. import cv2
  7. import glob
  8.  
  9. ####################################
  10. #FIRST CHESSBOARD (PRIMARY CAMERA TO CHESSBOARD IN SPACE)
  11. ####################################
  12.  
  13. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  14.  
  15. # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
  16. objp = np.zeros((7*7,3), np.float32)
  17. objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
  18. #Multiply by 50.8 because 50.8 mm is length of real chessboard
  19. objp = 50.8 * objp
  20.  
  21.  
  22. # Arrays to store object points and image points from all the images.
  23. objpoints = [] # 3d point in real world space
  24. imgpoints = [] # 2d points in image plane.
  25.  
  26. images = glob.glob('*.jpeg')
  27.  
  28. for fname in images:
  29.     img = cv2.imread(fname)
  30.     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  31.     # Find the chess board corners
  32.     ret, corners = cv2.findChessboardCorners(gray, (7,7),None)
  33.  
  34.     # If found, add object points, image points (after refining them)
  35.     if ret == True:
  36.         print "found"
  37.         objpoints.append(objp)
  38.         #Rotating corner points because webcam flips chessboard
  39.         corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
  40.         corners2 = np.flipud(corners2)
  41.         corners3 = np.copy(corners2)
  42.         multiple = 0
  43.         for i in range(0,49):
  44.             if (i%7 == 0) and (i!=0):
  45.                 multiple = multiple + 1
  46.             corners3[i] = corners2[(6-(i-(7*multiple))) + 7*multiple]
  47.         corners4 = np.copy(corners3)
  48.         k = 0
  49.         for i in range(0,7):
  50.             for j in range(0,49,7):
  51.                 corners4[k] = corners3[i+j]
  52.                 k = k + 1
  53.  
  54.         corners5 = np.copy(corners4)
  55.         multiple = 0
  56.         for i in range(0,49):
  57.             if (i%7 == 0) and (i!=0):
  58.                 multiple = multiple + 1
  59.             corners5[i] = corners4[(6-(i-(7*multiple))) + 7*multiple]
  60.  
  61.         corners2 = corners5
  62.         imgpoints.append(corners2)
  63.  
  64.         # Draw and display the corners
  65.         img = cv2.drawChessboardCorners(img, (7,7), corners2,ret)
  66.         cv2.imshow('img',img)
  67.         cv2.waitKey()
  68.     else:
  69.         print "not found"
  70.         print fname
  71.     #Displays grayscale image
  72.     # cv2.imshow('img',gray)
  73.     # cv2.waitKey()
  74.  
  75. #Getting camera matrix, distortion coefficients, and rvecs, tvecs
  76. ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  77.  
  78. #Displaying the axes
  79. i = 0
  80. for fname in images:
  81.     img = cv2.imread(fname)
  82.     cv2.aruco.drawAxis(img,mtx,dist,rvecs[i],tvecs[i],20.64*5)
  83.     cv2.imshow('img',img)
  84.     cv2.waitKey()
  85.     i = i +1
  86.  
  87. ####################################
  88. #SECOND CHESSBOARD (SECONDARY CAMERA TO CHESSBOARD IN SPACE)
  89. ####################################
  90.  
  91. objp = np.zeros((7*7,3), np.float32)
  92. objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
  93. #Multiply by 50.8 because 50.8 mm is length of real chessboard
  94. objp = 50.8 * objp
  95.  
  96. # Arrays to store object points and image points from all the images.
  97. objpoints = [] # 3d point in real world space
  98. imgpoints = [] # 2d points in image plane.
  99.  
  100. secondary_images = glob.glob("/Users/XXX/code/facregtests/screen_loc/secondary_camera/*.jpeg")
  101. for fname in secondary_images:
  102.     img = cv2.imread(fname)
  103.     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  104.     # Find the chess board corners
  105.     ret, corners = cv2.findChessboardCorners(gray, (7,7),None)
  106.  
  107.     # If found, add object points, image points (after refining them)
  108.     if ret == True:
  109.         print "found"
  110.         objpoints.append(objp)
  111.         corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
  112.         imgpoints.append(corners2)
  113.  
  114.         # Draw and display the corners
  115.         img = cv2.drawChessboardCorners(img, (7,7), corners2,ret)
  116.         # cv2.imshow('img',img)
  117.         # cv2.waitKey()
  118.     else:
  119.         print "not found"
  120.         print fname
  121.     #Displays grayscale image
  122.     # cv2.imshow('img',gray)
  123.     # cv2.waitKey()
  124.  
  125. #Getting camera matrix, distortion coefficients, and rvecs, tvecs
  126. ret2, mtx2, dist2, rvecs2, tvecs2 = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  127.  
  128.  
  129. ####################################
  130. #THIRD CHESSBOARD (SECONDARY CAMERA TO CHESSBOARD ON SCREEN)
  131. ####################################
  132. objp = np.zeros((7*7,3), np.float32)
  133. objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
  134. #Multiply by 20.64 because 20.64 mm is length of real chessboard
  135. objp = 20.64 * objp
  136.  
  137. # Arrays to store object points and image points from all the images.
  138. objpoints = [] # 3d point in real world space
  139. imgpoints = [] # 2d points in image plane.
  140. three_images = glob.glob("/Users/XXX/code/facregtests/screen_loc/secondary_camera/cropped/*.jpeg")
  141. for fname in three_images:
  142.     img = cv2.imread(fname)
  143.     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  144.     # Find the chess board corners
  145.     ret, corners = cv2.findChessboardCorners(gray, (7,7),None)
  146.     # If found, add object points, image points (after refining them)
  147.     if ret == True:
  148.         print "found"
  149.         objpoints.append(objp)
  150.         corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
  151.         #Rotating corners to be consistent with other chessboard and cameras  
  152.         corners2 = np.flipud(corners2)
  153.         corners3 = np.copy(corners2)
  154.         multiple = 0
  155.         for i in range(0,49):
  156.             if (i%7 == 0) and (i!=0):
  157.                 multiple = multiple + 1
  158.             corners3[i] = corners2[(6-(i-(7*multiple))) + 7*multiple]
  159.         corners4 = np.copy(corners3)
  160.         k = 0
  161.         for i in range(0,7):
  162.             for j in range(0,49,7):
  163.                 corners4[k] = corners3[i+j]
  164.                 k = k + 1
  165.         corners2 = corners4
  166.         imgpoints.append(corners2)
  167.         # Draw and display the corners
  168.         img = cv2.drawChessboardCorners(img, (7,7), corners2,ret)
  169.         # cv2.imshow('img',img)
  170.         # cv2.waitKey()
  171.     else:
  172.         print "not found"
  173.         print fname
  174.     #Displays grayscale image
  175.     # cv2.imshow('img',gray)
  176.     # cv2.waitKey()
  177.  
  178. #Getting camera matrix, distortion coefficients, and rvecs, tvecs
  179. ret3, mtx3, dist3, rvecs3, tvecs3 = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  180.  
  181.  
  182.  
  183. #Averaging the rvecs and tvecs for the primary camera
  184. rvec_primary = rvecs[0]
  185. tvec_primary = tvecs[0]
  186. i = 0
  187. for rvec in rvecs:
  188.     if not i == 0:
  189.         rvec_primary  = rvec_primary + rvec
  190.     else:
  191.         i = 1
  192. j = 0
  193. for tvec in tvecs:
  194.     if not j == 0:
  195.         tvec_primary = tvec_primary + tvec
  196.     else:
  197.         j = 1
  198.  
  199. rvec_primary = rvec_primary/len(rvecs)
  200. tvec_primary = tvec_primary/len(tvecs)
  201.  
  202.  
  203.  
  204. #Applyihg first transform with (0,0,0) for top left corner
  205. rot_primary,_ = cv2.Rodrigues(rvec_primary)
  206. corner1 = np.zeros((1,3),np.float32)
  207. first_transform_output = rot_primary.dot(corner1.T) + tvec_primary
  208.  
  209. #Applying second transform -- for now just testing with first rvec, tvec pair
  210. #TODO: use all rvec, tvec pairs and average end results
  211. rvec_secondary = rvecs2[0]
  212. tvec_secondary = tvecs2[0]
  213. rot_secondary,_ = cv2.Rodrigues(rvec_secondary)
  214. #Inverting transform
  215. rot_secondary = rot_secondary.T
  216. tvec_secondary = (-1*rot_secondary).dot(tvec_secondary)
  217. second_transform_output = rot_secondary.dot(first_transform_output) + tvec_secondary
  218.  
  219. #Applying Third transform -- for now just testing with first rvec, tvec pair
  220. #TODO: use all rvec, tvec pairs and average end results
  221. rvec_three = rvecs3[0]
  222. tvec_three = tvecs3[0]
  223. rot_three,_ = cv2.Rodrigues(rvec_three)
  224. third_transform_output = rot_three.dot(second_transform_output) + tvec_three
  225.  
  226. #Printing output -- should be close to (-165,5,0)
  227. print third_transform_output
  228.  
  229.  
  230.  
  231. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement