Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import glob
  4. import pprint
  5. import re
  6.  
  7. cm1 = np.loadtxt('calibrationdata\\leftcamera\\leftcameramatrix.txt')
  8. cm2 = np.loadtxt('calibrationdata\\rightcamera\\rightcameramatrix.txt')
  9. dc1 = np.loadtxt('calibrationdata\\leftcamera\\leftcameradistcoeffs.txt')
  10. dc2 = np.loadtxt('calibrationdata\\rightcamera\\rightcameradistcoeffs.txt')
  11.  
  12. # termination criteria
  13. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
  14.  
  15. # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
  16. objp = np.zeros((6*9,3), np.float32)
  17. objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
  18.  
  19. # Arrays to store object points and image points from all the images.
  20. objpoints = [] # 3d point in real world space
  21. imgpointsL = [] # 2d points in image plane.
  22. imgpointsR = [] # 2d points in image plane.
  23.  
  24. leftImages = glob.glob('stereocalib\left\*.jpg')
  25.  
  26. for fnameL in leftImages:
  27.     fnameR = re.sub('left', 'right', fnameL)
  28.     imgL = cv2.imread(fnameL)
  29.     imgR = cv2.imread(fnameR)
  30.     height, width, depth  = imgL.shape
  31.  
  32.     grayL = cv2.cvtColor(imgL,cv2.COLOR_BGR2GRAY)
  33.     grayR = cv2.cvtColor(imgR,cv2.COLOR_BGR2GRAY)
  34.  
  35.     # Find the chess board corners
  36.     retL, cornersL = cv2.findChessboardCorners(grayL, (9,6),None)
  37.     retR, cornersR = cv2.findChessboardCorners(grayR, (9,6),None)
  38.  
  39.     # If found, add object points, image points (after refining them)
  40.     if retL == True and retR == True:
  41.         objpoints.append(objp)
  42.  
  43.         corners2L = cv2.cornerSubPix(grayL, cornersL, (11,11), (-1,-1), criteria)
  44.         corners2R = cv2.cornerSubPix(grayR, cornersR, (11,11), (-1,-1), criteria)
  45.         imgpointsL.append(corners2L)
  46.         imgpointsR.append(corners2R)
  47.  
  48.         # Draw and display the corners
  49.         imgL = cv2.drawChessboardCorners(imgL, (9,6), corners2L, retL)
  50.         imgR = cv2.drawChessboardCorners(imgR, (9,6), corners2R, retR)
  51.         cv2.imshow('ORIGINAL LEFT CAMERA: ', imgL)
  52.         cv2.imshow('ORIGINAL RIGHT CAMERA: ', imgR)
  53.         cv2.waitKey(100)
  54.  
  55. cv2.destroyAllWindows()
  56.  
  57. # retVal = returned value, cm1 = Camera Matrix 1
  58. # dc1 = Distortion Coefficients matrix 1, cm2 = Camera Matrix 2
  59. # dc2 = Distortion Coefficients matrix 2
  60. # r = rotation matrix, t = translation vector
  61. # e = essential matrix, f = fundamental matrix
  62. retVal, cm1, dc1, cm2, dc2, r, t, e, f = cv2.stereoCalibrate(objpoints, imgpointsL, imgpointsR, cm1, dc1, cm2, dc2, (width, height), None, None, cv2.CALIB_FIX_INTRINSIC, criteria)
  63.  
  64. print "return value"
  65. pprint.pprint(retVal)
  66. print "\nCamera Mat1"
  67. pprint.pprint(cm1)
  68. print "\nDist Coeffs1"
  69. pprint.pprint(dc1)
  70. print "\nCamera Mat2"
  71. pprint.pprint(cm2)
  72. print "\nDist Coeffs2"
  73. pprint.pprint(dc2)
  74. print "\nRotation"
  75. pprint.pprint(r)
  76. print "\nTranslation"
  77. pprint.pprint(t)
  78. print "\nEssential Mat"
  79. pprint.pprint(e)
  80. print "\nFundamental Mat"
  81. pprint.pprint(f)
  82.  
  83. np.savetxt('calibrationdata\\cameramatrix1.txt', cm1);
  84. np.savetxt('calibrationdata\\cameramatrix2.txt', cm2);
  85. np.savetxt('calibrationdata\\distcoeffs1.txt', dc1);
  86. np.savetxt('calibrationdata\\distcoeffs2.txt', dc2);
  87. np.savetxt('calibrationdata\\rotation.txt', r);
  88. np.savetxt('calibrationdata\\translation.txt', t);
  89. np.savetxt('calibrationdata\\essentialmat.txt', e);
  90. np.savetxt('calibrationdata\\fundamentalmat.txt', f);
  91.  
  92. print "Calibration done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement