gpt4 book ai didi

c++ - OpenCV 中的重复率

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:28 25 4
gpt4 key购买 nike

我使用以下代码在 Microsoft Visual Studio 2012 中使用 C++ 检测描述和评估 OpenCV 2.4.10 中的特性。

#include <iostream>
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>

using namespace cv;
using namespace std;

int main(int argc, char ** argv)
{
Mat image1;
image1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); // Read the first file

Mat image2;
image2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);

if ((!image1.data) || (!image2.data)) {
std::cout << "ERROR: Cannot load images in\n" << argv[1] << "\n" << argv[2] << endl;
return -1;
}

vector < KeyPoint > keypoints1, keypoints2;
cv::Mat descriptors1, descriptors2;

/** Construction of the feature detector
*/

double ExTime = (double) cv::getTickCount();
cv::SurfFeatureDetector surf(800);

/** Detection of the features
*/
surf.detect(image1, keypoints1);
surf.detect(image2, keypoints2);

cv::SurfDescriptorExtractor surfDesc;
surfDesc.compute(image1, keypoints1, descriptors1);
surfDesc.compute(image2, keypoints2, descriptors2);

//Calculate the time needed for code execution
ExTime = ((double) cv::getTickCount() - ExTime) / cv::getTickFrequency();

/** Draw the keypoints
*/
Mat ImageKP1, ImageKP2;

drawKeypoints(image1, keypoints1, ImageKP1, cv::Scalar(255, 0, 255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
drawKeypoints(image2, keypoints2, ImageKP2, cv::Scalar(255, 0, 255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

// Construction of the matcher
cv::Mat ImageMatch;
cv::FlannBasedMatcher matcher;

// Match the two image descriptors
std::vector < DMatch > matches;
matcher.match(descriptors1, descriptors2, matches);

double max_dist = 0;
double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for (int i = 0; i < descriptors1.rows; i++) {
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}

//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
//-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
//-- small)
std::vector < DMatch > good_matches;

for (int i = 0; i < descriptors1.rows; i++) {
if (matches[i].distance <= 2 * min_dist) {
good_matches.push_back(matches[i]);
}
}

cout << "Number of good matches: " << good_matches.size() << endl;

drawMatches(image1, keypoints1, image2, keypoints2, good_matches, ImageMatch, cv::Scalar(255, 0, 255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

/** Evaluation of detected points
*/
std::cout << ">" << std::endl;
cout << "Evaluating feature detector..." << endl;
float repeatability;
int corrCounter;
cv::Mat Homog;

std::vector < cv::Point2f > srcKey;
std::vector < cv::Point2f > refKey;

for (int i = 0; i < matches.size(); i++) {
srcKey.push_back(keypoints1[matches[i].queryIdx].pt);
refKey.push_back(keypoints2[matches[i].queryIdx].pt);
}

Homog = cv::findHomography(srcKey, refKey, CV_RANSAC, 1);

cv::evaluateFeatureDetector(image1, image2, Homog, & keypoints1, & keypoints2, repeatability, corrCounter);

std::cout << "repeatability = " << repeatability << std::endl;
std::cout << "correspCount = " << corrCounter << std::endl;
std::cout << ">" << std::endl;

system("pause");

return 0;
}

问题在于 repeatability 率始终为 -1,correspCount 也是如此。我使用我的图像,它们有很大的重叠并且检测到许多特征。我在OpenCV的官方网站上找不到关于该功能的教程

cv::EvaluateFeatureDetector

但只是像这样的站点中的一些教程。有一些类似的问题,但没有人总是 -1 作为返回。可能有什么问题?

最佳答案

如果你勾选the source code对于 evaluateFeatureDetector 函数,它检查关键点并调用 calculateRepeatability 函数。在该函数中,第 436 行:

correspondencesCount = -1;
repeatability = -1.f;
if( overlaps.empty() )
return;

值设置为 -1,如果两幅图像中过滤后的关键点之间没有重叠,则返回这些值。在这种情况下,由于您的实际关键点 vector 不为空,因此我会说您的单应性不正确。这可能是因为图像不够相似,或者单应性计算不正确。由于您确定第一个不是这种情况,因此我建议您怀疑生成的单应矩阵。您可以使用 this tutorial通过由此产生的单应性在透视变换下绘制图像,这样如果图像意外扭曲,您将了解上述功能如何/为何失败。

关于c++ - OpenCV 中的重复率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292383/

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