-
This is an amazing project! Thanks for sharing. I have a question regarding the camera calibration. How did you calculate the intrinsic matrix, distortion and rotation. Did you follow this: https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html or did you write your own code? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hi, thanks for the kind words! I did follow that tutorial, and then put the intrinsic matrix and distortion parameters in the file camera-params.json. This is also where you define the number of cameras in your setup. |
Beta Was this translation helpful? Give feedback.
-
Hi, I want to add a few my notes about calibration, because that point is not obvious especially for new ppl in that field I made/adapted two raw scripts for that process based on next sources:
A note: I have used custom M12 lenses with focal length of 2.8 mm so I definitely needed these numbers.
from pseyepy import Camera, Display
import cv2 # OpenCV for image handling
import time
cam_index = 1
# Initialize camera
# cams = Camera(fps=60, resolution=Camera.RES_LARGE) # wrong camera mode
cams = Camera(fps=90, resolution=Camera.RES_SMALL) # camera mode which is used in main mocap logic
for i in range(30): # Capture 10 images
frame, timestamp = cams.read()
filename = f'cam_{cam_index}/image_{i}.jpg'
cv2.imwrite(filename, frame)
time.sleep(0.5) # Wait a second between captures
# Clean up
cams.end()
#!/usr/bin/env python
import cv2
import numpy as np
import os
import glob
cam_images_folder_name = 'cam_1'
cam_images_folder_name_calibrated = f'{cam_images_folder_name}_c'
# cam_images_folder_name = 'cam_1'
# Defining the dimensions of checkerboard
# CHECKERBOARD = (6,9)
CHECKERBOARD = (5,6)
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
# Extracting path of individual image stored in a given directory
images = glob.glob(f'./{cam_images_folder_name}/*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 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)
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH + 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)
new_frame_name = cam_images_folder_name_calibrated + '/' + os.path.basename(fname)
# print(new_frame_name)
cv2.imwrite(new_frame_name, img)
# 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, gray.shape[::-1], None, None)
print("Camera matrix : \n")
print(mtx)
print("dist : \n")
print(dist)
I wasn't able to get reproducible numbers of camera parameters, they are always fluctuating even if collections of samples are ok.
Anyway I took the most "middle" result on just my eye and camera pose calibration algorithm worked after that! Also I assume that it is better to have IR filter while doing calibration, because the image are stuffed with IR light and it's to grey which makes a bad contrast of black color, which lead to issues with chessboard. Any thoughts about that? |
Beta Was this translation helpful? Give feedback.
-
Hi @gumby0q, Thanks for sharing your notes and code for calibration. I'm currently trying to calibrate my cameras, but I'm having trouble downloading the chessboard you shared. I used a different one, but I'm unsure if the square size in mm matters for the calibration process. Here it says: This explanation is a bit confusing for me, and right now I can't get a correct camera pose with the chessboard I am using (5x6 inner corners and 34mm each square). Could you please clarify if the square size is important and, if so, what size I should be using? Additionally, I used your code to write another script that automates the task a bit more. You can find the code here. Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Hi,
that's really magnificent project :)
I want to add a few my notes about calibration, because that point is not obvious especially for new ppl in that field
I made/adapted two raw scripts for that process based on next sources:
A note: I have used custom M12 lenses with focal length of 2.8 mm so I definitely needed these numbers.