我无法保存并将cv::Mat
保存的内容读取到cv::FileNode
我正在迭代的内容中。我可以使用将矩阵写入文件
// Data to save
std::vector<cv::Mat> masks;
masks.push_back(cv::Mat::ones(3,3,CV_8UC1));
masks.push_back(cv::Mat::zeros(2,2,CV_8UC1));
// Open file for writing
cv::FileStorage fs(file, cv::FileStorage::WRITE);
// Create node
fs << "data" << "[";
for (size_t i=0; i<masks.size(); ++i)
{
// Fill each node with data
fs << "{:";
// Write other data...
fs << "mask" << masks.at(i);
fs << "}";
}
fs << "]";
以这种方式写入文件不会导致任何问题。但是,随后打开此文件会导致未处理的异常打印:
读取 OpenCV 错误:解析错误 (testPR.yml(7): Missing , between the elemen ts) in unknown function, file ......\modules\core\src\persistence.cpp, line 12 63
要读取矩阵,我尝试这样做:
// Open file for reading
cv::FileStorage fs(file, cv::FileStorage::READ); ///< This causes an exception
// Open node
cv::FileNode d = fs["data"];
cv::FileNodeIterator it = d.begin(), it_end = d.end();
for( ; it!= it_end; ++it)
{
// Read other data...
cv::Mat mask;
(*it)["mask"] >> mask;
}
当然可以以这种方式将矩阵保存/读取到节点,不是吗?