You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0KB

  1. # 标定并校正
  2. import cv2
  3. import numpy as np
  4. import glob
  5. # Defining the dimensions of checkerboard
  6. CHECKERBOARD = (4, 4)
  7. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  8. # Creating vector to store vectors of 3D points for each checkerboard image
  9. objpoints = []
  10. # Creating vector to store vectors of 2D points for each checkerboard image
  11. imgpoints = []
  12. # Defining the world coordinates for 3D points
  13. objp = np.zeros((1, CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
  14. objp[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
  15. prev_img_shape = None
  16. # Extracting path of individual image stored in a given directory
  17. images = glob.glob('./images/*.JPG')
  18. for fname in images:
  19. img = cv2.imread(fname)
  20. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  21. # Find the chess board corners
  22. # If desired number of corners are found in the image then ret = true
  23. ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD,
  24. cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)
  25. """
  26. If desired number of corner are detected,
  27. we refine the pixel coordinates and display
  28. them on the images of checker board
  29. """
  30. if ret == True:
  31. objpoints.append(objp)
  32. # refining pixel coordinates for given 2d points.
  33. corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
  34. imgpoints.append(corners2)
  35. # Draw and display the corners
  36. img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)
  37. cv2.imwrite("./save/checkerboardCallbration.jpg", img)
  38. h, w = img.shape[:2]
  39. """
  40. Performing camera calibration by
  41. passing the value of known 3D points (objpoints)
  42. and corresponding pixel coordinates of the
  43. detected corners (imgpoints)
  44. """
  45. ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
  46. mtx = mtx.astype(float)
  47. dist = dist.astype(float)
  48. print("Camera matrix : \n") # 内参矩阵
  49. print(mtx)
  50. print("dist : \n") # 畸变系数
  51. print(dist)
  52. print("rvecs : \n") # 旋转向量
  53. print(rvecs)
  54. print("tvecs : \n") # 平移向量
  55. print(tvecs)
  56. print("-----------------------------------------------------")
  57. # 畸变校正
  58. img = cv2.imread(r"./jiaoZheng/uav.JPG")
  59. print("------------------使用undistort函数-------------------")
  60. dst = cv2.undistort(img, mtx, dist, None, mtx)
  61. print("dst的大小为:", dst.shape)
  62. cv2.imwrite("./save/uav-dst-0418.JPG", dst)
  63. print("-------------------计算反向投影误差-----------------------") # 利用反向投影误差对我们找到的参数的准确性进行估计,得到的结果越接近 0 越好。
  64. tot_error = 0
  65. for i in range(len(objpoints)):
  66. img_points2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
  67. error = cv2.norm(imgpoints[i], img_points2, cv2.NORM_L2) / len(img_points2)
  68. print("error of " + str(i) + ": ", error)
  69. tot_error += error
  70. mean_error = tot_error / len(objpoints)
  71. print("total error: ", tot_error)
  72. print("mean error: ", mean_error)