Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4.  
  5. chessCell = 0.048
  6. cameraFocalLength = 0.014
  7. cameraMatrixWidth = 0.036
  8. cameraMatrixHeight = 0.024
  9. holoFrameWidth = 1408
  10. holoFrameHeight = 792
  11. gridCellsX = 6
  12. gridCellsY = 4
  13.  
  14. print('--- Finding chessboards in images ---')
  15.  
  16. cameraCorners, holoCorners = [], []
  17.  
  18. for s in os.listdir('.'):
  19. if s[-7:] == 'cam.png':
  20. image = cv2.imread(s)
  21. image = cv2.resize(image, (holoFrameWidth, holoFrameHeight))
  22. found, corners = cv2.findChessboardCorners(image, (gridCellsX - 1, gridCellsY - 1))
  23. if not found:
  24. print(s, ': failed to find chessboard corners', sep='')
  25. continue
  26. number = int(s[:s.find('_')])
  27. cameraCorners.append((number, np.concatenate(corners)))
  28. elif s[-8:] == 'holo.jpg':
  29. image = cv2.imread(s)
  30. image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  31. found, corners = cv2.findChessboardCorners(image, (gridCellsX - 1, gridCellsY - 1))
  32. if not found:
  33. print(s, ': failed to find chessboard corners', sep='')
  34. continue
  35. number = int(s[:s.find('_')])
  36. holoCorners.append((number, np.concatenate(corners)))
  37.  
  38. print('Using', len(cameraCorners), 'DSLR images and', len(holoCorners), 'HoloLens images')
  39.  
  40. objectPoints = [[[chessCell * j, chessCell * i, 0] for j in range(gridCellsX - 1)] for i in range(gridCellsY - 1)]
  41. objectPoints = np.concatenate(objectPoints)
  42.  
  43. print()
  44. print('--- Calibrating DSLR ---')
  45.  
  46. cameraFocalLengthPix = cameraFocalLength * min(holoFrameWidth / cameraMatrixWidth, holoFrameHeight / cameraMatrixHeight)
  47. cameraMatrix = np.array([
  48. [cameraFocalLengthPix, 0, holoFrameWidth / 2],
  49. [0, cameraFocalLengthPix, holoFrameHeight / 2],
  50. [0, 0, 1]
  51. ])
  52.  
  53. cameraCalib = cv2.calibrateCamera([objectPoints.astype('float32')] * len(cameraCorners), [s[1].astype('float32') for s in cameraCorners], (holoFrameWidth, holoFrameHeight), cameraMatrix, None, flags=cv2.CALIB_USE_INTRINSIC_GUESS)
  54.  
  55. print('Camera RMS:', cameraCalib[0])
  56. print('Camera matrix:')
  57. print(cameraCalib[1])
  58. print('Camera distortion coefficients:')
  59. print(cameraCalib[2])
  60.  
  61. cameraValues = cv2.calibrationMatrixValues(cameraCalib[1], (holoFrameWidth, holoFrameHeight), 0, 0)
  62.  
  63. print('Camera field of view:', cameraValues[:2])
  64.  
  65. print()
  66. print('--- Calibrating HoloLens ---')
  67.  
  68. holoCalib = cv2.calibrateCamera([objectPoints.astype('float32')] * len(holoCorners), [s[1].astype('float32') for s in holoCorners], (holoFrameWidth, holoFrameHeight), None, None)
  69.  
  70. print('Holo RMS:', holoCalib[0])
  71. print('Holo matrix:')
  72. print(holoCalib[1])
  73. print('Holo distortion coefficients:')
  74. print(holoCalib[2])
  75.  
  76. holoValues = cv2.calibrationMatrixValues(holoCalib[1], (holoFrameWidth, holoFrameHeight), 0, 0)
  77.  
  78. print('HoloLens field of view:', holoValues[:2])
  79.  
  80. print()
  81. print('--- Stereo calibrating ---')
  82.  
  83. cameraCorners = {number: corners for number, corners in cameraCorners}
  84. holoCorners = {number: corners for number, corners in holoCorners}
  85.  
  86. commonIndices = list(set(cameraCorners) & set(holoCorners))
  87.  
  88. print('Using', len(commonIndices), 'common images')
  89.  
  90. stereoCalib = cv2.stereoCalibrate([objectPoints.astype('float32')] * len(commonIndices), [cameraCorners[i].astype('float32') for i in commonIndices], [holoCorners[i].astype('float32') for i in commonIndices],
  91. cameraCalib[1].astype('float32'), cameraCalib[2].astype('float32'), holoCalib[1].astype('float32'), holoCalib[2].astype('float32'), (holoFrameWidth, holoFrameHeight), flags=cv2.CALIB_FIX_INTRINSIC)
  92.  
  93. print('Stereo RMS:', stereoCalib[0])
  94. print('Translation:', stereoCalib[6].flatten())
  95. print('Rotation in degrees:', cv2.Rodrigues(stereoCalib[5])[0].flatten() * 180 / np.pi)
  96. print('Rotation matrix:')
  97. print(stereoCalib[5])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement