【OpenCV】OpenCV3中的SURF特征点的寻找和匹配


不多说什么了,直接上代码吧:

#include <iostream>
#include <stdio.h>
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include"opencv2/flann.hpp"
#include"opencv2/xfeatures2d.hpp"
#include"opencv2/ml.hpp"

using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
using namespace cv::ml;

int main()
{
    Mat a = imread("box.png", IMREAD_GRAYSCALE);    //读取灰度图像
    Mat b = imread("box_in_scene.png", IMREAD_GRAYSCALE);

    Ptr<SURF> surf;      //创建方式和2中的不一样
    surf = SURF::create(800);

    BFMatcher matcher;
    Mat c, d;
    vector<KeyPoint>key1, key2;
    vector<DMatch> matches;

    surf->detectAndCompute(a, Mat(), key1, c);
    surf->detectAndCompute(b, Mat(), key2, d);

    matcher.match(c, d, matches);       //匹配

    sort(matches.begin(), matches.end());  //筛选匹配点
    vector< DMatch > good_matches;              
    int ptsPairs = std::min(50, (int)(matches.size() * 0.15));
    cout << ptsPairs << endl;
    for (int i = 0; i < ptsPairs; i++)
    {
        good_matches.push_back(matches[i]);
    }
    Mat outimg;
    drawMatches(a, key1, b, key2, good_matches, outimg, Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  //绘制匹配点

    std::vector<Point2f> obj;
    std::vector<Point2f> scene;

    for (size_t i = 0; i < good_matches.size(); i++)
    {
        obj.push_back(key1[good_matches[i].queryIdx].pt);
        scene.push_back(key2[good_matches[i].trainIdx].pt);
    }

    std::vector<Point2f> obj_corners(4);
    obj_corners[0] = Point(0, 0);
    obj_corners[1] = Point(a.cols, 0);
    obj_corners[2] = Point(a.cols, a.rows);
    obj_corners[3] = Point(0, a.rows);
    std::vector<Point2f> scene_corners(4);

    Mat H = findHomography(obj, scene, RANSAC);      //寻找匹配的图像
    perspectiveTransform(obj_corners, scene_corners, H);

    line(outimg,scene_corners[0] + Point2f((float)a.cols, 0), scene_corners[1] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);       //绘制
    line(outimg,scene_corners[1] + Point2f((float)a.cols, 0), scene_corners[2] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);
    line(outimg,scene_corners[2] + Point2f((float)a.cols, 0), scene_corners[3] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);
    line(outimg,scene_corners[3] + Point2f((float)a.cols, 0), scene_corners[0] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);
    imshow("aaaa",outimg);
    cvWaitKey(0);
}


运行图: 
这里写图片描述



//-------------读取模板------------
    cv::Mat img_object = imread("/storage/emulated/0/ApplePearFace/imgTemp.jpg");
    //-------------图像处理---------
    cv::Mat img_scene(yimage);

    /*
    // 检测surf特征点
    int minHessian = 400;
    OrbDescriptorExtractor detector(minHessian);

    std::vector<KeyPoint> keypoints_1, keypoints_2;
    detector.detect(img_1, keypoints_1);
    detector.detect(img_2, keypoints_2);
    //-- Step 2: Calculate descriptors (feature vectors)
    OrbDescriptorExtractor extractor;
    Mat descriptors_1, descriptors_2;
    extractor.compute(img_1, keypoints_1, descriptors_1);
    extractor.compute(img_2, keypoints_2, descriptors_2);

    //-- Step 3: Matching descriptor vectors with a brute force matcher
    BFMatcher matcher(NORM_L2);
    std::vector< DMatch > matches;
    matcher.match(descriptors_1, descriptors_2, matches);

    //-- Draw matches
    Mat img_matches;
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_matches);*/

    // 读取数据
    //cv::Mat img_object = cv::imread("doll01.jpg");
    //cv::Mat img_scene = cv::imread("doll012.jpg");
    if (!img_object.data || !img_scene.data) {
        cout << "Error reading images." << endl;
        return 0;
    }

    // 构建特征检测器和描述子提取器
    cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("ORB");
    cv::Ptr<cv::DescriptorExtractor> descriptor = cv::DescriptorExtractor::create("ORB");

    // 检测特征点
    vector<cv::KeyPoint> kp_object, kp_scene;
    detector->detect(img_object, kp_object);
    detector->detect(img_scene, kp_scene);

    // 计算描述子
    cv::Mat desp_object, desp_scene;
    descriptor->compute(img_object, kp_object, desp_object);
    descriptor->compute(img_scene, kp_scene, desp_scene);

    /*
    if (desp_object.type() != CV_32F) {
    desp_object.convertTo(desp_object, CV_32F);
    }

    if (desp_scene.type() != CV_32F) {
    desp_scene.convertTo(desp_scene, CV_32F);
    }
    */

    // 匹配描述子
    vector<cv::DMatch> matches;
    cv::FlannBasedMatcher matcher(new cv::flann::LshIndexParams(20, 10, 2));
    matcher.match(desp_object, desp_scene, matches);
    //cout << "Find total " << matches.size() << " matches." << endl;

    // 筛选匹配
    //double min_dist = 100000;
    //for (int i = 0; i < matches.size(); i++) {
    //	float a = matches[i].distance;
    //	if (a < min_dist) {
    //		min_dist = matches[i].distance;
    //	}
    //}

    //vector<cv::DMatch> good_matches;
    //for (int i = 0; i < matches.size(); i++) {
    //
    //	if (matches[i].distance < 3 * min_dist) {
    //		good_matches.push_back(matches[i]);
    //	}
    //}

    // 显示匹配
    //cout << "Good matches=" << matches.size() << endl;
    cv::Mat img_matches;
    cv::drawMatches(img_object, kp_object, img_scene, kp_scene, matches, img_matches);

    // 定位目标
    cv::vector<cv::Point2f> obj_points;
    cv::vector<cv::Point2f> scene;

    for (int i = 0; i < matches.size(); i++) {
        obj_points.push_back(kp_object[matches[i].queryIdx].pt);
        scene.push_back(kp_scene[matches[i].trainIdx].pt);
    }
    cv::Mat H = cv::findHomography(obj_points, scene, CV_RANSAC);


    cv::vector<cv::Point2f> obj_corners(4);
    cv::vector<cv::Point2f> scene_corners(4);
    obj_corners[0] = cv::Point(0, 0);
    obj_corners[1] = cv::Point(img_object.cols, 0);
    obj_corners[2] = cv::Point(img_object.cols, img_object.rows);
    obj_corners[3] = cv::Point(0, img_object.rows);

    cv::perspectiveTransform(obj_corners, scene_corners, H);

    cv::line(img_matches, scene_corners[0] + cv::Point2f(img_object.cols, 0), scene_corners[1] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);
    cv::line(img_matches, scene_corners[1] + cv::Point2f(img_object.cols, 0), scene_corners[2] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);
    cv::line(img_matches, scene_corners[2] + cv::Point2f(img_object.cols, 0), scene_corners[3] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);
    cv::line(img_matches, scene_corners[3] + cv::Point2f(img_object.cols, 0), scene_corners[0] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);


    cv::Mat dstSize;
    cv::resize(img_matches, dstSize, Size(2 * h, w));


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值