首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >opencv c++ mat到vector<keypoint>

opencv c++ mat到vector<keypoint>
EN

Stack Overflow用户
提问于 2015-04-27 14:33:45
回答 1查看 3.2K关注 0票数 2

在寻找这个问题的答案(1234)之后,我仍然对这两个问题感到困惑。

  • 如果有可能的话
  • 如果是的话,怎么做呢?

我希望能够从一个.xml文件中读取,该文件包含一个包含Point2f keypointscv::Mat,并将其读回原来的std::vector<cv::KeyPoint>状态。

我写的文件如下:

代码语言:javascript
复制
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匹配。

有什么帮助吗?

编辑

当前阅读的猜测:

代码语言:javascript
复制
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内容:

代码语言:javascript
复制
<?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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-27 16:12:25

您应该在:persistence.html中对特性执行如下操作。

但是,如果您想使用Mat,那么在访问Mat元素时,基本上使用structures.html#mat-at,您的模板将是Point2f。

代码语言:javascript
复制
Mat image_i;
fs2["Image_i"] >> image_i;
Point2f firstPoint = image_i.at<Point2f>(0);

编辑:

我检查了你的代码,所以,你应该做:

代码语言:javascript
复制
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时,您有位置,但是丢失了其他信息。如果这是一个问题:persistence.html的特性。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29898668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档