加载关键点和绘图
Loading keypoints and drawing
我有一段代码可以计算图像的 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);
然而,当程序启动时,它显示的是减去任何关键点的图像。为什么会这样?
我从未使用过存储功能,但凭直觉我认为您在加载数据时将 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);
我有一段代码可以计算图像的 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);
然而,当程序启动时,它显示的是减去任何关键点的图像。为什么会这样?
我从未使用过存储功能,但凭直觉我认为您在加载数据时将 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);