目录
枕形畸变和去枕形畸变
python
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 创建一个带网格的原始图像
def create_grid(image_size=512, grid_size=20):
image = np.zeros((image_size, image_size, 3), dtype=np.uint8)
for i in range(0, image_size, grid_size):
cv2.line(image, (i, 0), (i, image_size), (255, 255, 255), 1)
cv2.line(image, (0, i), (image_size, i), (255, 255, 255), 1)
return image
# 应用枕形畸变
def apply_pincushion_distortion(image, k1=0.05):
image_size = image.shape[0]
fx, fy = image_size * 1.0, image_size * 1.0
cx, cy = image_size / 2, image_size / 2
camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
dist_coeffs = np.array([k1, 0, 0, 0, 0]) # 正值增加枕形畸变
h, w = image.shape[:2]
new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coeffs, (w, h), 1)
distorted_image = cv2.undistort(image, camera_matrix, dist_coeffs, None, new_camera_matrix)
return distorted_image
# 去除枕形畸变
def remove_pincushion_distortion(image, k1=0.0005):
image_size = image.shape[0]
fx, fy = image_size * 1.0, image_size * 1.0
cx, cy = image_size / 2, image_size / 2
camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
dist_coeffs = np.array([-k1, 0, 0, 0, 0]) # 负值去除枕形畸变
h, w = image.shape[:2]
new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coeffs, (w, h), 1)
undistorted_image = cv2.undistort(image, camera_matrix, dist_coeffs, None, new_camera_matrix)
return undistorted_image
# 主函数
if __name__ == "__main__":
original_grid = create_grid()
distorted_grid = apply_pincushion_distortion(original_grid,k1=-0.2)
undistorted_grid = remove_pincushion_distortion(distorted_grid,k1=-0.2-0.2)
# 使用 OpenCV 展示图像
cv2.imshow('Original Image', original_grid)
cv2.imshow('Pincushion Distorted Image', distorted_grid)
cv2.imshow('Undistorted Image', undistorted_grid)
cv2.waitKey(0)
cv2.destroyAllWindows()
去枕形畸变失败
python
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 创建一个带有枕形畸变的网格图像
def create_pincushion_distorted_grid(image_size=512, grid_size=20):
image = np.zeros((image_size, image_size, 3), dtype=np.uint8)
for i in range(0, image_size, grid_size):
cv2.line(image, (i, 0), (i, image_size), (255, 255, 255), 1)
cv2.line(image, (0, i), (image_size, i), (255, 255, 255), 1)
k1, k2, p1, p2, k3 = -0.2, 0, 0, 0, 0 # 负数制造枕形畸变
fx, fy = image_size, image_size
cx, cy = image_size / 2, image_size / 2
camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
dist_coeffs = np.array([k1, k2, p1, p2, k3])
map1, map2 = cv2.initUndistortRectifyMap(camera_matrix, dist_coeffs, None, camera_matrix, (image_size, image_size), 5)
distorted_image = cv2.remap(image, map1, map2, cv2.INTER_LINEAR)
return image, distorted_image, camera_matrix, dist_coeffs
# 去除枕形畸变
def undistort_image(image, camera_matrix, dist_coeffs):
h, w = image.shape[:2]
new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coeffs, (w, h), 1, (w, h))
undistorted_image = cv2.undistort(image, camera_matrix, dist_coeffs, None, new_camera_matrix)
return undistorted_image
# 主函数
if __name__ == "__main__":
original_grid, distorted_grid, camera_matrix, dist_coeffs = create_pincushion_distorted_grid()
# 手动生成去畸变的映射
map1, map2 = cv2.initUndistortRectifyMap(camera_matrix, dist_coeffs, None, camera_matrix, (original_grid.shape[1], original_grid.shape[0]), cv2.CV_32FC1)
undistorted_grid = cv2.remap(distorted_grid, map1, map2, cv2.INTER_LINEAR)
# 使用 OpenCV 展示图像
cv2.imshow('Original Image', original_grid)
cv2.imshow('Distorted Image', distorted_grid)
cv2.imshow('Undistorted Image', undistorted_grid)
cv2.waitKey(0)
cv2.destroyAllWindows()