我有一段代码可以计算图像的SURF关键点并将其保存到yml
文件中。然后,我尝试加载该文件并在am image上绘制保存的关键点。
关键点并编写代码:
cv::Mat img_1 = cv::imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
detectKeypointsImage(img_1);
int minHessian = 400;
cv::SurfFeatureDetector detector(minHessian);
std::vector<cv::KeyPoint> keypoints_1;
detector.detect(img_1, keypoints_1);
cv::Mat img_keypoints_1;
drawKeypoints(img_1, keypoints_1, img_keypoints_1);
cv::FileStorage fs("keypointsVW.yml", cv::FileStorage::WRITE);
write(fs, "keypoints_1", keypoints_1);
fs.release();
为了测试是否有效,然后注释掉上面的代码块减去以下几行:
cv::Mat img_1 = cv::imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat img_keypoints_1;
然后,我使用以下代码读取关键点并将其绘制在图像上:
std::vector<cv::KeyPoint> testPoints;
cv::FileStorage fs2("keypointsVW.yml", cv::FileStorage::READ);
cv::FileNode kptFileNode = fs2["keypointsVW"];
read(kptFileNode, testPoints);
fs2.release();
drawKeypoints(img_1, testPoints, img_keypoints_1);
cv::imshow("keypoints_1", img_keypoints_1);
但是,程序启动时将显示减去所有关键点的图像。为什么会这样呢?
最佳答案
我从未使用过存储功能,但从直觉上我认为您在加载数据时错误地命名了FileNode。
我测试了这段代码,它的工作原理是:
std::vector<cv::KeyPoint> testPoints;
cv::FileStorage fs2("keypointsVW.yml", cv::FileStorage::READ);
cv::FileNode kptFileNode = fs2["keypoints_1"]; // Here you must use the name that you used for writing the data within the file.
// You named it "keypoints_1" before.
// It must be the same name that you used in write(fs, "keypoints_1", keypoints_1);
// so for example write(storage, "nodeName", data); needs you to call cv::FileNode kptFileNode = fs2["nodeName"]; later
read(kptFileNode, testPoints);
fs2.release();
drawKeypoints(img_1, testPoints, img_keypoints_1);
cv::imshow("keypoints_1", img_keypoints_1);
关于c++ - 加载关键点和工程图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27842444/