Skip to content

Instantly share code, notes, and snippets.

@treideme
Created October 25, 2023 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treideme/3920ce85dca6643a0cf901a6ceca1a4f to your computer and use it in GitHub Desktop.
Save treideme/3920ce85dca6643a0cf901a6ceca1a4f to your computer and use it in GitHub Desktop.
Highlight distortion parameter impact in image plane projection
# This gists highlights the impact of various distortion parameters
import numpy as np
import cv2
# Set camera calibration parameters
fx = 8262.158089604814
fy = 8241.805993224913
cx = 2147.360664332489
cy = 897.9466061409038
k1 = -0.33193540220291506
k2 = 5.3113213233505805
k3 = -67.30762636779896
p1 = -0.012499072958136133
p2 = 0.014968462658950266
# Generate a 4K (3840x2160) chessboard image
chessboard_size = (3840, 2160)
chessboard = np.zeros((chessboard_size[1], chessboard_size[0], 3), dtype=np.uint8)
square_size = 100 # Size of each square in pixels
# Create the chessboard pattern to show areas of uneven correction
for i in range(0, chessboard_size[0], square_size):
for j in range(0, chessboard_size[1], square_size):
if (i // square_size + j // square_size) % 2 == 0:
chessboard[j:j+square_size, i:i+square_size, :] = [255, 255, 255]
# Save the chessboard image
cv2.imwrite("chessboard_4K.png", chessboard)
camera_matrix = np.array([[fx, 0, cx],
[0, fy, cy],
[0, 0, 1]])
distortion_coefficients = np.array([k1, k2, p1, p2, k3])
# Undistort the chessboard image
chessboard = cv2.imread("chessboard_4K.png")
undistorted_chessboard = cv2.undistort(chessboard, camera_matrix, distortion_coefficients)
# Save the undistorted image
cv2.imwrite("undistorted_chessboard_4K.png", undistorted_chessboard)
# Generate an overlay with color highlighting differences
alpha = 0.7 # Adjust the alpha value for transparency
beta = 1 - alpha
difference_overlay = cv2.addWeighted(chessboard, alpha, undistorted_chessboard, beta, 0)
cv2.imwrite("diff_4K.png", difference_overlay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment