Browse Source

无人机摄像头标定及无人机图像校正-代码交接

master
nyh 5 months ago
commit
e705c3dd7c
14 changed files with 92 additions and 0 deletions
  1. +92
    -0
      MonocularCalibration.py
  2. BIN
      images/DJI_20240326092508_0001_Z.JPG
  3. BIN
      images/DJI_20240326092510_0002_Z.JPG
  4. BIN
      images/DJI_20240326092512_0003_Z.JPG
  5. BIN
      images/DJI_20240326092514_0004_Z.JPG
  6. BIN
      images/DJI_20240326092518_0006_Z.JPG
  7. BIN
      images/DJI_20240326092520_0007_Z.JPG
  8. BIN
      images/DJI_20240326092636_0045_Z.JPG
  9. BIN
      images/DJI_20240326092638_0046_Z.JPG
  10. BIN
      images/DJI_20240326092646_0050_Z.JPG
  11. BIN
      jiaoZheng/satellite.JPG
  12. BIN
      jiaoZheng/uav.JPG
  13. BIN
      save/checkerboardCallbration.jpg
  14. BIN
      save/uav-dst-0418.JPG

+ 92
- 0
MonocularCalibration.py View File

@@ -0,0 +1,92 @@
# 标定并校正

import cv2
import numpy as np
import glob

# Defining the dimensions of checkerboard
CHECKERBOARD = (4, 4)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# Creating vector to store vectors of 3D points for each checkerboard image
objpoints = []
# Creating vector to store vectors of 2D points for each checkerboard image
imgpoints = []

# Defining the world coordinates for 3D points
objp = np.zeros((1, CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
objp[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
prev_img_shape = None

# Extracting path of individual image stored in a given directory
images = glob.glob('./images/*.JPG')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
# If desired number of corners are found in the image then ret = true
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD,
cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)

"""
If desired number of corner are detected,
we refine the pixel coordinates and display
them on the images of checker board
"""
if ret == True:
objpoints.append(objp)
# refining pixel coordinates for given 2d points.
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners2)

# Draw and display the corners
img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)
cv2.imwrite("./save/checkerboardCallbration.jpg", img)
h, w = img.shape[:2]


"""
Performing camera calibration by
passing the value of known 3D points (objpoints)
and corresponding pixel coordinates of the
detected corners (imgpoints)
"""
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
mtx = mtx.astype(float)
dist = dist.astype(float)

print("Camera matrix : \n") # 内参矩阵
print(mtx)
print("dist : \n") # 畸变系数
print(dist)
print("rvecs : \n") # 旋转向量
print(rvecs)
print("tvecs : \n") # 平移向量
print(tvecs)
print("-----------------------------------------------------")

# 畸变校正
img = cv2.imread(r"./jiaoZheng/uav.JPG")

print("------------------使用undistort函数-------------------")
dst = cv2.undistort(img, mtx, dist, None, mtx)
print("dst的大小为:", dst.shape)
cv2.imwrite("./save/uav-dst-0418.JPG", dst)

print("-------------------计算反向投影误差-----------------------") # 利用反向投影误差对我们找到的参数的准确性进行估计,得到的结果越接近 0 越好。
tot_error = 0
for i in range(len(objpoints)):
img_points2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
error = cv2.norm(imgpoints[i], img_points2, cv2.NORM_L2) / len(img_points2)
print("error of " + str(i) + ": ", error)
tot_error += error

mean_error = tot_error / len(objpoints)
print("total error: ", tot_error)
print("mean error: ", mean_error)







BIN
images/DJI_20240326092508_0001_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 13MB

BIN
images/DJI_20240326092510_0002_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 12MB

BIN
images/DJI_20240326092512_0003_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 12MB

BIN
images/DJI_20240326092514_0004_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 12MB

BIN
images/DJI_20240326092518_0006_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 13MB

BIN
images/DJI_20240326092520_0007_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 13MB

BIN
images/DJI_20240326092636_0045_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 12MB

BIN
images/DJI_20240326092638_0046_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 12MB

BIN
images/DJI_20240326092646_0050_Z.JPG View File

Before After
Width: 5184  |  Height: 3888  |  Size: 12MB

BIN
jiaoZheng/satellite.JPG View File

Before After
Width: 4056  |  Height: 3040  |  Size: 7.6MB

BIN
jiaoZheng/uav.JPG View File

Before After
Width: 4056  |  Height: 3040  |  Size: 7.6MB

BIN
save/checkerboardCallbration.jpg View File

Before After
Width: 5184  |  Height: 3888  |  Size: 10MB

BIN
save/uav-dst-0418.JPG View File

Before After
Width: 4056  |  Height: 3040  |  Size: 5.2MB

Loading…
Cancel
Save