I'm trying to make a barrel normalization using a Charuco board as reference for camera calibration.
The code worked perfectly, but then it started not to work. The resulting image after applying undistort is not flattened.
I tried changing versions of opencv, but none of them seems to work correctly. Anyone having this problem?
Here is the code I'm using to detect the aruco markers, calculate camera calibration and undistort:
def read_chessboards(img):
"""
Charuco base pose estimation.
"""
allCorners = []
allIds = []
decimator = 0
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_7X7_1000)
board = cv.aruco.CharucoBoard_create(53, 34, 10, 8, aruco_dict) #37, 25
# SUB PIXEL CORNER DETECTION CRITERION
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.00001)
frame = cv.imread(img)
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
gray = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY,95,10) #95
corners, ids, rejectedImgPoints = cv.aruco.detectMarkers(gray, aruco_dict)
if len(corners)>0:
# SUB PIXEL DETECTION
for corner in corners:
cv.cornerSubPix(gray, corner,
winSize = (3,3),
zeroZone = (-1,-1),
criteria = criteria)
res2 = cv.aruco.interpolateCornersCharuco(corners,ids,gray,board)
if res2[1] is not None and res2[2] is not None and len(res2[1])>3 and decimator%1==0:
allCorners.append(res2[1])
allIds.append(res2[2])
decimator+=1
imsize = gray.shape
return allCorners,allIds,imsize
def calibrate_camera(allCorners,allIds,imsize):
"""
Calibrates the camera using the dected corners.
"""
print("CAMERA CALIBRATION")
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_7X7_1000)
cameraMatrixInit = np.array([[ 1000., 0., imsize[0]/2.],
[ 0., 1000., imsize[1]/2.],
[ 0., 0., 1.]])
distCoeffsInit = np.zeros((5,1))
#flags = (cv.CALIB_USE_INTRINSIC_GUESS + cv.CALIB_RATIONAL_MODEL + cv.CALIB_FIX_ASPECT_RATIO)
#flags = (cv.CALIB_RATIONAL_MODEL)
flags = None
(ret, camera_matrix, distortion_coefficients0,
rotation_vectors, translation_vectors,
stdDeviationsIntrinsics, stdDeviationsExtrinsics,
perViewErrors) = cv.aruco.calibrateCameraCharucoExtended(
charucoCorners=allCorners,
charucoIds=allIds,
board=cv.aruco.CharucoBoard_create(53, 34, 10, 8, aruco_dict), #53, 34, 10, 8
imageSize=imsize,
cameraMatrix=cameraMatrixInit,
distCoeffs=distCoeffsInit,
flags=flags,
criteria=(cv.TERM_CRITERIA_EPS & cv.TERM_CRITERIA_COUNT, 10000, 1e-9))
newcameramtx, roi = cv.getOptimalNewCameraMatrix(
camera_matrix, distortion_coefficients0, imsize, 0, imsize)
return ret, camera_matrix, distortion_coefficients0, rotation_vectors, translation_vectors, newcameramtx
def undistort(img, mtx, dist, nmtx):
img = cv.undistort(img,
mtx,
dist,
None,
nmtx,)
return img
Here are the images used:
charuco board:
Example image + result: