0

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:

charuco board

Example image + result:

Example image result

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • looks like your board parameters don't match the physical board. detect all markers individually, draw their IDs on the picture, match up with the _definition_ of how charuco boards are meant to be laid out – Christoph Rackwitz Sep 02 '22 at 11:06
  • you have two instances of `CharucoBoard_create`. watch that. it's a pitfall. they both have the same arguments but maybe not when you edit the code. – Christoph Rackwitz Sep 02 '22 at 11:14
  • @ChristophRackwitz thanks for your comments. I have changed the CharucoBoard_create to be created only one time, and the same error continues. Also, I can detect almost all markers on my charuco board (the ones on the edges are difficult), and the thing is I used the same code, and it worked with the same charuco board, and now is not working... – Wilson Veloz Sep 04 '22 at 09:42
  • did you update opencv versions between it working and not working anymore? someone recently changed a lot of stuff in the aruco module. it's in contrib, less oversight. perhaps they messed something up. – Christoph Rackwitz Sep 04 '22 at 10:25
  • I updated the OpenCV version, however I tried every single other version and the problem continues. – Wilson Veloz Sep 05 '22 at 12:10

0 Answers0