【发布时间】:2021-05-18 07:12:50
【问题描述】:
我正在使用 openCV arUco 标记来检测一些标记。
所以我生成了预定义的字典并将其保存在一个文件中。
然而aruco::detectMarkers 只能得到Ptr<aruco::Dictionary>。所以我创建了一个Ptr<aruco::Dictionary> 对象并将对象本身的地址发送给构造函数。
这导致应用程序结束时出现异常。
我该如何解决?
这是我的(简化的)代码:
#include <opencv2/highgui.hpp>
#include <opencv2/aruco/charuco.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
aruco::Dictionary ReadDictionaryFromFile(std::string fileName)
{
cv::FileStorage fileStorage(fileName, cv::FileStorage::READ);
Mat bytesList;
int markerSize;
int maxCorrectionBits;
fileStorage\["markerSize"\] >> markerSize;
fileStorage\["maxCorrectionBits"\] >> maxCorrectionBits;
fileStorage\["bytesList"\] >> bytesList;
fileStorage.release();
aruco::Dictionary dictionary = cv::aruco::Dictionary(bytesList, markerSize, maxCorrectionBits);
return dictionary;
}
int main(int argc, char *argv\[\])
{
//Detector parameters:
Ptr<aruco::DetectorParameters> detectorParams = aruco::DetectorParameters::create();
//This works but I am afraid will generate another dictionary on another machine
//Ptr<aruco::Dictionary> dictionary = aruco::generateCustomDictionary(1, 4);
//This works but generate exception when app is terminated
aruco::Dictionary dictionaryTemp = ReadDictionaryFromFile("Dictionary.yml");
Ptr<aruco::Dictionary> dictionary = cv::Ptr<aruco::Dictionary>(&dictionaryTemp);
while (true)
{
if (camera.grab(image) != SUCCESS)
{
cout << "error on grab()" << std::endl;
return 0;
}
vector< int > ids;
vector< vector< Point2f > > corners, rejected;
vector< Vec3d > rvecs, tvecs;
// detect markers and estimate pose
aruco::detectMarkers(image, dictionary, corners, ids, detectorParams, rejected);
aruco::drawDetectedMarkers(image, corners, ids);
imshow("out", image);
char key = (char)waitKey(1);
if (key == 'q')
{
break;
}
}
return 0;
}
【问题讨论】:
标签: c++ opencv exception memory aruco