Skip to content

Commit

Permalink
Fixing ChArUco board support on opencv >= 4.7.0 (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
matlabbe authored Aug 26, 2024
1 parent 6ce85b4 commit 4df207a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ ENDIF()
set(RTABMAP_QT_VERSION AUTO CACHE STRING "Force a specific Qt version.")
set_property(CACHE RTABMAP_QT_VERSION PROPERTY STRINGS AUTO 4 5 6)

FIND_PACKAGE(OpenCV REQUIRED QUIET COMPONENTS core calib3d imgproc highgui stitching photo video videoio OPTIONAL_COMPONENTS aruco xfeatures2d nonfree gpu cudafeatures2d)
FIND_PACKAGE(OpenCV REQUIRED QUIET COMPONENTS core calib3d imgproc highgui stitching photo video videoio OPTIONAL_COMPONENTS aruco objdetect xfeatures2d nonfree gpu cudafeatures2d)

IF(WITH_QT)
FIND_PACKAGE(PCL 1.7 REQUIRED QUIET COMPONENTS common io kdtree search surface filters registration sample_consensus segmentation visualization)
Expand Down
2 changes: 1 addition & 1 deletion RTABMapConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include(CMakeFindDependencyMacro)

# Mandatory dependencies
find_dependency(OpenCV COMPONENTS core calib3d imgproc highgui stitching photo video OPTIONAL_COMPONENTS aruco xfeatures2d nonfree gpu cudafeatures2d)
find_dependency(OpenCV COMPONENTS core calib3d imgproc highgui stitching photo video OPTIONAL_COMPONENTS aruco objdetect xfeatures2d nonfree gpu cudafeatures2d)

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/RTABMap_guiTargets.cmake")
find_dependency(PCL 1.7 COMPONENTS common io kdtree search surface filters registration sample_consensus segmentation visualization)
Expand Down
9 changes: 8 additions & 1 deletion guilib/include/rtabmap/gui/CalibrationDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <rtabmap/utilite/UEventsHandler.h>

#if defined(HAVE_OPENCV_ARUCO) || CV_MAJOR_VERSION > 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION >= 7)
#if defined(HAVE_OPENCV_OBJDETECT) && (CV_MAJOR_VERSION > 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION >= 7))
#include <opencv2/objdetect/charuco_detector.hpp>
#define HAVE_CHARUCO
#elif defined(HAVE_OPENCV_ARUCO)
#include <opencv2/aruco/charuco.hpp>
#define HAVE_CHARUCO
#endif
Expand Down Expand Up @@ -141,6 +144,10 @@ private Q_SLOTS:
cv::Ptr<cv::aruco::Dictionary> markerDictionary_;
cv::Ptr<cv::aruco::DetectorParameters> arucoDetectorParams_;
cv::Ptr<cv::aruco::CharucoBoard> charucoBoard_;
#if CV_MAJOR_VERSION > 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION >= 7)
cv::Ptr<cv::aruco::ArucoDetector> arucoDetector_;
cv::Ptr<cv::aruco::CharucoDetector> charucoDetector_;
#endif
#endif


Expand Down
25 changes: 22 additions & 3 deletions guilib/src/CalibrationDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ void matchCharucoImagePoints(
objectPoints.push_back(board.chessboardCorners[pointId]);
}
#else
board.matchImagePoints(detectedCorners, detectedIds, objectPoints, cv::noArray());
cv::Mat imgPts;
board.matchImagePoints(detectedCorners, detectedIds, objectPoints, imgPts);
#endif
}
#endif
Expand Down Expand Up @@ -753,16 +754,32 @@ void CalibrationDialog::processImages(const cv::Mat & imageLeft, const cv::Mat &
UASSERT(charucoBoard_.get());

// detect markers
UDEBUG("Detecting aruco markers...");
#if CV_MAJOR_VERSION > 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION >= 7)
UASSERT(arucoDetector_.get());
arucoDetector_->detectMarkers(timg, markerCorners, markerIds, rejected);
#else
cv::aruco::detectMarkers(timg, markerDictionary_, markerCorners, markerIds, arucoDetectorParams_, rejected);

#endif
// refine strategy to detect more markers
UDEBUG("Refining aruco markers...");
#if CV_MAJOR_VERSION > 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION >= 7)
arucoDetector_->refineDetectedMarkers(timg, *charucoBoard_, markerCorners, markerIds, rejected);
#else
cv::aruco::refineDetectedMarkers(timg, charucoBoard_, markerCorners, markerIds, rejected);

#endif
// interpolate charuco corners
UDEBUG("Finding charuco corners (markers=%ld)...", markerCorners.size());
if(markerIds.size() > 0)
{
UASSERT(markerIds.size() == markerCorners.size());
#if CV_MAJOR_VERSION > 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION >= 7)
UASSERT(charucoDetector_.get());
charucoDetector_->detectBoard(timg, pointBuf[id], pointIds[id], markerCorners, markerIds);
#else
cv::aruco::interpolateCornersCharuco(markerCorners, markerIds, timg, charucoBoard_, pointBuf[id], pointIds[id], cv::noArray(), cv::noArray(), 1);
#endif
UDEBUG("Found %ld charuco corners (requires 12)", pointBuf[id].size());
if(pointBuf[id].size() >= 12) {
// Match image points
matchCharucoImagePoints(*charucoBoard_, pointBuf[id], pointIds[id], objectBuf[id]);
Expand Down Expand Up @@ -1273,6 +1290,8 @@ void CalibrationDialog::restart()
ui_->doubleSpinBox_markerLength->value(),
*markerDictionary_));
charucoBoard_->setLegacyPattern(ui_->comboBox_board_type->currentIndex()==1);
arucoDetector_.reset(new cv::aruco::ArucoDetector(*markerDictionary_, *arucoDetectorParams_));
charucoDetector_.reset(new cv::aruco::CharucoDetector(*charucoBoard_, cv::aruco::CharucoParameters(), *arucoDetectorParams_));
#else
charucoBoard_ = cv::aruco::CharucoBoard::create(
ui_->spinBox_boardWidth->value(),
Expand Down

0 comments on commit 4df207a

Please sign in to comment.