我希望能够从.xml
文件中读取已写入cv::Mat
Point2f keypoints
的文件,并将其读回原始状态std::vector<cv::KeyPoint>
我对文件的写作如下:
cv::Ptr<cv::FeatureDetector> detector = new cv::OrbFeatureDetector(featureSize);
cv::FileStorage fsKpts("keypoints.xml", cv::FileStorage::WRITE);
for(size_t i = 0; i < ImgVec.size(); i ++){
std::vector<cv::KeyPoint> imgKpts;
std::vector<cv::Point2f> points;
std::vector<cv::KeyPoint>::iterator it;
detector->detect(ImgVec[i], imgKpts);
for(it = imgKpts.begin(); it != imgKpts.end(); it ++) points.push_back(it->pt);
cv::Mat matKpts(points);
cv::string iValue = std::to_string(i);
cv::string filename = "Image_" + iValue;
fsKpts << filename << matKpts;
}
fsKpts.release();
但到目前为止,我完全无法将其读回原来的状态std::vector<cv::KeyPoint>
,以便进行keypoint
匹配。
任何帮助?
修改
目前的阅读猜测:
std::vector<cv::KeyPoint> fileKptsVec;
std::vector<cv::Point2f> filePoints;
cv::FileStorage fs("keypoints.xml", cv::FileStorage::READ);
for(size_t i = 0; i < imgVec.size(); i++){
cv::string iValue = std::to_string(i);
cv::string filename = "Image_" + iValue;
fs[filename] >> filePoints[i];
fileKptsVec.push_back(filePoints[i]); // -- Doesn't work
}
fs.release();
编辑2
.xml
内容:
<?xml version="1.0"?>
<opencv_storage>
<Image_0 type_id="opencv-matrix">
<rows>500</rows>
<cols>1</cols>
<dt>"2f"</dt>
<data>
456. 640. 458. 638. 603. 525. 411. 353. 604. 548. 417. 334. 600.
515. 382. 334. 594. 448. 572. 481. 449. 603. 574. 612. 441. 648.
470. 334. 439. 650. 412. 387. 438. 550. 568. 514. 575. 602. 452.
...
</data></Image_0>
<Image_1 type_id="opencv-matrix">
<rows>500</rows>
<cols>1</cols>
<dt>"2f"</dt>
<data>
521. 353. 467. 563. 537. 510. 505. 378. 479. 286. 451. 552. 460.
554. 541. 545. 463. 554. 492. 404. 457. 552. 534. 546. 463. 562.
541. 550. 519. 350. 490. 285. 498. 473. 497. 635. 451. 578. 461.
...
</data></Image_1>
etc. etc.
答案 0 :(得分:2)
您应该在http://docs.opencv.org/2.4.9/modules/core/doc/xml_yaml_persistence.html中执行功能。
如果你想使用Mat,当访问Mat元素时,基本上使用http://docs.opencv.org/2.4.9/modules/core/doc/basic_structures.html#mat-at你的模板将是Point2f。
Mat image_i;
fs2["Image_i"] >> image_i;
Point2f firstPoint = image_i.at<Point2f>(0);
修改强>
我检查了你的代码,所以你应该这样做:
std::vector<std::vector<cv::KeyPoint>> fileKptsVec;
std::vector<cv::Mat> filesVec(imgVec.size());
cv::FileStorage fs("keypoints.xml", cv::FileStorage::READ);
for(size_t i = 0; i < imgVec.size(); i++){
std::vector<cv::KeyPoint> imageIKeypoints;
cv::string iValue = std::to_string(i);
cv::string filename = "Image_" + iValue;
fs[filename] >> filesVec[i];
for(size_t j = 0; j < filesVec[i].rows; j++) {
cv::KeyPoint kp;
kp.pt = filesVec[i].at<cv::Point2f>(j);
imageIKeypoints.push_back(kp);
}
fileKptsVec.push_back(imageIKeypoints);
}
fs.release();
但是,当您返回KeyPoint时,您有位置但却丢失了其他信息。如果是问题:http://docs.opencv.org/2.4.9/modules/core/doc/xml_yaml_persistence.html表示功能。