您的位置:首页 > 运维架构

OPENCV3的匹配

2017-06-12 18:29 99 查看
看了下sift的原理,实践时发现opencv3已经开始版权保护了,也就是要用sift和surf,必须下个contrib的附加模块。

一开始用的python,很愉快,不过发现官方文档中的fast,orb算法都是donot find the module。

网上一查,才知道3.0的文档实际是2.4的,官网怎么就不更新呢。

所以用c++,啃一下源代码吧,好在只要看个函数头,大概能猜出做什么,再说网上例子很多。

sift要安装新模块,就懒得用了,大致看了下fast,orb,觉得orb不错。以下为代码:#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;

int main()
{
Mat img_1 = imread("C:\\Users\\acer\\Pictures\\algo\\book3.jpg");
Mat img_2 = imread("C:\\Users\\acer\\Pictures\\algo\\book5.jpg");
resize(img_1, img_1, Size(360, 640));
resize(img_2, img_2, Size(360,640));
if (!img_1.data || !img_2.data)
{
cout << "error reading images " << endl;
return -1;
}

Ptr<ORB> orb=ORB::create();
vector<KeyPoint> keyPoints_1, keyPoints_2;
Mat descriptors_1, descriptors_2;

orb->detect(img_1, keyPoints_1);
orb->detect(img_2, keyPoints_2);

orb->compute(img_1, keyPoints_1, descriptors_1);
orb->compute(img_2, keyPoints_2, descriptors_2);
BFMatcher matcher(NORM_HAMMING);
vector<DMatch> matches;
matcher.match(descriptors_1, descriptors_2, matches);

Mat img_mathes;
drawMatches(img_1, keyPoints_1, img_2, keyPoints_2, matches, img_mathes);
// -- show
namedWindow("Test");
imshow("Test", img_mathes);

waitKey(0);
return 0;
}




图片采用一个背景比较好,不同背景产生了大量的匹配错误,至于如何辨别是同一个物体以后解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 图像