0

我有一段代码可以计算图像的 SURF 关键点并将它们保存到yml文件中。然后我尝试加载该文件并在图像上绘制保存的关键点。

关键点并编写代码:

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);

但是,当程序启动时,它显示的图像减去任何关键点。为什么会这样?

4

1 回答 1

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);
于 2015-01-08T15:21:53.220 回答