0
我正在計算單引號這裏。 http://docs.opencv.org/2.4/doc/tutorials/features2d/feature_homography/feature_homography.htmlopencv使用特徵匹配找到單應性
但是,即使它是一個參考代碼也有錯誤。 min_dist值始終爲0,因此代碼中的good_matches的大小爲0。
什麼是錯,我們該如何解決?
int main(int argc, char **argv)
{
cv::VideoCapture cam(0);
cv::Mat img(480, 640, CV_8UC3, cv::Scalar(0, 0, 0));
cv::Mat img2(480, 640, CV_8UC3, cv::Scalar(0, 0, 0));
cv::Ptr<cv::AKAZE> akaze = cv::AKAZE::create();
std::vector<cv::KeyPoint> kp;
cv::Mat descr;
while (1)
{
cam >> img;
cv::Mat aka;
akaze->detectAndCompute(img, cv::noArray(), kp, descr);
cv::drawKeypoints(img, kp, aka);
cv::imshow("AKAZE", aka);
if (cv::waitKey(10) == 's')
{
break;
}
kp.clear();
}
akaze.release();
std::vector<cv::KeyPoint> kp_query;
cv::Mat descr_query;
cv::Ptr<cv::AKAZE> akaze2 = cv::AKAZE::create();
while (1)
{
cv::Mat aka;
cam >> img2;
akaze2->detectAndCompute(img2, cv::noArray(), kp_query, descr_query);
cv::BFMatcher matcher(cv::NORM_HAMMING);
std::vector<cv::DMatch> matches;
matcher.match(descr_query, descr, matches);
double max_dist = 0, min_dist = 100.0;
for (int i = 0; i<descr_query.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}
printf("-- Max dist : %f \n", max_dist);
printf("-- Min dist : %f \n", min_dist);
std::vector<cv::DMatch> good_matches;
for (int i = 0; i<descr_query.rows; i++)
{
if (matches[i].distance < 3 * min_dist)
good_matches.push_back(matches[i]);
}
std::vector<cv::Point2f> train, query;
for (int i = 0; i<good_matches.size(); i++)
{
train.push_back(kp[good_matches[i].queryIdx].pt);
query.push_back(kp_query[good_matches[i].trainIdx].pt);
}
cv::Mat output_mask;
cv::Mat H = cv::findHomography(train, query, CV_RANSAC, 3, output_mask);
cv::Mat m;
cv::drawMatches(img, kp, img2, kp_query, good_matches, m, cv::Scalar::all(-1), cv::Scalar::all(-1),
std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
cv::imshow("match", m);
cv::drawKeypoints(img2, kp_query, aka);
cv::waitKey(10);
kp_query.clear();
}
}