forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request opencv#23363 from vovka643:4.x_generate_charuco
Added charuco board generation to gen_pattern.py opencv#23363 added charuco board generation in gen_pattern.py moved aruco_dict_utils.cpp to samples from opencv_contrib (opencv/opencv_contrib#3464) ### 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 a reference to the original bug report and related work - [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
- Loading branch information
Showing
29 changed files
with
629 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python | ||
|
||
from __future__ import print_function | ||
|
||
import sys | ||
sys.dont_write_bytecode = True # Don't generate .pyc files / __pycache__ directories | ||
|
||
import os | ||
import sys | ||
import unittest | ||
|
||
# Python 3 moved urlopen to urllib.requests | ||
try: | ||
from urllib.request import urlopen | ||
except ImportError: | ||
from urllib import urlopen | ||
|
||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
sys.path.append(os.path.join(os.path.split(basedir)[0], "modules", "python", "test")) | ||
from tests_common import NewOpenCVTests | ||
|
||
def load_tests(loader, tests, pattern): | ||
cwd = os.getcwd() | ||
config_file = 'opencv_apps_python_tests.cfg' | ||
locations = [cwd, basedir] | ||
if os.path.exists(config_file): | ||
with open(config_file, 'r') as f: | ||
locations += [str(s).strip() for s in f.readlines()] | ||
else: | ||
print('WARNING: OpenCV tests config file ({}) is missing, running subset of tests'.format(config_file)) | ||
|
||
tests_pattern = os.environ.get('OPENCV_APPS_TEST_FILTER', 'test_*') + '.py' | ||
if tests_pattern != 'test_*.py': | ||
print('Tests filter: {}'.format(tests_pattern)) | ||
|
||
processed = set() | ||
for l in locations: | ||
if not os.path.isabs(l): | ||
l = os.path.normpath(os.path.join(cwd, l)) | ||
if l in processed: | ||
continue | ||
processed.add(l) | ||
print('Discovering python tests from: {}'.format(l)) | ||
sys_path_modify = l not in sys.path | ||
if sys_path_modify: | ||
sys.path.append(l) # Hack python loader | ||
discovered_tests = loader.discover(l, pattern=tests_pattern, top_level_dir=l) | ||
print(' found {} tests'.format(discovered_tests.countTestCases())) | ||
tests.addTests(loader.discover(l, pattern=tests_pattern)) | ||
if sys_path_modify: | ||
sys.path.remove(l) | ||
return tests | ||
|
||
if __name__ == '__main__': | ||
NewOpenCVTests.bootstrap() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
from __future__ import print_function | ||
|
||
import os, tempfile, numpy as np | ||
|
||
import sys | ||
import cv2 as cv | ||
from tests_common import NewOpenCVTests | ||
import gen_pattern | ||
|
||
class aruco_objdetect_test(NewOpenCVTests): | ||
|
||
def test_aruco_dicts(self): | ||
try: | ||
from svglib.svglib import svg2rlg | ||
from reportlab.graphics import renderPM | ||
except: | ||
raise self.skipTest("libraies svglib and reportlab not found") | ||
else: | ||
cols = 3 | ||
rows = 5 | ||
square_size = 100 | ||
aruco_type = [cv.aruco.DICT_4X4_1000, cv.aruco.DICT_5X5_1000, cv.aruco.DICT_6X6_1000, | ||
cv.aruco.DICT_7X7_1000, cv.aruco.DICT_ARUCO_ORIGINAL, cv.aruco.DICT_APRILTAG_16h5, | ||
cv.aruco.DICT_APRILTAG_25h9, cv.aruco.DICT_APRILTAG_36h10, cv.aruco.DICT_APRILTAG_36h11] | ||
aruco_type_str = ['DICT_4X4_1000','DICT_5X5_1000', 'DICT_6X6_1000', | ||
'DICT_7X7_1000', 'DICT_ARUCO_ORIGINAL', 'DICT_APRILTAG_16h5', | ||
'DICT_APRILTAG_25h9', 'DICT_APRILTAG_36h10', 'DICT_APRILTAG_36h11'] | ||
marker_size = 0.8*square_size | ||
board_width = cols*square_size | ||
board_height = rows*square_size | ||
|
||
for aruco_type_i in range(len(aruco_type)): | ||
#draw desk using opencv | ||
aruco_dict = cv.aruco.getPredefinedDictionary(aruco_type[aruco_type_i]) | ||
board = cv.aruco.CharucoBoard((cols, rows), square_size, marker_size, aruco_dict) | ||
charuco_detector = cv.aruco.CharucoDetector(board) | ||
from_cv_img = board.generateImage((cols*square_size*10, rows*square_size*10)) | ||
|
||
#draw desk using svg | ||
fd1, filesvg = tempfile.mkstemp(prefix="out", suffix=".svg") | ||
os.close(fd1) | ||
fd2, filepng = tempfile.mkstemp(prefix="svg_marker", suffix=".png") | ||
os.close(fd2) | ||
|
||
try: | ||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
pm = gen_pattern.PatternMaker(cols, rows, filesvg, "px", square_size, 0, board_width, | ||
board_height, "charuco_checkboard", marker_size, | ||
os.path.join(basedir, aruco_type_str[aruco_type_i]+'.json.gz')) | ||
pm.make_charuco_board() | ||
pm.save() | ||
drawing = svg2rlg(filesvg) | ||
renderPM.drawToFile(drawing, filepng, fmt='PNG', dpi=720) | ||
from_svg_img = cv.imread(filepng) | ||
|
||
#test | ||
_charucoCorners, _charucoIds, markerCorners_svg, markerIds_svg = charuco_detector.detectBoard(from_svg_img) | ||
_charucoCorners, _charucoIds, markerCorners_cv, markerIds_cv = charuco_detector.detectBoard(from_cv_img) | ||
|
||
np.testing.assert_allclose(markerCorners_svg, markerCorners_cv, 0.1, 0.1) | ||
np.testing.assert_allclose(markerIds_svg, markerIds_cv, 0.1, 0.1) | ||
finally: | ||
if os.path.exists(filesvg): | ||
os.remove(filesvg) | ||
if os.path.exists(filepng): | ||
os.remove(filepng) | ||
|
||
def test_aruco_marker_sizes(self): | ||
try: | ||
from svglib.svglib import svg2rlg | ||
from reportlab.graphics import renderPM | ||
except: | ||
raise self.skipTest("libraies svglib and reportlab not found") | ||
else: | ||
cols = 3 | ||
rows = 5 | ||
square_size = 100 | ||
aruco_type = cv.aruco.DICT_5X5_1000 | ||
aruco_type_str = 'DICT_5X5_1000' | ||
marker_sizes_rate = [0.25, 0.5, 0.75, 0.9] | ||
board_width = cols*square_size | ||
board_height = rows*square_size | ||
|
||
for marker_s_rate in marker_sizes_rate: | ||
marker_size = marker_s_rate*square_size | ||
#draw desk using opencv | ||
aruco_dict = cv.aruco.getPredefinedDictionary(aruco_type) | ||
board = cv.aruco.CharucoBoard((cols, rows), square_size, marker_size, aruco_dict) | ||
charuco_detector = cv.aruco.CharucoDetector(board) | ||
from_cv_img = board.generateImage((cols*square_size*10, rows*square_size*10)) | ||
|
||
#draw desk using svg | ||
fd1, filesvg = tempfile.mkstemp(prefix="out", suffix=".svg") | ||
os.close(fd1) | ||
fd2, filepng = tempfile.mkstemp(prefix="svg_marker", suffix=".png") | ||
os.close(fd2) | ||
|
||
try: | ||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
pm = gen_pattern.PatternMaker(cols, rows, filesvg, "px", square_size, 0, board_width, | ||
board_height, "charuco_checkboard", marker_size, os.path.join(basedir, aruco_type_str+'.json.gz')) | ||
pm.make_charuco_board() | ||
pm.save() | ||
drawing = svg2rlg(filesvg) | ||
renderPM.drawToFile(drawing, filepng, fmt='PNG', dpi=720) | ||
from_svg_img = cv.imread(filepng) | ||
|
||
#test | ||
_charucoCorners, _charucoIds, markerCorners_svg, markerIds_svg = charuco_detector.detectBoard(from_svg_img) | ||
_charucoCorners, _charucoIds, markerCorners_cv, markerIds_cv = charuco_detector.detectBoard(from_cv_img) | ||
|
||
np.testing.assert_allclose(markerCorners_svg, markerCorners_cv, 0.1, 0.1) | ||
np.testing.assert_allclose(markerIds_svg, markerIds_cv, 0.1, 0.1) | ||
finally: | ||
if os.path.exists(filesvg): | ||
os.remove(filesvg) | ||
if os.path.exists(filepng): | ||
os.remove(filepng) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
svglib>=1.5.1 | ||
reportlab>=4.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.