cameraCalibrationWithUndistortion.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python
  2. import cv2
  3. import numpy as np
  4. import os
  5. import glob
  6. # Defining the dimensions of checkerboard
  7. CHECKERBOARD = (6,9)
  8. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  9. # Creating vector to store vectors of 3D points for each checkerboard image
  10. objpoints = []
  11. # Creating vector to store vectors of 2D points for each checkerboard image
  12. imgpoints = []
  13. # Defining the world coordinates for 3D points
  14. objp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32)
  15. objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
  16. prev_img_shape = None
  17. # Extracting path of individual image stored in a given directory
  18. images = glob.glob('./images/*.jpg')
  19. for fname in images:
  20. img = cv2.imread(fname)
  21. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  22. # Find the chess board corners
  23. # If desired number of corners are found in the image then ret = true
  24. ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+
  25. cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
  26. """
  27. If desired number of corner are detected,
  28. we refine the pixel coordinates and display
  29. them on the images of checker board
  30. """
  31. if ret == True:
  32. objpoints.append(objp)
  33. # refining pixel coordinates for given 2d points.
  34. corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
  35. imgpoints.append(corners2)
  36. # Draw and display the corners
  37. img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2,ret)
  38. cv2.imshow('img',img)
  39. cv2.waitKey(0)
  40. cv2.destroyAllWindows()
  41. h,w = img.shape[:2]
  42. """
  43. Performing camera calibration by
  44. passing the value of known 3D points (objpoints)
  45. and corresponding pixel coordinates of the
  46. detected corners (imgpoints)
  47. """
  48. ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
  49. print("Camera matrix : \n")
  50. print(mtx)
  51. print("dist : \n")
  52. print(dist)
  53. print("rvecs : \n")
  54. print(rvecs)
  55. print("tvecs : \n")
  56. print(tvecs)
  57. # Using the derived camera parameters to undistort the image
  58. img = cv2.imread(images[0])
  59. # Refining the camera matrix using parameters obtained by calibration
  60. newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))
  61. # Method 1 to undistort the image
  62. dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
  63. # Method 2 to undistort the image
  64. mapx,mapy=cv2.initUndistortRectifyMap(mtx,dist,None,newcameramtx,(w,h),5)
  65. dst = cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
  66. # Displaying the undistorted image
  67. cv2.imshow("undistorted image",dst)
  68. cv2.waitKey(0)