Reputation: 9
I want to do camera calibration using a chessboard
. And to do that I have developed code based on some corner detection scripts I found on internet.
The issue I am facing is that the my script is able to detect corners on a chessboard
whose image has been taken by a camera (let's call it camera 1). But the same script is unable to detect corners on other chessboards whose images has been taken with two other cameras (let's say camera 2 and camera 3).
I've tried many things to understand and solve the issue, but all my attempts have failed. For example, I've tried the script on the same image while varying the my specified chessboard size but this attempt failed. I've also tried to take images a different period of the day (because I was wondering if ambient luminosity affect the detection) but with my failed tests I guess the issue was not there.
What else can I try?
I attach to this description one image of chessboard from each camera.
Here is my code:
import numpy as np
import cv2 as cv
import glob
import pickle
import os
chessboardSize = (50,15) # Nombre de coins dans chaque direction du damier
# Critère d'arrêt des itérations de recherches de coins
# 30 itérations max ou erreur entre 2 itérations < 0.001
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
threedpoints = [] # Vecteur de points 3D
twodpoints = [] # Vecteur de points 2D
objectp3d = np.zeros((1, chessboardSize[0]*chessboardSize[1], 3), np.float32)
objectp3d[0, :, :2] = np.mgrid[:chessboardSize[0], :chessboardSize[1]].T.reshape(-1,
2)
path = "Camera2.jpeg"
image = cv.imread(path)
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# Si nombre de coin attendu est trouvé alors return cornersFound = True
cornersFound, corners = cv.findChessboardCorners(img_gray, chessboardSize, None)
# If found, add object points, image points (after refining them)
print(cornersFound)
if cornersFound == True:
threedpoints.append(objectp3d)
corners2 = cv.cornerSubPix(img_gray,corners, (11,11), (-1,-1), criteria)
twodpoints.append(corners2)
# Draw and display the corners
for corner in corners2:
x, y = corner.ravel()
cv.circle(image, (int(x), int(y)), 4, (0, 0, 255), -1)
####################### Nomenclature de l'image avec coins détectés
#########################################################
directory, file = os.path.split(path)
base_name, _ = os.path.splitext(file)
new_format = ".jpg"
coin_name = f"{base_name}_Corners{new_format}"
cv.imshow('img', image)
cv.waitKey(0)
cv.imwrite(coin_name, image)
cv.destroyAllWindows()
ret, cameraMatrix, dist, rvecs, tvecs = cv.calibrateCamera(threedpoints, twodpoints,
img_gray.shape[::-1], None, None)
print("\nCamera Calibrated: ", ret) # ret est l'erreur de calibration
print("\nCamera Matrix:\n", cameraMatrix)
print("\nDistorsion Coefficients:\n", dist)
# print("\nRotation Vectors:\n", rvecs)
# print("\nTranslation Vectors:\n", tvecs)
Upvotes: -2
Views: 55