Skip to content

Commit 2df0634

Browse files
authored
Fix charuco board detection bug (#96)
1 parent 69bf682 commit 2df0634

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

McCalib/include/Board.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class Board final {
4949
// Functions
5050
Board() = delete;
5151
~Board(){};
52-
Board(const std::filesystem::path &config, const int board_idx);
52+
Board(const std::filesystem::path &config, const int board_idx,
53+
const cv::Ptr<cv::aruco::CharucoBoard> charuco_board);
5354
void insertNewBoard(const std::shared_ptr<BoardObs> new_board);
5455
void insertNewFrame(const std::shared_ptr<Frame> new_frame);
5556
};

McCalib/src/Board.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ namespace McCalib {
1919
* @param config_path path to the configuration file
2020
* @param board_idx index of the board
2121
*/
22-
Board::Board(const std::filesystem::path &config_path, const int board_idx) {
22+
Board::Board(const std::filesystem::path &config_path, const int board_idx,
23+
const cv::Ptr<cv::aruco::CharucoBoard> charuco_board) {
2324
std::vector<int> number_x_square_per_board, number_y_square_per_board;
2425
std::vector<double> square_size_per_board;
2526
std::vector<int> boards_index;
@@ -59,6 +60,19 @@ Board::Board(const std::filesystem::path &config_path, const int board_idx) {
5960
}
6061

6162
fs.release(); // close the input file
63+
64+
nb_pts_ = (nb_x_square_ - 1) * (nb_y_square_ - 1);
65+
66+
// Prepare the 3D pts of the boards
67+
pts_3d_.reserve(nb_pts_);
68+
for (int y = 0; y < nb_y_square_ - 1; y++) {
69+
for (int x = 0; x < nb_x_square_ - 1; x++) {
70+
pts_3d_.emplace_back(x * square_size_, y * square_size_, 0);
71+
}
72+
}
73+
74+
charuco_board_ = charuco_board;
75+
6276
board_id_ = board_idx;
6377

6478
// initialize color of the board

McCalib/src/McCalib.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,18 @@ Calibration::Calibration(const std::filesystem::path &config_path) {
119119
}
120120

121121
const auto charuco_boards = createCharucoBoards(
122-
nb_board_, number_x_square_per_board_, number_y_square_per_board_,
122+
max_board_idx + 1, number_x_square_per_board_, number_y_square_per_board_,
123123
length_square, length_marker, dict_);
124124

125-
// Initialize the 3D boards
126-
for (auto const &[i, charuco_board] : charuco_boards) {
127-
// Initialize board
128-
std::shared_ptr<Board> new_board = std::make_shared<Board>(config_path, i);
129-
boards_3d_[i] = new_board;
130-
boards_3d_[i]->nb_pts_ =
131-
(boards_3d_[i]->nb_x_square_ - 1) * (boards_3d_[i]->nb_y_square_ - 1);
132-
boards_3d_[i]->charuco_board_ = charuco_board;
133-
boards_3d_[i]->pts_3d_.reserve(boards_3d_[i]->nb_pts_);
134-
// Prepare the 3D pts of the boards
135-
for (int y = 0; y < boards_3d_[i]->nb_y_square_ - 1; y++) {
136-
for (int x = 0; x < boards_3d_[i]->nb_x_square_ - 1; x++) {
137-
boards_3d_[i]->pts_3d_.emplace_back(x * boards_3d_[i]->square_size_,
138-
y * boards_3d_[i]->square_size_, 0);
139-
}
140-
}
125+
for (unsigned int board_idx = 0u; board_idx < nb_board_; ++board_idx) {
126+
const int charuco_board_idx =
127+
boards_index.size() > 0 ? boards_index[board_idx] : board_idx;
128+
assert(charuco_boards.count(charuco_board_idx) > 0);
129+
const cv::Ptr<cv::aruco::CharucoBoard> charuco_board =
130+
charuco_boards.at(charuco_board_idx);
131+
132+
boards_3d_[board_idx] =
133+
std::make_shared<Board>(config_path, board_idx, charuco_board);
141134
}
142135
}
143136

tests/test_calibration.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111

1212
#define PI 3.14159265
1313

14-
constexpr double INTRINSICS_TOLERANCE = 4.0; // in percentage
14+
constexpr double INTRINSICS_TOLERANCE = 4.0; // in percentage
15+
constexpr double ROTATION_ERROR_TOLERANCE = 1.0; // in degrees
16+
17+
#if (defined(CV_VERSION_MAJOR) && CV_VERSION_MAJOR <= 4 && \
18+
defined(CV_VERSION_MINOR) && CV_VERSION_MINOR < 7)
1519
constexpr double TRANSLATION_ERROR_TOLERANCE = 0.005; // in meters
16-
constexpr double ROTATION_ERROR_TOLERANCE = 1.0; // in degrees
20+
#else
21+
constexpr double TRANSLATION_ERROR_TOLERANCE = 0.01; // in meters
22+
#endif
1723

1824
double getTranslationError(cv::Mat a, cv::Mat b) {
1925
double dist = cv::norm(a, b, cv::NORM_L2);

0 commit comments

Comments
 (0)