I am building a program in ROS using OpenCV C++ to detect ArUco codes.My OpenCV version is 4.2.
I've included the header file:
#include <opencv2/aruco.hpp>.
And called the function:
cv::aruco::ArucoDetector detector(dictionary, detectorParams);
While writing this line of code, vscode can provide automatic completion and allow me to navigate to the definition file of this function.BUT when I compile, an error occurs.
error: ‘ArucoDetector’ is not a member of ‘cv::aruco’
How can I resolve this issue? What caused this problem?
My code is as follows.
#include <opencv2/highgui.hpp>
#include <opencv2/aruco.hpp>
//#include <opencv2/objdetect/aruco_detector.hpp>
#include <Eigen/Dense>
#include <iostream>
#include "ros/ros.h"
#include "sensor_msgs/Image.h"
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
static const std::string OPENCV_WINDOW = "Image window";
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
public:
ImageConverter()
: it_(nh_)
{
// Subscrive to input video feed and publish output video feed
image_sub_ = it_.subscribe("/robot/camera/image", 1,&ImageConverter::imageCb, this);
cv::namedWindow(OPENCV_WINDOW);
}
~ImageConverter()
{
cv::destroyWindow(OPENCV_WINDOW);
}
void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
cv::Mat img = cv_ptr->image;
cv::Mat img_copy = img.clone();
cv::Mat img_gray;
//cv::imshow("test_opencv", img);
cv::cvtColor(img, img_gray, cv::COLOR_BGR2GRAY);
cv::aruco::DetectorParameters detectorParams = cv::aruco::DetectorParameters();
cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
cv::aruco::ArucoDetector detector(dictionary, detectorParams);
}
};
int main(int argc, char *argv[])
{
ros::init(argc, argv, "opencv_test");
ImageConverter ic;
ros::spin();
return 0;
}
The error message is as follows.
/home/zhilinxu/catkin_ws_webots/src/vis_ser_demo01/src/main.cpp: In member function ‘void ImageConverter::imageCb(const ImageConstPtr&)’:
/home/zhilinxu/catkin_ws_webots/src/vis_ser_demo01/src/main.cpp:94:20: error: ‘ArucoDetector’ is not a member of ‘cv::aruco’
94 | cv::aruco::ArucoDetector detector(dictionary, detectorParams);
| ^~~~~~~~~~~~~
make[2]: *** [vis_ser_demo01/CMakeFiles/demo01.dir/build.make:63: vis_ser_demo01/CMakeFiles/demo01.dir/src/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:902: vis_ser_demo01/CMakeFiles/demo01.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Invoking "make -j20 -l20" failed
It seems that cv::aruco::ArucoDetector is defined in the header file opencv2/objdetect/aruco_detector.hpp, which is already included in opencv2/aruco.hpp. When attempting to directly include opencv2/objdetect/aruco_detector.hpp in my code, I encountered redefinition errors during compilation.
In my code, both cv::aruco::DetectorParameters and cv::aruco::Dictionary are from opencv2/objdetect/aruco_detector.hpp, yet they compile fine. It's quite odd.
I too had this problem (though I was using cv 4.5), and found that the api seems to have changed
Instead of this (from the opencv example):
We end up with this:
Notice the use of cv::Ptr for the dictionary, and the static function detectMarkers (no need to instantiate a detector)