使用OpenCV 4.8/4.10检测Aruco标记时触发断言失败错误
OpenCV 4.8.1/4.10.0-dev中Aruco标记检测触发断言失败问题
我是一名C++/OpenCV初学者,近期在学习Aruco标记检测时遇到棘手问题。使用OpenCV 4.8.1和4.10.0-dev版本检测Aruco标记时,持续触发以下断言失败错误:
OpenCV(4.8.1) Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /home/ci/opencv/modules/core/src/matrix_wrap.cpp, line 1393
编译命令
编译这两个OpenCV版本使用的命令如下:
$ gcc --version gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 $ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/raj/.local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules .. $ make -j$(nproc) && make install
检测代码(OpenCV 4.5+版本使用)
#include <opencv2/opencv.hpp> #include <opencv2/objdetect/aruco_detector.hpp> #include <iostream> #define DICT cv::aruco::DICT_6X6_100 cv::Mat generateMarker(int id) { cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(DICT); cv::Mat marker; cv::aruco::generateImageMarker(dictionary, 0, 200, marker, 1); return marker; } //for OpenCV version > 4.5 int main() { // read image cv::Mat imgCopy, img = generateMarker(10); // cv::imread("/tmp/sk2.png"); std::cout << "Converting to Gray scale! " << std::endl; // convert to Gray scale before detection int incn = img.channels(); if (incn == 3 || incn == 4) cvtColor(img, imgCopy, cv::COLOR_BGR2GRAY); else if (incn == 1) img.copyTo(imgCopy); else { std::cout << "Unsupported image format" << std::endl; img.release(); return -1; } // detect aruco markers: cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(DICT); cv::aruco::ArucoDetector detector(dictionary); std::vector<int> markerIds; std::vector<std::vector<cv::Point2f>> markerCorners; try { detector.detectMarkers(imgCopy, markerIds, markerCorners); } catch (std::exception& e) { std::cout << "Exception: " << e.what() << std::endl; return -1; } std::cout << "Detected markers: " << markerIds.size() << std::endl; for (int i = 0; i < markerIds.size(); i++) { std::cout << "Marker ID: " << markerIds[i] << std::endl; } // draw detected markers if (markerIds.size() > 0) cv::aruco::drawDetectedMarkers(imgCopy, markerCorners, markerIds); cv::imshow("Image", imgCopy); // wait for a key press to exit cv::waitKey(0); cv::destroyAllWindows(); img.release(); imgCopy.release(); return 0; }
可正常运行的代码(OpenCV 4.5.1版本使用)
相同编译命令下,适配的检测代码在OpenCV 4.5.1版本中可正常运行,对应代码如下:
int main() { std::cout << "OpenCV version: " << CV_VERSION << std::endl; // read image cv::Mat imgCopy, img = cv::imread("/tmp/sk2.png"); cvtColor(img, imgCopy, cv::COLOR_BGR2GRAY); std::vector<int> ids; std::vector<std::vector<cv::Point2f>> corners; cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250); cv::aruco::detectMarkers(imgCopy, dictionary, corners, ids); if (ids.size() > 0) { cv::aruco::drawDetectedMarkers(imgCopy, corners, ids); } cv::imshow("Image", imgCopy); cv::waitKey(0); cv::destroyAllWindows(); img.release(); imgCopy.release(); }
测试图片

内容的提问来源于stack exchange,提问作者raju mb
相关产品推荐
相关产品推荐

