Dear OpenCV enjoyers,
I am trying to solve perspective distortion problem with my acA1300-30um camera (Datasheet).
I followed calibration steps (Calibration tutorial), but still I am unable to correct my images from perspective distortion.
I took 14 pictures with 5x5 chessboard pattern from variaous agles and distances. Here I am for example attaching one of them (limited because I am new member of this forum):
After receiving camera calibration and distortion coefficients, the undistorted images are almost the same as the distorted ones:
Do you have any suggestions on what I should do differently or if there is any significant problem? I used 2 methods to undistort my images, but with no proper results.
Here is the Python code that I used:
import cv2
import numpy as np
import os
CHESSBOARD_SIZE = (5, 5)
CALIBRATION_FOLDER = 'CalibImages'
object_points = []
image_points = []
for filename in os.listdir(CALIBRATION_FOLDER):
image_path = os.path.join(CALIBRATION_FOLDER, filename)
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if image is None or image.size == 0 or image.shape[0] == 0 or image.shape[1] == 0:
print(f"Error: Unable to read or invalid image file {image_path}")
continue
ret, corners = cv2.findChessboardCorners(image, CHESSBOARD_SIZE)
if ret:
print(f"Corners found in image {image_path}")
object_points.append(np.zeros((CHESSBOARD_SIZE[0] * CHESSBOARD_SIZE[1], 3), np.float32))
object_points[-1][:, :2] = np.mgrid[0:CHESSBOARD_SIZE[0], 0:CHESSBOARD_SIZE[1]].T.reshape(-1, 2)
image_points.append(corners.reshape(-1, 2))
else:
print(f"No corners found in image {image_path}")
if len(image_points) == 0:
print("No images with corners found. Calibration cannot proceed.")
else:
retval, camera_matrix, dist_coeffs, rvecs, tvecs =
cv2.calibrateCamera(object_points, image_points, image.shape[::-1], None, None)
np.save('camera_matrix.npy', camera_matrix)
np.save('dist_coeffs.npy', dist_coeffs)
img = cv2.imread('test12.bmp')
distorted_image = cv2.undistort(img, camera_matrix, dist_coeffs)
cv2.imwrite('undistorted_image.jpg', distorted_image)
height, width = img.shape[:2]
mapx, mapy = cv2.initUndistortRectifyMap(camera_matrix, dist_coeffs, None, camera_matrix, (width, height), 5)
undistorted_image = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imwrite('undistorted_imageMAP.jpg', undistorted_image)