使用OpenCV读取yaml文件

时间:2013-09-18 16:48:02

标签: opencv file-io

使用OpenCV,我想在不同的图像中找到关键点并将它们保存在我的硬盘上。这对于保存部分非常有效。要保存我使用的关键点:

Mat it;
it = imread( "pic1.jpg", IMREAD_GRAYSCALE);
vector<KeyPoint> keypoints;
detector.detect( (it), keypoints );
FileStorage fs("keypoint1.yml", FileStorage::WRITE);
write( fs , "keypoint", keypoints );
fs.release();

当我尝试再次阅读文件时:

vector<KeyPoint> keypoint1s;
FileStorage fs2("keypoint1.yml", FileStorage::READ);
FileNode kptFileNode = fs2["keypoint1"];
read( kptFileNode, keypoint1s );
fs2.release();

如果我这样做,“keypoint1s”为空。有什么问题?

1 个答案:

答案 0 :(得分:5)

阅读时您的变量名称错误:

FileNode kptFileNode = fs2["keypoint1"];

应该是

FileNode kptFileNode = fs2["keypoint"];

这对我有用:

vector<int> keypoints;
keypoints.push_back(1);
keypoints.push_back(2);

FileStorage fs("keypoint1.yml", FileStorage::WRITE);
write(fs , "keypoint", keypoints);
fs.release();

vector<int> newKeypoints;
FileStorage fs2("keypoint1.yml", FileStorage::READ);
FileNode kptFileNode = fs2["keypoint"];
read(kptFileNode, newKeypoints);
fs2.release();