|
| 1 | +# System information: |
| 2 | +# - Linux Mint 18.1 Cinnamon 64-bit |
| 3 | +# - Python 2.7 with OpenCV 3.2.0 |
| 4 | + |
| 5 | +import numpy |
| 6 | +import cv2 |
| 7 | +from cv2 import aruco |
| 8 | +import pickle |
| 9 | +import glob |
| 10 | + |
| 11 | + |
| 12 | +# ChAruco board variables |
| 13 | +CHARUCOBOARD_ROWCOUNT = 7 |
| 14 | +CHARUCOBOARD_COLCOUNT = 5 |
| 15 | +ARUCO_DICT = aruco.Dictionary_get(aruco.DICT_5X5_1000) |
| 16 | + |
| 17 | +# Create constants to be passed into OpenCV and Aruco methods |
| 18 | +CHARUCO_BOARD = aruco.CharucoBoard_create( |
| 19 | + squaresX=CHARUCOBOARD_COLCOUNT, |
| 20 | + squaresY=CHARUCOBOARD_ROWCOUNT, |
| 21 | + squareLength=0.04, |
| 22 | + markerLength=0.02, |
| 23 | + dictionary=ARUCO_DICT) |
| 24 | + |
| 25 | +# Create the arrays and variables we'll use to store info like corners and IDs from images processed |
| 26 | +corners_all = [] # Corners discovered in all images processed |
| 27 | +ids_all = [] # Aruco ids corresponding to corners discovered |
| 28 | +image_size = None # Determined at runtime |
| 29 | + |
| 30 | + |
| 31 | +# This requires a set of images or a video taken with the camera you want to calibrate |
| 32 | +# I'm using a set of images taken with the camera with the naming convention: |
| 33 | +# 'camera-pic-of-charucoboard-<NUMBER>.jpg' |
| 34 | +# All images used should be the same size, which if taken with the same camera shouldn't be a problem |
| 35 | +images = glob.glob('./pictures/*.jpg') |
| 36 | + |
| 37 | + |
| 38 | +# Loop through images glob'ed |
| 39 | +for iname in images: |
| 40 | + # Open the image |
| 41 | + # ret, img = cap.read() |
| 42 | + |
| 43 | + img = cv2.imread(iname) |
| 44 | + |
| 45 | + # img = cv2.imread(iname) |
| 46 | + cv2.imshow('Charuco board', img) |
| 47 | + |
| 48 | + # Grayscale the image |
| 49 | + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| 50 | + |
| 51 | + # Find aruco markers in the query image |
| 52 | + corners, ids, _ = aruco.detectMarkers( |
| 53 | + image=gray, |
| 54 | + dictionary=ARUCO_DICT) |
| 55 | + |
| 56 | + # Outline the aruco markers found in our query image |
| 57 | + img = aruco.drawDetectedMarkers( |
| 58 | + image=img, |
| 59 | + corners=corners) |
| 60 | + |
| 61 | + # Get charuco corners and ids from detected aruco markers |
| 62 | + response, charuco_corners, charuco_ids = aruco.interpolateCornersCharuco( |
| 63 | + markerCorners=corners, |
| 64 | + markerIds=ids, |
| 65 | + image=gray, |
| 66 | + board=CHARUCO_BOARD) |
| 67 | + |
| 68 | + # If a Charuco board was found, let's collect image/corner points |
| 69 | + # Requiring at least 20 squares |
| 70 | + if response > 20: |
| 71 | + # Add these corners and ids to our calibration arrays |
| 72 | + corners_all.append(charuco_corners) |
| 73 | + ids_all.append(charuco_ids) |
| 74 | + |
| 75 | + # Draw the Charuco board we've detected to show our calibrator the board was properly detected |
| 76 | + img = aruco.drawDetectedCornersCharuco( |
| 77 | + image=img, |
| 78 | + charucoCorners=charuco_corners, |
| 79 | + charucoIds=charuco_ids) |
| 80 | + |
| 81 | + # If our image size is unknown, set it now |
| 82 | + if not image_size: |
| 83 | + image_size = gray.shape[::-1] |
| 84 | + |
| 85 | + # Reproportion the image, maxing width or height at 1000 |
| 86 | + proportion = max(img.shape) / 1000.0 |
| 87 | + img = cv2.resize(img, (int(img.shape[1]/proportion), int(img.shape[0]/proportion))) |
| 88 | + # Pause to display each image, waiting for key press |
| 89 | + cv2.imshow('Charuco board', img) |
| 90 | + if cv2.waitKey(0) == ord('q'): |
| 91 | + break |
| 92 | + else: |
| 93 | + print("Not able to detect a charuco board in image: {}".format(iname)) |
| 94 | + |
| 95 | +# Destroy any open CV windows |
| 96 | +cv2.destroyAllWindows() |
| 97 | + |
| 98 | +# Make sure at least one image was found |
| 99 | +if len(images) < 1: |
| 100 | + # Calibration failed because there were no images, warn the user |
| 101 | + print("Calibration was unsuccessful. No images of charucoboards were found. Add images of charucoboards and use or alter the naming conventions used in this file.") |
| 102 | + # Exit for failure |
| 103 | + exit() |
| 104 | + |
| 105 | +# Make sure we were able to calibrate on at least one charucoboard by checking |
| 106 | +# if we ever determined the image size |
| 107 | +if not image_size: |
| 108 | + # Calibration failed because we didn't see any charucoboards of the PatternSize used |
| 109 | + print("Calibration was unsuccessful. We couldn't detect charucoboards in any of the images supplied. Try changing the patternSize passed into Charucoboard_create(), or try different pictures of charucoboards.") |
| 110 | + # Exit for failure |
| 111 | + exit() |
| 112 | + |
| 113 | +# Now that we've seen all of our images, perform the camera calibration |
| 114 | +# based on the set of points we've discovered |
| 115 | +calibration, cameraMatrix, distCoeffs, rvecs, tvecs = aruco.calibrateCameraCharuco( |
| 116 | + charucoCorners=corners_all, |
| 117 | + charucoIds=ids_all, |
| 118 | + board=CHARUCO_BOARD, |
| 119 | + imageSize=image_size, |
| 120 | + cameraMatrix=None, |
| 121 | + distCoeffs=None) |
| 122 | + |
| 123 | +# Print matrix and distortion coefficient to the console |
| 124 | +print(cameraMatrix) |
| 125 | +print(distCoeffs) |
| 126 | + |
| 127 | +# Save values to be used where matrix+dist is required, for instance for posture estimation |
| 128 | +# I save files in a pickle file, but you can use yaml or whatever works for you |
| 129 | +f = open('calibration.pckl', 'wb') |
| 130 | +pickle.dump((cameraMatrix, distCoeffs, rvecs, tvecs), f) |
| 131 | +f.close() |
| 132 | + |
| 133 | +# Print to console our success |
| 134 | +print('Calibration successful. Calibration file used: {}'.format('calibration.pckl')) |
0 commit comments