opencv3 特征检测与匹配及寻找目标

来源:互联网 发布:淘宝哪家店女装时尚 编辑:程序博客网 时间:2024/05/29 18:05

1.算法:surf特征提取算法

SURF算法是著名的尺度不变特征检测器SIFT(Scale-Invariant Features Transform)的高效变种,它为每个检测到的特征定义了位置和尺度,其中尺度的值可用于定义围绕特征点的窗口大小,使得每个特征点都与众不同。这里便是使用SURF算法提取两幅图像中的特征点描述子,并调用OpenCV中的函数进行匹配,

2 代码

//特征检测与匹配#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(){     //对BGR空间的图像直接进行计算很费时间,所以,需要转换为灰度图      Mat a = imread("/home/daniel/catkin_ws/src/cvbridge/src/1.jpg", IMREAD_GRAYSCALE);    //读取灰度图像    Mat b = imread("/home/daniel/catkin_ws/src/cvbridge/src/2.jpg", 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);   // if(!outimg.empty())    imshow("aaaa",outimg);    cvWaitKey(0);}

3 错误集锦

这里写图片描述
这个错误折腾了好几天,一直还以为是opencv版本的问题,所以就一直各种卸载安装,最终解决办法:
我是在ubuntu下做的,就把catkin_ws工作空间下的devel和build文件夹删了,重新执行

catkin_makerosrun cvbridge cvbridge

终于好了。。。
这里写图片描述

原创粉丝点击