Merge pull request #23587 from vovka643:4.x_aruco_calibrate_py

Added charuco pattern into calibrate.py #23587

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Vladimir Ponomarev 2023-05-17 18:30:30 +03:00 committed by GitHub
parent eefee8574a
commit d2618bfe11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,11 +5,21 @@ camera calibration for distorted images with chess board samples
reads distorted images, calculates the calibration and write undistorted images
usage:
calibrate.py [--debug <output path>] [--square_size] [<image mask>]
calibrate.py [--debug <output path>] [-w <width>] [-h <height>] [-t <pattern type>] [--square_size=<square size>]
[--marker_size=<aruco marker size>] [--aruco_dict=<aruco dictionary name>] [<image mask>]
usage example:
calibrate.py -w 4 -h 6 -t chessboard --square_size=50 ../data/left*.jpg
default values:
--debug: ./output/
--square_size: 1.0
-w: 4
-h: 6
-t: chessboard
--square_size: 50
--marker_size: 25
--aruco_dict: DICT_4X4_50
--threads: 4
<image mask> defaults to ../data/left*.jpg
'''
@ -30,31 +40,81 @@ def main():
import getopt
from glob import glob
args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size=', 'threads='])
args, img_names = getopt.getopt(sys.argv[1:], 'w:h:t:', ['debug=','square_size=', 'marker_size=',
'aruco_dict=', 'threads=', ])
args = dict(args)
args.setdefault('--debug', './output/')
args.setdefault('--square_size', 1.0)
args.setdefault('-w', 4)
args.setdefault('-h', 6)
args.setdefault('-t', 'chessboard')
args.setdefault('--square_size', 10)
args.setdefault('--marker_size', 5)
args.setdefault('--aruco_dict', 'DICT_4X4_50')
args.setdefault('--threads', 4)
if not img_mask:
img_mask = '../data/left??.jpg' # default
else:
img_mask = img_mask[0]
img_names = glob(img_mask)
if not img_names:
img_mask = '../data/left??.jpg' # default
img_names = glob(img_mask)
debug_dir = args.get('--debug')
if debug_dir and not os.path.isdir(debug_dir):
os.mkdir(debug_dir)
square_size = float(args.get('--square_size'))
pattern_size = (9, 6)
pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
pattern_points *= square_size
height = int(args.get('-h'))
width = int(args.get('-w'))
pattern_type = str(args.get('-t'))
square_size = float(args.get('--square_size'))
marker_size = float(args.get('--marker_size'))
aruco_dict_name = str(args.get('--aruco_dict'))
pattern_size = (height, width)
if pattern_type == 'chessboard':
pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
pattern_points *= square_size
elif pattern_type == 'charucoboard':
pattern_points = np.zeros((np.prod((height-1, width-1)), 3), np.float32)
pattern_points[:, :2] = np.indices((height-1, width-1)).T.reshape(-1, 2)
pattern_points *= square_size
else:
print("unknown pattern")
return None
obj_points = []
img_points = []
h, w = cv.imread(img_names[0], cv.IMREAD_GRAYSCALE).shape[:2] # TODO: use imquery call to retrieve results
aruco_dicts = {
'DICT_4X4_50':cv.aruco.DICT_4X4_50,
'DICT_4X4_100':cv.aruco.DICT_4X4_100,
'DICT_4X4_250':cv.aruco.DICT_4X4_250,
'DICT_4X4_1000':cv.aruco.DICT_4X4_1000,
'DICT_5X5_50':cv.aruco.DICT_5X5_50,
'DICT_5X5_100':cv.aruco.DICT_5X5_100,
'DICT_5X5_250':cv.aruco.DICT_5X5_250,
'DICT_5X5_1000':cv.aruco.DICT_5X5_1000,
'DICT_6X6_50':cv.aruco.DICT_6X6_50,
'DICT_6X6_100':cv.aruco.DICT_6X6_100,
'DICT_6X6_250':cv.aruco.DICT_6X6_250,
'DICT_6X6_1000':cv.aruco.DICT_6X6_1000,
'DICT_7X7_50':cv.aruco.DICT_7X7_50,
'DICT_7X7_100':cv.aruco.DICT_7X7_100,
'DICT_7X7_250':cv.aruco.DICT_7X7_250,
'DICT_7X7_1000':cv.aruco.DICT_7X7_1000,
'DICT_ARUCO_ORIGINAL':cv.aruco.DICT_ARUCO_ORIGINAL,
'DICT_APRILTAG_16h5':cv.aruco.DICT_APRILTAG_16h5,
'DICT_APRILTAG_25h9':cv.aruco.DICT_APRILTAG_25h9,
'DICT_APRILTAG_36h10':cv.aruco.DICT_APRILTAG_36h10,
'DICT_APRILTAG_36h11':cv.aruco.DICT_APRILTAG_36h11
}
if (aruco_dict_name not in set(aruco_dicts.keys())):
print("unknown aruco dictionary name")
return None
aruco_dict = cv.aruco.getPredefinedDictionary(aruco_dicts[aruco_dict_name])
board = cv.aruco.CharucoBoard(pattern_size, square_size, marker_size, aruco_dict)
charuco_detector = cv.aruco.CharucoDetector(board)
def processImage(fn):
print('processing %s... ' % fn)
img = cv.imread(fn, cv.IMREAD_GRAYSCALE)
@ -63,10 +123,20 @@ def main():
return None
assert w == img.shape[1] and h == img.shape[0], ("size: %d x %d ... " % (img.shape[1], img.shape[0]))
found, corners = cv.findChessboardCorners(img, pattern_size)
if found:
term = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1)
cv.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
found = False
corners = 0
if pattern_type == 'chessboard':
found, corners = cv.findChessboardCorners(img, pattern_size)
if found:
term = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1)
cv.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
elif pattern_type == 'charucoboard':
corners, _charucoIds, _markerCorners_svg, _markerIds_svg = charuco_detector.detectBoard(img)
if (len(corners) == (height-1)*(width-1)):
found = True
else:
print("unknown pattern type", pattern_type)
return None
if debug_dir:
vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
@ -76,7 +146,7 @@ def main():
cv.imwrite(outfile, vis)
if not found:
print('chessboard not found')
print('pattern not found')
return None
print(' %s... OK' % fn)