平面识别对象

#include <iostream>
#include <opencv2/opencv.hpp>
#include"opencv2/xfeatures2d.hpp"


using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main()
{
        Mat a = imread("C:/Users/zhang/Desktop/77.png", IMREAD_GRAYSCALE);    //读取灰度图像
        Mat b = imread("C:/Users/zhang/Desktop/76.png", IMREAD_GRAYSCALE);

        Ptr<SURF> surf;
        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);  //绘制匹配点

        vector<Point2f> obj;
        vector<Point2f> scene;

        for (size_t i = 0; i < good_matches.size(); i++)
        {
                obj.push_back(key1[good_matches[i].queryIdx].pt);
                //good_matches[i].queryIdx为好的匹配点的查询图像索引,如1,2,3
                //key1[good_matches[i].queryIdx].pt为查询图像上该特征点的坐标
                scene.push_back(key2[good_matches[i].trainIdx].pt);
                //good_matches[i].queryIdx为好的匹配点的训练图像索引,如1,2,3
                //key1[good_matches[i].queryIdx].pt为训练图像上该特征点的坐标
        }

        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);
        vector<Point2f> scene_corners(4);

        //透视变换
        Mat H = findHomography(obj, scene, RANSAC);
        //根据两幅图像的特征点求变换矩阵
        perspectiveTransform(obj_corners, scene_corners, H);
        //根据查询图像的坐标求训练图像ROI区域的坐标
        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);
        waitKey(0);
        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值