diff --git a/modules/aruco/include/opencv2/aruco.hpp b/modules/aruco/include/opencv2/aruco.hpp index 65a19dff9..4e69ca3fd 100644 --- a/modules/aruco/include/opencv2/aruco.hpp +++ b/modules/aruco/include/opencv2/aruco.hpp @@ -281,6 +281,18 @@ class CV_EXPORTS_W Board { * */ CV_WRAP static Ptr create(InputArrayOfArrays objPoints, const Ptr &dictionary, InputArray ids); + + /** + * @brief Set ids vector + * + * @param ids vector of the identifiers of the markers in the board (should be the same size + * as objPoints) + * + * Recommended way to set ids vector, which will fail if the size of ids does not match size + * of objPoints. + */ + CV_WRAP void setIds(InputArray ids); + /// array of object points of all the marker corners in the board /// each marker include its 4 corners in CCW order. For M markers, the size is Mx4. CV_PROP std::vector< std::vector< Point3f > > objPoints; @@ -290,7 +302,7 @@ class CV_EXPORTS_W Board { /// vector of the identifiers of the markers in the board (same size than objPoints) /// The identifiers refers to the board dictionary - CV_PROP std::vector< int > ids; + CV_PROP_RW std::vector< int > ids; }; diff --git a/modules/aruco/misc/python/test/test_aruco.py b/modules/aruco/misc/python/test/test_aruco.py new file mode 100644 index 000000000..474aa87ec --- /dev/null +++ b/modules/aruco/misc/python/test/test_aruco.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +# Python 2/3 compatibility +from __future__ import print_function + +import os, numpy as np + +import cv2 as cv + +from tests_common import NewOpenCVTests + +class aruco_test(NewOpenCVTests): + + def test_idsAccessibility(self): + + ids = np.array([[elem] for elem in range(17)]) + rev_ids = np.array(list(reversed(ids))) + + aruco_dict = cv.aruco.Dictionary_get(cv.aruco.DICT_5X5_250) + board = cv.aruco.CharucoBoard_create(7, 5, 1, 0.5, aruco_dict) + + self.assertTrue(np.equal(board.ids, ids).all()) + + board.ids = rev_ids + self.assertTrue(np.equal(board.ids, rev_ids).all()) + + board.setIds(ids) + self.assertTrue(np.equal(board.ids, ids).all()) + + with self.assertRaises(cv.error): + board.setIds(np.array([0])) + +if __name__ == '__main__': + NewOpenCVTests.bootstrap() diff --git a/modules/aruco/src/aruco.cpp b/modules/aruco/src/aruco.cpp index 0667e3375..6b340f521 100644 --- a/modules/aruco/src/aruco.cpp +++ b/modules/aruco/src/aruco.cpp @@ -1659,6 +1659,13 @@ Ptr Board::create(InputArrayOfArrays objPoints, const Ptr &di return res; } +/** + */ +void Board::setIds(InputArray ids_) { + CV_Assert(objPoints.size() == ids_.total()); + ids_.copyTo(this->ids); +} + /** */ Ptr GridBoard::create(int markersX, int markersY, float markerLength, float markerSeparation,