zoukankan      html  css  js  c++  java
  • OpenCV——KAZE、AKAZE特征检测、匹配与对象查找

     

    AKAZE是KAZE的加速版

    特征点查找和绘制:把surf中的surf改成KAZEAKAZE即可

     1 #include <opencv2/opencv.hpp>
     2 #include <opencv2/xfeatures2d.hpp>
     3 #include <iostream>
     4 
     5 using namespace cv;
     6 using namespace cv::xfeatures2d;
     7 using namespace std;
     8 
     9 int main(int argc, char** argv) {
    10     Mat src = imread("test.jpg", IMREAD_GRAYSCALE);
    11     if (src.empty()) {
    12         printf("could not load image...
    ");
    13         return -1;
    14     }
    15     namedWindow("input image", CV_WINDOW_AUTOSIZE);
    16     imshow("input image", src);
    17 
    18     // AKAZE特征点检测
    19     Ptr<AKAZE> detector = AKAZE::create();//创建一个AKAZE类对象并初始化
    20     vector<KeyPoint> keypoints;
    21     detector->detect(src, keypoints, Mat());//找出关键点
    22 
    23     // 绘制关键点
    24     Mat keypoint_img;
    25     drawKeypoints(src, keypoints, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    26     imshow("KeyPoints Image", keypoint_img);
    27 
    28     waitKey(0);
    29     return 0;
    30 }

    匹配:

     1 #include <opencv2/opencv.hpp>
     2 #include <iostream>
     3 #include <math.h>
     4 
     5 using namespace cv;
     6 using namespace std;
     7 
     8 int main(int argc, char** argv) {
     9     Mat img1 = imread("fire_5.jpg", IMREAD_GRAYSCALE);
    10     Mat img2 = imread("数字.jpg", IMREAD_GRAYSCALE);
    11     if (img1.empty() || img2.empty()) {
    12         printf("could not load images...
    ");
    13         return -1;
    14     }
    15     imshow("box image", img1);
    16     imshow("scene image", img2);
    17 
    18     
    19     // extract akaze features
    20     Ptr<AKAZE> detector = AKAZE::create();
    21     vector<KeyPoint> keypoints_obj;
    22     vector<KeyPoint> keypoints_scene;
    23     Mat descriptor_obj, descriptor_scene;    
    24     detector->detectAndCompute(img1, Mat(), keypoints_obj, descriptor_obj);
    25     detector->detectAndCompute(img2, Mat(), keypoints_scene, descriptor_scene);
    26 
    27 
    28     // matching
    29     FlannBasedMatcher matcher(new flann::LshIndexParams(20, 10, 2));
    30     //FlannBasedMatcher matcher;
    31     vector<DMatch> matches;
    32     matcher.match(descriptor_obj, descriptor_scene, matches);
    33 
    34     // draw matches(key points)
    35     Mat akazeMatchesImg;
    36     /*
    37     drawMatches(img1, keypoints_obj, img2, keypoints_scene, matches, akazeMatchesImg);
    38     imshow("akaze match result", akazeMatchesImg);*/
    39     
    40     vector<DMatch> goodMatches;
    41     double minDist = 100000, maxDist = 0;
    42     for (int i = 0; i < descriptor_obj.rows; i++) {
    43         double dist = matches[i].distance;
    44         if (dist < minDist) {
    45             minDist = dist;
    46         }
    47         if (dist > maxDist) {
    48             maxDist = dist;
    49         }
    50     }
    51     printf("min distance : %f", minDist);
    52 
    53     for (int i = 0; i < descriptor_obj.rows; i++) {
    54         double dist = matches[i].distance;
    55         if (dist < max( 1.5*minDist, 0.02)) {
    56             goodMatches.push_back(matches[i]);
    57         }
    58     }
    59 
    60     drawMatches(img1, keypoints_obj, img2, keypoints_scene, goodMatches, akazeMatchesImg, Scalar::all(-1),
    61         Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
    62     imshow("good match result", akazeMatchesImg);
    63     
    64     waitKey(0);
    65     return 0;
    66 }
  • 相关阅读:
    iOS MVC、MVVM、MVP详解
    消息队列(mq)是什么?
    使用 tail 结合 grep 查找日志关键字并高亮及显示所在行上下文
    Linux下实现高可用软件-Keepalived基础知识梳理
    手动编译部署LNMP环境(CentOS7.5+Nginx-1.18.0+MySQL-5.7.30+PHP-7.4.14)
    linux 查看硬盘使用情况
    解决vue3.0新建项目无法选中Manually select features
    运行vue项目时报错:You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.
    git pull 报错remote: HTTP Basic: Access denied fatal: Authentication failed for 的解决方法
    Excel文档导出——后端返回文件流,前端实现下载功能
  • 原文地址:https://www.cnblogs.com/long5683/p/9740156.html
Copyright © 2011-2022 走看看