opencv3 环境安装及实验surf特征的描述符匹配文章整理

来源:互联网 发布:java数组赋值未满 长度 编辑:程序博客网 时间:2024/06/07 03:57


OpenCV3.1 SIFT使用

OpenCV3对OpenCV的模块进行了调整,将开发中与nofree模块放在 了OpenCV_contrib中(包含SIFT),gitHub上的官方项目分成了两个,opencv 与 opencv_contrib。所以,要使用sift接口需在opencv3.1基础上,再安装opencv_contrib。本文主要记录如何安装opencv_contrib,配置Xcode,sift接口的用法。 
环境:OSX + Xcode + OpenCV3.1

  • OpenCV31 SIFT使用
    • install opencv_contrib
    • configuration Xcode
      • pro_name Build Setting Search Paths
      • pro_name Build Setting Other Linker Flags
    • sample of sift
    • References

install opencv_contrib

  • download contrib source code https://github.com/Itseez/opencv_contrib, follow README.md to install
$ cd <opencv_build_directory>$ cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>$ make -j5$ sudo make install
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Where <opencv_build_directory> and <opencv_source_directory> is directory in opencv3.1 install tutorial

configuration Xcode

like How to develop OpenCV with Xcode

pro_name Build Setting > Search Paths

  • /usr/local/lib
  • /usr/local/include

pro_name Build Setting >Other Linker Flags

  • -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lippicv -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core

sample of sift

  • sample in (souce_dir)/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp or bellow

    #include "opencv2/xfeatures2d.hpp"// // now, you can no more create an instance on the 'stack', like in the tutorial// (yea, noticed for a fix/pr).// you will have to use cv::Ptr all the way down://cv::Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();//cv::Ptr<Feature2D> f2d = xfeatures2d::SURF::create();//cv::Ptr<Feature2D> f2d = ORB::create();// you get the picture, i hope..//-- Step 1: Detect the keypoints:std::vector<KeyPoint> keypoints_1, keypoints_2;    f2d->detect( img_1, keypoints_1 );f2d->detect( img_2, keypoints_2 );//-- Step 2: Calculate descriptors (feature vectors)    Mat descriptors_1, descriptors_2;    f2d->compute( img_1, keypoints_1, descriptors_1 );f2d->compute( img_2, keypoints_2, descriptors_2 );//-- Step 3: Matching descriptor vectors using BFMatcher :BFMatcher matcher;std::vector< DMatch > matches;matcher.match( descriptors_1, descriptors_2, matches );
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

References

https://github.com/Itseez/opencv_contrib 
https://github.com/Itseez/opencv 
http://blog.csdn.net/lijiang1991/article/details/50756065 
http://docs.opencv.org/3.1.0/d5/d3c/classcv_1_1xfeatures2d_1_1SIFT.html#gsc.tab=0


一个实验代码::


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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include "opencv2/core.hpp"  
  4. #include "opencv2/core/utility.hpp"  
  5. #include "opencv2/core/ocl.hpp"  
  6. #include "opencv2/imgcodecs.hpp"  
  7. #include "opencv2/highgui.hpp"  
  8. #include "opencv2/features2d.hpp"  
  9. #include "opencv2/calib3d.hpp"  
  10. #include "opencv2/imgproc.hpp"  
  11. #include"opencv2/flann.hpp"  
  12. #include"opencv2/xfeatures2d.hpp"  
  13. #include"opencv2/ml.hpp"  
  14.   
  15. using namespace cv;  
  16. using namespace std;  
  17. using namespace cv::xfeatures2d;  
  18. using namespace cv::ml;  
  19.   
  20. int main()  
  21. {  
  22.     Mat a = imread("box.png", IMREAD_GRAYSCALE);    //读取灰度图像  
  23.     Mat b = imread("box_in_scene.png", IMREAD_GRAYSCALE);  
  24.   
  25.     Ptr<SURF> surf;      //创建方式和2中的不一样  
  26.     surf = SURF::create(800);  
  27.   
  28.     BFMatcher matcher;  
  29.     Mat c, d;  
  30.     vector<KeyPoint>key1, key2;  
  31.     vector<DMatch> matches;  
  32.   
  33.     surf->detectAndCompute(a, Mat(), key1, c);  
  34.     surf->detectAndCompute(b, Mat(), key2, d);  
  35.   
  36.     matcher.match(c, d, matches);       //匹配  
  37.   
  38.     sort(matches.begin(), matches.end());  //筛选匹配点  
  39.     vector< DMatch > good_matches;                
  40.     int ptsPairs = std::min(50, (int)(matches.size() * 0.15));  
  41.     cout << ptsPairs << endl;  
  42.     for (int i = 0; i < ptsPairs; i++)  
  43.     {  
  44.         good_matches.push_back(matches[i]);  
  45.     }  
  46.     Mat outimg;  
  47.     drawMatches(a, key1, b, key2, good_matches, outimg, Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  //绘制匹配点  
  48.   
  49.     std::vector<Point2f> obj;  
  50.     std::vector<Point2f> scene;  
  51.   
  52.     for (size_t i = 0; i < good_matches.size(); i++)  
  53.     {  
  54.         obj.push_back(key1[good_matches[i].queryIdx].pt);  
  55.         scene.push_back(key2[good_matches[i].trainIdx].pt);  
  56.     }  
  57.   
  58.     std::vector<Point2f> obj_corners(4);  
  59.     obj_corners[0] = Point(00);  
  60.     obj_corners[1] = Point(a.cols, 0);  
  61.     obj_corners[2] = Point(a.cols, a.rows);  
  62.     obj_corners[3] = Point(0, a.rows);  
  63.     std::vector<Point2f> scene_corners(4);  
  64.   
  65.     Mat H = findHomography(obj, scene, RANSAC);      //寻找匹配的图像  
  66.     perspectiveTransform(obj_corners, scene_corners, H);  
  67.   
  68.     line(outimg,scene_corners[0] + Point2f((float)a.cols, 0), scene_corners[1] + Point2f((float)a.cols, 0),Scalar(02550), 2, LINE_AA);       //绘制  
  69.     line(outimg,scene_corners[1] + Point2f((float)a.cols, 0), scene_corners[2] + Point2f((float)a.cols, 0),Scalar(02550), 2, LINE_AA);  
  70.     line(outimg,scene_corners[2] + Point2f((float)a.cols, 0), scene_corners[3] + Point2f((float)a.cols, 0),Scalar(02550), 2, LINE_AA);  
  71.     line(outimg,scene_corners[3] + Point2f((float)a.cols, 0), scene_corners[0] + Point2f((float)a.cols, 0),Scalar(02550), 2, LINE_AA);  
  72.     imshow("aaaa",outimg);  
  73.     cvWaitKey(0);  
  74. }  


运行图: 
这里写图片描述



[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //-------------读取模板------------  
  2.     cv::Mat img_object = imread("/storage/emulated/0/ApplePearFace/imgTemp.jpg");  
  3.     //-------------图像处理---------  
  4.     cv::Mat img_scene(yimage);  
  5.   
  6.     /* 
  7.     // 检测surf特征点 
  8.     int minHessian = 400; 
  9.     OrbDescriptorExtractor detector(minHessian); 
  10.  
  11.     std::vector<KeyPoint> keypoints_1, keypoints_2; 
  12.     detector.detect(img_1, keypoints_1); 
  13.     detector.detect(img_2, keypoints_2); 
  14.     //-- Step 2: Calculate descriptors (feature vectors) 
  15.     OrbDescriptorExtractor extractor; 
  16.     Mat descriptors_1, descriptors_2; 
  17.     extractor.compute(img_1, keypoints_1, descriptors_1); 
  18.     extractor.compute(img_2, keypoints_2, descriptors_2); 
  19.  
  20.     //-- Step 3: Matching descriptor vectors with a brute force matcher 
  21.     BFMatcher matcher(NORM_L2); 
  22.     std::vector< DMatch > matches; 
  23.     matcher.match(descriptors_1, descriptors_2, matches); 
  24.  
  25.     //-- Draw matches 
  26.     Mat img_matches; 
  27.     drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_matches);*/  
  28.   
  29.     // 读取数据  
  30.     //cv::Mat img_object = cv::imread("doll01.jpg");  
  31.     //cv::Mat img_scene = cv::imread("doll012.jpg");  
  32.     if (!img_object.data || !img_scene.data) {  
  33.         cout << "Error reading images." << endl;  
  34.         return 0;  
  35.     }  
  36.   
  37.     // 构建特征检测器和描述子提取器  
  38.     cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("ORB");  
  39.     cv::Ptr<cv::DescriptorExtractor> descriptor = cv::DescriptorExtractor::create("ORB");  
  40.   
  41.     // 检测特征点  
  42.     vector<cv::KeyPoint> kp_object, kp_scene;  
  43.     detector->detect(img_object, kp_object);  
  44.     detector->detect(img_scene, kp_scene);  
  45.   
  46.     // 计算描述子  
  47.     cv::Mat desp_object, desp_scene;  
  48.     descriptor->compute(img_object, kp_object, desp_object);  
  49.     descriptor->compute(img_scene, kp_scene, desp_scene);  
  50.   
  51.     /* 
  52.     if (desp_object.type() != CV_32F) { 
  53.     desp_object.convertTo(desp_object, CV_32F); 
  54.     } 
  55.  
  56.     if (desp_scene.type() != CV_32F) { 
  57.     desp_scene.convertTo(desp_scene, CV_32F); 
  58.     } 
  59.     */  
  60.   
  61.     // 匹配描述子  
  62.     vector<cv::DMatch> matches;  
  63.     cv::FlannBasedMatcher matcher(new cv::flann::LshIndexParams(20102));  
  64.     matcher.match(desp_object, desp_scene, matches);  
  65.     //cout << "Find total " << matches.size() << " matches." << endl;  
  66.   
  67.     // 筛选匹配  
  68.     //double min_dist = 100000;  
  69.     //for (int i = 0; i < matches.size(); i++) {  
  70.     //  float a = matches[i].distance;  
  71.     //  if (a < min_dist) {  
  72.     //      min_dist = matches[i].distance;  
  73.     //  }  
  74.     //}  
  75.   
  76.     //vector<cv::DMatch> good_matches;  
  77.     //for (int i = 0; i < matches.size(); i++) {  
  78.     //  
  79.     //  if (matches[i].distance < 3 * min_dist) {  
  80.     //      good_matches.push_back(matches[i]);  
  81.     //  }  
  82.     //}  
  83.   
  84.     // 显示匹配  
  85.     //cout << "Good matches=" << matches.size() << endl;  
  86.     cv::Mat img_matches;  
  87.     cv::drawMatches(img_object, kp_object, img_scene, kp_scene, matches, img_matches);  
  88.   
  89.     // 定位目标  
  90.     cv::vector<cv::Point2f> obj_points;  
  91.     cv::vector<cv::Point2f> scene;  
  92.   
  93.     for (int i = 0; i < matches.size(); i++) {  
  94.         obj_points.push_back(kp_object[matches[i].queryIdx].pt);  
  95.         scene.push_back(kp_scene[matches[i].trainIdx].pt);  
  96.     }  
  97.     cv::Mat H = cv::findHomography(obj_points, scene, CV_RANSAC);  
  98.   
  99.   
  100.     cv::vector<cv::Point2f> obj_corners(4);  
  101.     cv::vector<cv::Point2f> scene_corners(4);  
  102.     obj_corners[0] = cv::Point(00);  
  103.     obj_corners[1] = cv::Point(img_object.cols, 0);  
  104.     obj_corners[2] = cv::Point(img_object.cols, img_object.rows);  
  105.     obj_corners[3] = cv::Point(0, img_object.rows);  
  106.   
  107.     cv::perspectiveTransform(obj_corners, scene_corners, H);  
  108.   
  109.     cv::line(img_matches, scene_corners[0] + cv::Point2f(img_object.cols, 0), scene_corners[1] + cv::Point2f(img_object.cols, 0), cv::Scalar(02550), 4);  
  110.     cv::line(img_matches, scene_corners[1] + cv::Point2f(img_object.cols, 0), scene_corners[2] + cv::Point2f(img_object.cols, 0), cv::Scalar(02550), 4);  
  111.     cv::line(img_matches, scene_corners[2] + cv::Point2f(img_object.cols, 0), scene_corners[3] + cv::Point2f(img_object.cols, 0), cv::Scalar(02550), 4);  
  112.     cv::line(img_matches, scene_corners[3] + cv::Point2f(img_object.cols, 0), scene_corners[0] + cv::Point2f(img_object.cols, 0), cv::Scalar(02550), 4);  
  113.   
  114.   
  115.     cv::Mat dstSize;  
  116.     cv::resize(img_matches, dstSize, Size(2 * h, w));  




0 0