|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""aruco_detect_board_charuco.py |
| 4 | +Usage example: |
| 5 | +python aruco_detect_board_charuco.py -w=5 -h=7 -sl=0.04 -ml=0.02 -d=10 -c=../data/aruco/tutorial_camera_charuco.yml |
| 6 | + -i=../data/aruco/choriginal.jpg |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import numpy as np |
| 11 | +import cv2 as cv |
| 12 | +import sys |
| 13 | + |
| 14 | + |
| 15 | +def read_camera_parameters(filename): |
| 16 | + fs = cv.FileStorage(cv.samples.findFile(filename, False), cv.FileStorage_READ) |
| 17 | + if fs.isOpened(): |
| 18 | + cam_matrix = fs.getNode("camera_matrix").mat() |
| 19 | + dist_coefficients = fs.getNode("distortion_coefficients").mat() |
| 20 | + return True, cam_matrix, dist_coefficients |
| 21 | + return False, [], [] |
| 22 | + |
| 23 | + |
| 24 | +def main(): |
| 25 | + # parse command line options |
| 26 | + parser = argparse.ArgumentParser(description="detect markers and corners of charuco board, estimate pose of charuco" |
| 27 | + "board", add_help=False) |
| 28 | + parser.add_argument("-H", "--help", help="show help", action="store_true", dest="show_help") |
| 29 | + parser.add_argument("-v", "--video", help="Input from video or image file, if omitted, input comes from camera", |
| 30 | + default="", action="store", dest="v") |
| 31 | + parser.add_argument("-i", "--image", help="Input from image file", default="", action="store", dest="img_path") |
| 32 | + parser.add_argument("-w", help="Number of squares in X direction", default="3", action="store", dest="w", type=int) |
| 33 | + parser.add_argument("-h", help="Number of squares in Y direction", default="3", action="store", dest="h", type=int) |
| 34 | + parser.add_argument("-sl", help="Square side length", default="1.", action="store", dest="sl", type=float) |
| 35 | + parser.add_argument("-ml", help="Marker side length", default="0.5", action="store", dest="ml", type=float) |
| 36 | + parser.add_argument("-d", help="dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2, DICT_4X4_1000=3," |
| 37 | + "DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, DICT_6X6_50=8," |
| 38 | + "DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12, DICT_7X7_100=13," |
| 39 | + "DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}", |
| 40 | + default="0", action="store", dest="d", type=int) |
| 41 | + parser.add_argument("-ci", help="Camera id if input doesnt come from video (-v)", default="0", action="store", |
| 42 | + dest="ci", type=int) |
| 43 | + parser.add_argument("-c", help="Input file with calibrated camera parameters", default="", action="store", |
| 44 | + dest="cam_param") |
| 45 | + |
| 46 | + args = parser.parse_args() |
| 47 | + |
| 48 | + show_help = args.show_help |
| 49 | + if show_help: |
| 50 | + parser.print_help() |
| 51 | + sys.exit() |
| 52 | + width = args.w |
| 53 | + height = args.h |
| 54 | + sqruare_len = args.sl |
| 55 | + marker_len = args.ml |
| 56 | + dict = args.d |
| 57 | + video = args.v |
| 58 | + camera_id = args.ci |
| 59 | + img_path = args.img_path |
| 60 | + |
| 61 | + cam_param = args.cam_param |
| 62 | + cam_matrix = [] |
| 63 | + dist_coefficients = [] |
| 64 | + if cam_param != "": |
| 65 | + _, cam_matrix, dist_coefficients = read_camera_parameters(cam_param) |
| 66 | + |
| 67 | + aruco_dict = cv.aruco.getPredefinedDictionary(dict) |
| 68 | + board_size = (width, height) |
| 69 | + board = cv.aruco.CharucoBoard(board_size, sqruare_len, marker_len, aruco_dict) |
| 70 | + charuco_detector = cv.aruco.CharucoDetector(board) |
| 71 | + |
| 72 | + image = None |
| 73 | + input_video = None |
| 74 | + wait_time = 10 |
| 75 | + if video != "": |
| 76 | + input_video = cv.VideoCapture(cv.samples.findFileOrKeep(video, False)) |
| 77 | + image = input_video.retrieve()[1] if input_video.grab() else None |
| 78 | + elif img_path == "": |
| 79 | + input_video = cv.VideoCapture(camera_id) |
| 80 | + image = input_video.retrieve()[1] if input_video.grab() else None |
| 81 | + elif img_path != "": |
| 82 | + wait_time = 0 |
| 83 | + image = cv.imread(cv.samples.findFile(img_path, False)) |
| 84 | + |
| 85 | + if image is None: |
| 86 | + print("Error: unable to open video/image source") |
| 87 | + sys.exit(0) |
| 88 | + |
| 89 | + while image is not None: |
| 90 | + image_copy = np.copy(image) |
| 91 | + charuco_corners, charuco_ids, marker_corners, marker_ids = charuco_detector.detectBoard(image) |
| 92 | + if not (marker_ids is None) and len(marker_ids) > 0: |
| 93 | + cv.aruco.drawDetectedMarkers(image_copy, marker_corners) |
| 94 | + if not (charuco_ids is None) and len(charuco_ids) > 0: |
| 95 | + cv.aruco.drawDetectedCornersCharuco(image_copy, charuco_corners, charuco_ids) |
| 96 | + if len(cam_matrix) > 0 and len(charuco_ids) >= 4: |
| 97 | + try: |
| 98 | + obj_points, img_points = board.matchImagePoints(charuco_corners, charuco_ids) |
| 99 | + flag, rvec, tvec = cv.solvePnP(obj_points, img_points, cam_matrix, dist_coefficients) |
| 100 | + if flag: |
| 101 | + cv.drawFrameAxes(image_copy, cam_matrix, dist_coefficients, rvec, tvec, .2) |
| 102 | + except cv.error as error_inst: |
| 103 | + print("SolvePnP recognize calibration pattern as non-planar pattern. To process this need to use " |
| 104 | + "minimum 6 points. The planar pattern may be mistaken for non-planar if the pattern is " |
| 105 | + "deformed or incorrect camera parameters are used.") |
| 106 | + print(error_inst.err) |
| 107 | + cv.imshow("out", image_copy) |
| 108 | + key = cv.waitKey(wait_time) |
| 109 | + if key == 27: |
| 110 | + break |
| 111 | + image = input_video.retrieve()[1] if input_video is not None and input_video.grab() else None |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments