Advertisement
bfc_paste

chessboard.py

Jun 12th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.72 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. #Counter to track which set image we are currently on
  29. #Needed as some images need to be rotated such as to maintain consistency
  30. a = 0
  31. for fname in images:
  32.     img = cv2.imread(fname)
  33.     #Need to flip image as primary camera mirrors images over y axis
  34.     img = cv2.flip(img,1)
  35.     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  36.  
  37.     # Find the chess board corners
  38.     ret, corners = cv2.findChessboardCorners(gray, (7,7),None)
  39.  
  40.     # If found, add object points, image points (after refining them)
  41.     if ret == True:
  42.         print "found"
  43.         objpoints.append(objp)
  44.         corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
  45.         #Need to rotate corner points for some images by 90 degrees counterclockwise
  46.         if (a == 0 or a == 3 or a == 4 or a == 6 or a ==7):
  47.             corners2 = np.flipud(corners2)
  48.             corners3 = np.copy(corners2)
  49.             multiple = 0
  50.             for i in range(0,49):
  51.                 if (i%7 == 0) and (i!=0):
  52.                     multiple = multiple + 1
  53.                 corners3[i] = corners2[(6-(i-(7*multiple))) + 7*multiple]
  54.             corners4 = np.copy(corners3)
  55.             k = 0
  56.             for i in range(0,7):
  57.                 for j in range(0,49,7):
  58.                     corners4[k] = corners3[i+j]
  59.                     k = k + 1
  60.             # corners5 = np.copy(corners4)
  61.             # multiple = 0
  62.             # for i in range(0,49):
  63.             #     if (i%7 == 0) and (i!=0):
  64.             #         multiple = multiple + 1
  65.             #     corners5[i] = corners4[(6-(i-(7*multiple))) + 7*multiple]
  66.             corners2 = corners4
  67.  
  68.        
  69.         imgpoints.append(corners2)
  70.  
  71.         # Draw and display the corners
  72.         img = cv2.drawChessboardCorners(img, (7,7), corners2,ret)
  73.         cv2.imshow('img',img)
  74.         cv2.waitKey()
  75.     else:
  76.         print "not found"
  77.         print fname
  78.     a = a + 1
  79.     #Displays grayscale image
  80.     # cv2.imshow('img',gray)
  81.     # cv2.waitKey()
  82.  
  83. #Getting camera matrix, distortion coefficients, and rvecs, tvecs
  84. ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  85.  
  86.  
  87. #Displaying the axes
  88. i = 0
  89. for fname in images:
  90.     img = cv2.imread(fname)
  91.     img = cv2.flip(img,1)
  92.     cv2.aruco.drawAxis(img,mtx,dist,rvecs[i],tvecs[i],50.8)
  93.     cv2.imshow('img',img)
  94.     cv2.waitKey()
  95.     i = i +1
  96.  
  97.  
  98.  
  99. ####################################
  100. #SECOND CHESSBOARD (SECONDARY CAMERA TO CHESSBOARD IN SPACE)
  101. ####################################
  102.  
  103. objp = np.zeros((7*7,3), np.float32)
  104. objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
  105. #Multiply by 50.8 because 50.8 mm is length of real chessboard
  106. objp = 50.8 * objp
  107.  
  108. # Arrays to store object points and image points from all the images.
  109. objpoints = [] # 3d point in real world space
  110. imgpoints = [] # 2d points in image plane.
  111.  
  112. secondary_images = glob.glob("/Users/XXX/code/facregtests/screen_loc/secondary_camera/*.jpeg")
  113. for fname in secondary_images:
  114.     img = cv2.imread(fname)
  115.     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  116.     # Find the chess board corners
  117.     ret, corners = cv2.findChessboardCorners(gray, (7,7),None)
  118.  
  119.     # If found, add object points, image points (after refining them)
  120.     if ret == True:
  121.         print "found"
  122.         objpoints.append(objp)
  123.         corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
  124.         imgpoints.append(corners2)
  125.  
  126.         # Draw and display the corners
  127.         img = cv2.drawChessboardCorners(img, (7,7), corners2,ret)
  128.         cv2.imshow('img',img)
  129.         cv2.waitKey()
  130.     else:
  131.         print "not found"
  132.         print fname
  133.     #Displays grayscale image
  134.     # cv2.imshow('img',gray)
  135.     # cv2.waitKey()
  136.  
  137. #Getting camera matrix, distortion coefficients, and rvecs, tvecs
  138. ret2, mtx2, dist2, rvecs2, tvecs2 = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  139.  
  140.  
  141. ####################################
  142. #THIRD CHESSBOARD (SECONDARY CAMERA TO CHESSBOARD ON SCREEN)
  143. ####################################
  144. objp = np.zeros((7*7,3), np.float32)
  145. objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
  146. #Multiply by 20.64 because 20.64 mm is length of real chessboard
  147. objp = 20.64 * objp
  148.  
  149. # Arrays to store object points and image points from all the images.
  150. objpoints = [] # 3d point in real world space
  151. imgpoints = [] # 2d points in image plane.
  152. three_images = glob.glob("/Users/XXX/code/facregtests/screen_loc/secondary_camera/cropped/*.jpeg")
  153. for fname in three_images:
  154.     img = cv2.imread(fname)
  155.     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  156.     # Find the chess board corners
  157.     ret, corners = cv2.findChessboardCorners(gray, (7,7),None)
  158.     # If found, add object points, image points (after refining them)
  159.     if ret == True:
  160.         print "found"
  161.         objpoints.append(objp)
  162.         corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
  163.         #Rotating corners to be consistent with other chessboard and cameras  
  164.         corners2 = np.flipud(corners2)
  165.         corners3 = np.copy(corners2)
  166.         multiple = 0
  167.         for i in range(0,49):
  168.             if (i%7 == 0) and (i!=0):
  169.                 multiple = multiple + 1
  170.             corners3[i] = corners2[(6-(i-(7*multiple))) + 7*multiple]
  171.         corners4 = np.copy(corners3)
  172.         k = 0
  173.         for i in range(0,7):
  174.             for j in range(0,49,7):
  175.                 corners4[k] = corners3[i+j]
  176.                 k = k + 1
  177.         corners2 = corners4
  178.         imgpoints.append(corners2)
  179.         # Draw and display the corners
  180.         img = cv2.drawChessboardCorners(img, (7,7), corners2,ret)
  181.         cv2.imshow('img',img)
  182.         cv2.waitKey()
  183.     else:
  184.         print "not found"
  185.         print fname
  186.     #Displays grayscale image
  187.     # cv2.imshow('img',gray)
  188.     # cv2.waitKey()
  189.  
  190. #Getting camera matrix, distortion coefficients, and rvecs, tvecs
  191. ret3, mtx3, dist3, rvecs3, tvecs3 = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  192.  
  193.  
  194.  
  195. #Only using last 3 images for rvecs, tvecs as rest were just for camera matrix, distortion
  196. rvecs = rvecs[-3:]
  197. tvecs = tvecs[-3:]
  198.  
  199. #Averaging the rvecs and tvecs for the primary camera
  200. rvec_primary = rvecs[0]
  201. tvec_primary = tvecs[0]
  202. i = 0
  203. for rvec in rvecs:
  204.     if not i == 0:
  205.         rvec_primary  = rvec_primary + rvec
  206.     else:
  207.         i = 1
  208. j = 0
  209. for tvec in tvecs:
  210.     if not j == 0:
  211.         tvec_primary = tvec_primary + tvec
  212.     else:
  213.         j = 1
  214.  
  215. rvec_primary = rvec_primary/len(rvecs)
  216. tvec_primary = tvec_primary/len(tvecs)
  217.  
  218. #Variable for all outputs for averaging later
  219. outputs = []
  220.  
  221. #Averaging outputs of all rvec, tvec transforms
  222. for i in range(0, len(rvecs2)):
  223.     #Applyihg first transform with (0,0,0) for top left corner
  224.     rvec_three = rvecs3[i]
  225.     tvec_three = tvecs3[i]
  226.     rot_three,_ = cv2.Rodrigues(rvec_three)
  227.     #Inverting transform
  228.     rot_three = rot_three.T
  229.     tvec_three = (-1*rot_three).dot(tvec_three)
  230.     corner1 = np.zeros((1,3),np.float32)
  231.     first_transform_output = rot_three.dot(corner1.T) + tvec_three
  232.  
  233.     #Applying second transform
  234.     rvec_secondary = rvecs2[i]
  235.     tvec_secondary = tvecs2[i]
  236.     rot_secondary,_ = cv2.Rodrigues(rvec_secondary)
  237.     second_transform_output = rot_secondary.dot(first_transform_output) + tvec_secondary
  238.  
  239.     #Applying Third transform
  240.     rot_primary,_ = cv2.Rodrigues(rvec_primary)
  241.     #Inverting transform
  242.     rot_primary = rot_primary.T
  243.     tvec_primary = (-1*rot_primary).dot(tvec_primary)
  244.     third_transform_output = rot_primary.dot(second_transform_output) + tvec_primary
  245.  
  246.     #Appending output to list
  247.     outputs.append(third_transform_output)
  248.  
  249.     #Printing output for specific rvec, tvec pair -- should be close to (-165,5,0)
  250.     print third_transform_output
  251.  
  252. #Averaging final output:
  253. final_output = 0
  254. for output in outputs:
  255.     final_output = final_output + output
  256. final_output = final_output/len(outputs)
  257.  
  258. print "Final output \n"
  259. print final_output
  260.  
  261.  
  262. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement