From 0248e76f79d04eff3c24bbac5250406732908490 Mon Sep 17 00:00:00 2001 From: ros Date: Tue, 7 Mar 2023 17:07:43 +0100 Subject: [PATCH] lol --- calib (copy).py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ calib.py | 2 +- calib2.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ findcorners.py | 42 ++++++++++++++++++++++++++++++++++++++++++ test2.py | 14 ++++++++++---- 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 calib (copy).py create mode 100644 calib2.py create mode 100644 findcorners.py diff --git a/calib (copy).py b/calib (copy).py new file mode 100644 index 0000000..9004fbd --- /dev/null +++ b/calib (copy).py @@ -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) + +"" diff --git a/calib.py b/calib.py index 8f0fa82..494ef15 100644 --- a/calib.py +++ b/calib.py @@ -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) diff --git a/calib2.py b/calib2.py new file mode 100644 index 0000000..5da0428 --- /dev/null +++ b/calib2.py @@ -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) + +"" diff --git a/findcorners.py b/findcorners.py new file mode 100644 index 0000000..a627646 --- /dev/null +++ b/findcorners.py @@ -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() diff --git a/test2.py b/test2.py index fbc4426..457a8b8 100644 --- a/test2.py +++ b/test2.py @@ -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()