Loading saved SURF keypoints

2019-08-10 01:27发布

问题:

I am detecting SURF features in an image and then writing them to a yml file. I then want to load the features from the yml file again to try and detect an object but at the moment I'm having trouble loading the keypoints to draw them on an image.

I'm writing the keypoints like so:

cv::FileStorage fs("keypointsVW.yml", cv::FileStorage::WRITE);
write(fs, "keypoints_1", keypoints_1);
fs.release();

I am trying to read them like so:

cv::FileStorage fs2("keypointsVW.yml", cv::FileStorage::READ);
read(fs2, "keypoints_1", keypoints_1);
fs2.release();

But this is producing a host of errors.

Detection and draw code:

    cv::Mat img_1 = cv::imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
    int minHessian = 400;
    cv::SurfFeatureDetector detector(minHessian);
    std::vector<cv::KeyPoint> keypoints_1;
    detector.detect(img_1, keypoints_1);
    cv::Mat img_keypoints_1;
    //......write code
    //.......read code
    drawKeypoints(img_1, keypoints_1, img_keypoints_1);
    imshow("keypoints_1", img_keypoints_1);

回答1:

Found the solution, I'll post it here in case anyone else has the same problem.

std::vector<cv::KeyPoint> testPoints;
cv::FileStorage fs2("keypointsVW.yml", cv::FileStorage::READ);
cv::FileNode kptFileNode = fs2["keypointsVW"];
read(kptFileNode, testPoints);
fs2.release();