Skip to content

Commit

Permalink
Fix aruco for OpenCV>=4.7 (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymd-stella authored Jun 12, 2024
1 parent 0a1f13e commit aa928c9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,25 @@ message(STATUS "Use OpenCV ${OpenCV_VERSION}")

# Check aruco module
set(USE_ARUCO ON CACHE BOOL "Enable aruco")
set(LINK_OBJDETECT OFF CACHE BOOL "Link opencv_objdetect")
set(LINK_ARUCO OFF CACHE BOOL "Link opencv_aruco")
list(APPEND ARUCO_CHECK_INCLUDE_DIRS
${OpenCV_INSTALL_PATH}/include/
${OpenCV_INSTALL_PATH}/include/opencv4)
unset(ARUCO_INCLUDE_DIRS CACHE)
find_path(ARUCO_INCLUDE_DIRS NAMES aruco.hpp
PATH_SUFFIXES opencv2
PATHS ${ARUCO_CHECK_INCLUDE_DIRS})
if(ARUCO_INCLUDE_DIRS)
set(LINK_ARUCO ON)
else()
find_path(ARUCO_INCLUDE_DIRS NAMES aruco_detector.hpp
PATH_SUFFIXES opencv2/objdetect
PATHS ${ARUCO_CHECK_INCLUDE_DIRS})
if(ARUCO_INCLUDE_DIRS)
set(LINK_OBJDETECT ON)
endif()
endif()
if(USE_ARUCO AND ARUCO_INCLUDE_DIRS)
message(STATUS "aruco: enabled (Found in ${ARUCO_INCLUDE_DIRS})")
else()
Expand Down
3 changes: 2 additions & 1 deletion src/stella_vslam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ target_link_libraries(${PROJECT_NAME}
opencv_core
opencv_features2d
opencv_calib3d
"$<$<BOOL:${USE_ARUCO}>:opencv_aruco>"
"$<$<BOOL:${LINK_OBJDETECT}>:opencv_objdetect>"
"$<$<BOOL:${LINK_ARUCO}>:opencv_aruco>"
g2o::core
g2o::stuff
g2o::types_sba
Expand Down
5 changes: 3 additions & 2 deletions src/stella_vslam/marker_detector/aruco.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using PredefinedDictionaryType = cv::aruco::PREDEFINED_DICTIONARY_NAME;
using PredefinedDictionaryObjectType = cv::Ptr<cv::aruco::Dictionary>;
#else
#include <opencv2/objdetect/aruco_detector.hpp>
using PredefinedDictionaryType = cv::aruco::PredefinedDictionaryType;
using PredefinedDictionaryObjectType = cv::aruco::Dictionary;
#endif
Expand Down Expand Up @@ -84,7 +85,7 @@ aruco::aruco(const camera::base* camera,
parameters_ = cv::aruco::DetectorParameters::create();
dictionary_ = get_predefined_dictionary(aruco_marker_model->marker_size_, aruco_marker_model->max_markers_);
#else
detector_ = cv::aruco::ArucoDetector(get_predefined_dictionary(aruco_marker_model->marker_size_, aruco_marker_model->max_markers_));
detector_ = std::make_shared<cv::aruco::ArucoDetector>(get_predefined_dictionary(aruco_marker_model->marker_size_, aruco_marker_model->max_markers_));
#endif
}

Expand All @@ -96,7 +97,7 @@ void aruco::detect_2d(const cv::_InputArray& in_image, std::vector<std::vector<c
#if CV_MAJOR_VERSION <= 4 && CV_MINOR_VERSION < 7
cv::aruco::detectMarkers(in_image, dictionary_, corners, ids, parameters_);
#else
detector_.detectMarkers(in_image, corners, ids);
detector_->detectMarkers(in_image, corners, ids);
#endif
}

Expand Down
8 changes: 6 additions & 2 deletions src/stella_vslam/marker_detector/aruco.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class Dictionary;
} // namespace aruco
} // namespace cv
#else
#include <opencv2/objdetect/aruco_detector.hpp>
namespace cv {
namespace aruco {
class ArucoDetector;
} // namespace aruco
} // namespace cv
#endif

#include <opencv2/core/mat.hpp>
Expand Down Expand Up @@ -56,7 +60,7 @@ class aruco : public base {
cv::Ptr<cv::aruco::DetectorParameters> parameters_;
cv::Ptr<cv::aruco::Dictionary> dictionary_;
#else
cv::aruco::ArucoDetector detector_;
std::shared_ptr<cv::aruco::ArucoDetector> detector_;
#endif
};

Expand Down

0 comments on commit aa928c9

Please sign in to comment.