ca_ur5/calib.py

44 lines
1.5 KiB
Python

#retval, corners = cv2.findChessboardCorners(image,patternSize, flags)
import cv2
import numpy as np
# Define the size of the chessboard
chessboard_size = (21, 15)
# Define the object points of the chessboard
object_points = np.zeros((np.prod(chessboard_size), 3), dtype=np.float32)
object_points[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2)
# Create arrays to store the object points and image points from all the images
object_points_array = []
image_points_array = []
# Load the images
images = []
images.append(cv2.imread("/home/ros/Bureau/ca_ur5/left.jpg"))
images.append(cv2.imread("/home/ros/Bureau/ca_ur5/left2.jpg"))
# Add more images as needed
# Loop through each image and find the chessboard corners
for image in images:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
found, corners = cv2.findChessboardCorners(gray, chessboard_size, None)
# If the corners are found, add the object points and image points to the arrays
if found:
object_points_array.append(object_points)
image_points_array.append(corners)
# Calibrate the camera using the object points and image points
ret, camera_matrix, distortion_coefficients, rotation_vectors, translation_vectors = cv2.calibrateCamera(
object_points_array, image_points_array, gray.shape[::-1], None, None)
# Print the camera matrix and distortion coefficients
print("Camera matrix:")
print(camera_matrix)
print("Distortion coefficients:")
print(distortion_coefficients)