Compare commits

...

2 Commits

Author SHA1 Message Date
ros 164ad2f845 Photo 2023-02-28 11:50:49 +01:00
ros 1645f1969c calib 2023-02-28 10:29:14 +01:00
5 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,43 @@
#retval, corners = cv2.findChessboardCorners(image,patternSize, flags)
import cv2
import numpy as np
# Define the size of the chessboard
chessboard_size = (22, 16)
# 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("left.jpg"))
images.append(cv2.imread("right.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)

View File

Before

Width:  |  Height:  |  Size: 164 KiB

After

Width:  |  Height:  |  Size: 164 KiB

BIN
left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

BIN
right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

71
test2.py Normal file
View File

@ -0,0 +1,71 @@
import cv2
import numpy as np
import os
import glob
# Defining the dimensions of checkerboard
CHECKERBOARD = (16,22)
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
h = 480
w = 640
# Extracting path of individual image stored in a given directory
images = glob.glob('/home/ros/Bureau/ca_ur5/*.jpg')
for fname in images:
img = cv2.imread(fname)
h,w = img.shape[:2]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray)
# 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.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#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, (w,h), None, None)
print("Camera matrix : \n")
print(mtx)
print("dist : \n")
print(dist)
print("rvecs : \n")
print(rvecs)
print("tvecs : \n")
print(tvecs)