gpt4 book ai didi

c++ - opencv c++ 垫到 vector <关键点>

转载 作者:搜寻专家 更新时间:2023-10-31 02:22:50 26 4
gpt4 key购买 nike

在搜索了这个(1234)的答案后,我仍然对两者感到困惑

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

我希望能够从一个 .xml 文件中读取,该文件有一个包含 cv::MatPoint2f 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.

最佳答案

您应该按照以下方式进行操作: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功能。

关于c++ - opencv c++ 垫到 vector <关键点>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29898668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com