版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbaodong070411209/article/details/89349848
开发AR应用,不仅需要三维投影方面的原创,也需要二维图像中位姿定位。
今天,我们只是实现了位姿定位功能,主要分两部分,一部分:生成标定物;第二部分:检测标定物,计算位姿。
生成标定物源码
- 参数设置
"{@outfile |a.jpg | Output image }"
"{w |1 | Number of markers in X direction }"
"{h |1 | Number of markers in Y direction }"
"{l |64 | Marker side length (in pixels) }"
"{s |1 | Separation between two consecutive markers in the grid (in pixels)}"
"{d |2 | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
"DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
"{m |2 | Margins size (in pixels). Default is marker separation (-s) }"
"{bb | 1 | Number of bits in marker borders }"
"{si | false | show generated image }";
2.生成标定物
// Dictionary
Ptr<aruco::Dictionary> dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
// Creat Board
Ptr<aruco::GridBoard> board = aruco::GridBoard::create(markersX, markersY, float(markerLength), float(markerSeparation), dictionary);
// show created board
Mat boardImage;
board->draw(imageSize, boardImage, margins, borderBits);
位姿&定位
- 参数设置
"{w |1 | Number of squares in X direction }"
"{h |1 | Number of squares in Y direction }"
"{l |64 | Marker side lenght (in pixels) }"
"{s |1 | Separation between two consecutive markers in the grid (in pixels)}"
"{d |2 | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
"DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
"{c | | Output file with calibrated camera parameters }"
"{v | | Input from video file, if ommited, input comes from camera }"
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
"{dp | detector_params.yml | File of marker detector parameters }"
"{rs | | Apply refind strategy }"
"{r | | show rejected candidates too }";
2.参数输入
2.1 相机内参
Mat camMatrix = (Mat_<double>(3, 3) << 550.7876, 0, 331.2659,
0, 550.5972, 264.1054,
0 , 0 , 1.0000);
/*k1,k2,p1,p2,k3*/
Mat distCoeffs = (Mat_<double>(5, 1) << -0.3804 , 0.1771, 0.0012 , 0.0001, 0);
2.2 检测阈值
%YAML:1.0
nmarkers: 1024
adaptiveThreshWinSizeMin: 3
adaptiveThreshWinSizeMax: 23
adaptiveThreshWinSizeStep: 10
adaptiveThreshWinSize: 21
adaptiveThreshConstant: 7
minMarkerPerimeterRate: 0.03
maxMarkerPerimeterRate: 4.0
polygonalApproxAccuracyRate: 0.05
minCornerDistance: 10.0
minDistanceToBorder: 3
minMarkerDistance: 10.0
minMarkerDistanceRate: 0.05
cornerRefinementMethod: 0
cornerRefinementWinSize: 5
cornerRefinementMaxIterations: 30
cornerRefinementMinAccuracy: 0.1
markerBorderBits: 1
perspectiveRemovePixelPerCell: 8
perspectiveRemoveIgnoredMarginPerCell: 0.13
maxErroneousBitsInBorderRate: 0.04
minOtsuStdDev: 5.0
errorCorrectionRate: 0.6
- 定位
// detect markers
aruco::detectMarkers(image, dictionary, corners, ids, detectorParams, rejected);
- 位姿计算
// estimate board pose
int markersOfBoardDetected = 0;
if(ids.size() > 0)
markersOfBoardDetected = aruco::estimatePoseBoard(corners, ids, board, camMatrix, distCoeffs, rvec, tvec);
通过,上述几个步骤,基本完成了,标定物的检测、定位和位姿计算。下面看一下实验效果: