This commit is contained in:
Lucas DAVAL-POMMIER 2023-03-07 17:07:43 +01:00
parent e46fa51a66
commit 0248e76f79
5 changed files with 149 additions and 5 deletions

48
calib (copy).py Normal file
View File

@ -0,0 +1,48 @@
#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("/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:
print("yes it is found")
object_points_array.append(object_points)
image_points_array.append(corners)
else
print("not found")
"""
# 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

@ -4,7 +4,7 @@ import cv2
import numpy as np
# Define the size of the chessboard
chessboard_size = (22, 16)
chessboard_size = (21, 15)
# Define the object points of the chessboard
object_points = np.zeros((np.prod(chessboard_size), 3), dtype=np.float32)

48
calib2.py Normal file
View File

@ -0,0 +1,48 @@
#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("/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:
print("yes it is found")
"object_points_array.append(object_points)
"image_points_array.append(corners)
else
print("not found")
"""
# 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)
""

42
findcorners.py Normal file
View File

@ -0,0 +1,42 @@
import cv2
cam = cv2.VideoCapture(0)
CHECKERBOARD = (5,8)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
frame = cv2.resize(frame, (640,480))
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(frame, CHECKERBOARD)
cv2.imshow("test", frame)
if ret == True:
print(corners)
fnn = cv2.drawChessboardCorners(frame, CHECKERBOARD, corners, ret)
cv2.imshow("result", fnn)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k%256 == 32:
# SPACE pressed
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
cam.release()
cv2.destroyAllWindows()

View File

@ -19,19 +19,23 @@ objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
prev_img_shape = None
h = 480
w = 640
h = 544
w = 960
#print(CHECKERBOARD)
# 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)
cv2.imshow('gray',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)
#print(cv2.CALIB_CB_ADAPTIVE_THRESH)
print(ret)
print(corners)
"""
If desired number of corner are detected,
we refine the pixel coordinates and display
@ -39,6 +43,8 @@ for fname in images:
"""
if ret == True:
objpoints.append(objp)
print("objpoints is : \n")
print(objpoints)
# refining pixel coordinates for given 2d points.
corners2 = cv2.cornerSubPix(gray, corners, (11,11),(-1,-1), criteria)
imgpoints.append(corners2)
@ -46,7 +52,7 @@ for fname in images:
# Draw and display the corners
img = cv2.drawChessboardCorners(img,CHECKERBOARD,corners2,ret)
cv2.imshow('img',img)
#cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()