Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9bf43c3

Browse files
fix matchImagePoints, add py tests
1 parent96a45e8 commit9bf43c3

File tree

4 files changed

+71
-26
lines changed

4 files changed

+71
-26
lines changed

‎modules/objdetect/include/opencv2/objdetect/aruco_board.hpp‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class CV_EXPORTS_W_SIMPLE Board {
6161
* image points and object points to call solvePnP()
6262
*
6363
* @param detectedCorners List of detected marker corners of the board.
64-
*For CharucoBoard class you can set list of charuco corners.
65-
* @param detectedIds List of identifiers for each marker or list of charuco identifiers for each corner.
66-
*For CharucoBoard class you can set list of charuco identifiers for each corner.
64+
*Board/GridBoard support std::vector<std::vector<Point2f>> or std::vector<Mat> for marker corners.
65+
* @param detectedIds List of identifiers for each marker.
66+
*Board/GridBoard support std::vector<int> or Mat.
6767
* @param objPoints Vector of vectors of board marker points in the board coordinate space.
6868
* @param imgPoints Vector of vectors of the projections of board marker corner points.
6969
*/
@@ -142,6 +142,19 @@ class CV_EXPORTS_W_SIMPLE CharucoBoard : public Board {
142142
CV_WRAPfloatgetSquareLength()const;
143143
CV_WRAPfloatgetMarkerLength()const;
144144

145+
/** @brief Given a board configuration and a set of detected markers, returns the corresponding
146+
* image points and object points to call solvePnP()
147+
*
148+
* @param detectedCharuco List of detected charuco corners of the board.
149+
* CharucoBoard support std::vector<Point2f> or Mat for charuco corners.
150+
* @param detectedIds List of charuco identifiers for each corner.
151+
* CharucoBoard support std::vector<int> or Mat.
152+
* @param objPoints Vector of vectors of board marker points in the board coordinate space.
153+
* @param imgPoints Vector of vectors of the projections of board marker corner points.
154+
*/
155+
CV_WRAPvoidmatchImagePoints(InputArray detectedCharuco, InputArray detectedIds,
156+
OutputArray objPoints, OutputArray imgPoints)const;
157+
145158
/** @brief get CharucoBoard::chessboardCorners
146159
*/
147160
CV_WRAP std::vector<Point3f>getChessboardCorners()const;

‎modules/objdetect/misc/python/test/test_objdetect_aruco.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,33 @@ def test_charuco_detector(self):
142142
self.assertEqual(charucoIds[i],i)
143143
np.testing.assert_allclose(gold_corners,charucoCorners.reshape(-1,2),0.01,0.1)
144144

145+
deftest_aruco_match_image_points(self):
146+
aruco_dict=cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
147+
board_size= (3,4)
148+
board=cv.aruco.GridBoard(board_size,5.0,1.0,aruco_dict)
149+
aruco_corners=np.array(board.getObjPoints())[:, :, :2]
150+
aruco_ids=board.getIds()
151+
obj_points,img_points=board.matchImagePoints(aruco_corners,aruco_ids)
152+
aruco_corners=aruco_corners.reshape(-1,2)
153+
154+
self.assertEqual(aruco_corners.shape[0],obj_points.shape[0])
155+
self.assertEqual(img_points.shape[0],obj_points.shape[0])
156+
self.assertEqual(2,img_points.shape[2])
157+
np.testing.assert_array_equal(aruco_corners,obj_points[:, :, :2].reshape(-1,2))
158+
159+
deftest_charuco_match_image_points(self):
160+
aruco_dict=cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
161+
board_size= (3,4)
162+
board=cv.aruco.CharucoBoard(board_size,5.0,1.0,aruco_dict)
163+
chessboard_corners=np.array(board.getChessboardCorners())[:, :2]
164+
chessboard_ids=board.getIds()
165+
obj_points,img_points=board.matchImagePoints(chessboard_corners,chessboard_ids)
166+
167+
self.assertEqual(chessboard_corners.shape[0],obj_points.shape[0])
168+
self.assertEqual(img_points.shape[0],obj_points.shape[0])
169+
self.assertEqual(2,img_points.shape[2])
170+
np.testing.assert_array_equal(chessboard_corners,obj_points[:, :, :2].reshape(-1,2))
171+
172+
145173
if__name__=='__main__':
146174
NewOpenCVTests.bootstrap()

‎modules/objdetect/src/aruco/aruco_board.cpp‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct Board::Impl {
3434
};
3535

3636
voidBoard::Impl::matchImagePoints(InputArray detectedCorners, InputArray detectedIds, OutputArray _objPoints,
37-
OutputArray imgPoints)const {
37+
OutputArray imgPoints)const {
3838

3939
CV_Assert(ids.size() == objPoints.size());
4040
CV_Assert(detectedIds.total() == detectedCorners.total());
@@ -313,7 +313,7 @@ struct CharucoBoardImpl : Board::Impl {
313313

314314
voidcalcNearestMarkerCorners();
315315

316-
voidmatchImagePoints(InputArrayOfArrays detectedCorners, InputArray detectedIds,
316+
voidmatchImagePoints(InputArray detectedCharuco, InputArray detectedIds,
317317
OutputArray objPoints, OutputArray imgPoints)constoverride;
318318

319319
voidgenerateImage(Size outSize, OutputArray img,int marginSize,int borderBits)constoverride;
@@ -368,25 +368,28 @@ void CharucoBoardImpl::calcNearestMarkerCorners() {
368368
}
369369
}
370370

371-
voidCharucoBoardImpl::matchImagePoints(InputArrayOfArrays detectedCorners, InputArray detectedIds,
372-
OutputArray _objPoints, OutputArray imgPoints)const {
373-
if (detectedCorners.kind() == _InputArray::STD_VECTOR_VECTOR ||
374-
detectedCorners.isMatVector() || detectedCorners.isUMatVector())
375-
Board::Impl::matchImagePoints(detectedCorners, detectedIds, _objPoints, imgPoints);
376-
else {
377-
CV_Assert(detectedCorners.isMat() || detectedCorners.isVector());
378-
size_t nDetected = detectedCorners.total();
379-
vector<Point3f>objPnts(nDetected);
380-
vector<Point2f>imgPnts(nDetected);
381-
for(size_t i =0ull; i < nDetected; i++) {
382-
int pointId = detectedIds.getMat().at<int>((int)i);
383-
CV_Assert(pointId >=0 && pointId < (int)chessboardCorners.size());
384-
objPnts[i] = chessboardCorners[pointId];
385-
imgPnts[i] = detectedCorners.getMat().at<Point2f>((int)i);
386-
}
387-
Mat(objPnts).copyTo(_objPoints);
388-
Mat(imgPnts).copyTo(imgPoints);
371+
voidCharucoBoardImpl::matchImagePoints(InputArray detectedCharuco, InputArray detectedIds,
372+
OutputArray _objPoints, OutputArray imgPoints)const {
373+
// detectedCharuco includes charuco corners as vector<Point2f> or Mat.
374+
CV_Assert(detectedCharuco.isMat() || detectedCharuco.isVector());
375+
CV_Assert(detectedIds.total() == detectedCharuco.total());
376+
size_t nDetected = detectedCharuco.total();
377+
vector<Point3f>objPnts(nDetected);
378+
vector<Point2f>imgPnts(nDetected);
379+
for(size_t i =0ull; i < nDetected; i++) {
380+
int pointId = detectedIds.getMat().at<int>((int)i);
381+
CV_Assert(pointId >=0 && pointId < (int)chessboardCorners.size());
382+
objPnts[i] = chessboardCorners[pointId];
383+
imgPnts[i] = detectedCharuco.getMat().at<Point2f>((int)i);
389384
}
385+
Mat(objPnts).copyTo(_objPoints);
386+
Mat(imgPnts).copyTo(imgPoints);
387+
}
388+
389+
voidCharucoBoard::matchImagePoints(InputArray detectedCharuco, InputArray detectedIds, OutputArray objPoints,
390+
OutputArray imgPoints)const {
391+
CV_Assert(this->impl);
392+
impl->matchImagePoints(detectedCharuco, detectedIds, objPoints, imgPoints);
390393
}
391394

392395
voidCharucoBoardImpl::generateImage(Size outSize, OutputArray img,int marginSize,int borderBits)const {

‎modules/objdetect/src/aruco/charuco_detector.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ struct CharucoDetector::CharucoDetectorImpl {
130130
Mat approximatedRvec, approximatedTvec;
131131
Mat objPoints, imgPoints;// object and image points for the solvePnP function
132132
// printf("before board.matchImagePoints(markerCorners, markerIds, objPoints, imgPoints);\n");
133-
board.matchImagePoints(markerCorners, markerIds, objPoints, imgPoints);
133+
BoardsimpleBoard(board.getObjPoints(), board.getDictionary(), board.getIds());
134+
simpleBoard.matchImagePoints(markerCorners, markerIds, objPoints, imgPoints);
134135
// printf("after board.matchImagePoints(markerCorners, markerIds, objPoints, imgPoints);\n");
135136
if (objPoints.total() <4ull)// need, at least, 4 corners
136137
return;
@@ -292,7 +293,7 @@ void CharucoDetector::setRefineParameters(const RefineParameters& refineParamete
292293

293294
voidCharucoDetector::detectBoard(InputArray image, OutputArray charucoCorners, OutputArray charucoIds,
294295
InputOutputArrayOfArrays markerCorners, InputOutputArray markerIds)const {
295-
CV_Assert((markerCorners.empty() && markerIds.empty() && !image.empty()) || (markerCorners.size() == markerIds.size()));
296+
CV_Assert((markerCorners.empty() && markerIds.empty() && !image.empty()) || (markerCorners.total() == markerIds.total()));
296297
vector<vector<Point2f>> tmpMarkerCorners;
297298
vector<int> tmpMarkerIds;
298299
InputOutputArrayOfArrays _markerCorners = markerCorners.needed() ? markerCorners : tmpMarkerCorners;
@@ -321,7 +322,7 @@ void CharucoDetector::detectBoard(InputArray image, OutputArray charucoCorners,
321322
voidCharucoDetector::detectDiamonds(InputArray image, OutputArrayOfArrays _diamondCorners, OutputArray _diamondIds,
322323
InputOutputArrayOfArrays inMarkerCorners, InputOutputArrayOfArrays inMarkerIds)const {
323324
CV_Assert(getBoard().getChessboardSize() ==Size(3,3));
324-
CV_Assert((inMarkerCorners.empty() && inMarkerIds.empty() && !image.empty()) || (inMarkerCorners.size() == inMarkerIds.size()));
325+
CV_Assert((inMarkerCorners.empty() && inMarkerIds.empty() && !image.empty()) || (inMarkerCorners.total() == inMarkerIds.total()));
325326

326327
vector<vector<Point2f>> tmpMarkerCorners;
327328
vector<int> tmpMarkerIds;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp