OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体

使用二维特征点(Features2D)和单映射(Homography)寻找已知物体

目标

在本教程中我们将涉及以下内容:

理论

代码

这个教程的源代码如下所示。你还可以从 以下链接下载到源代码

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"

using namespace cv;

void readme();

/** @function main */
int main( int argc, char** argv )
{
  if( argc != 3 )
  { readme(); return -1; }

  Mat img_object = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
  Mat img_scene = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );

  if( !img_object.data || !img_scene.data )
  { std::cout<< " --(!) Error reading images " << std::endl; return -1; }

  //-- Step 1: Detect the keypoints using SURF Detector
  int minHessian = 400;

  SurfFeatureDetector detector( minHessian );

  std::vector<KeyPoint> keypoints_object, keypoints_scene;

  detector.detect( img_object, keypoints_object );
  detector.detect( img_scene, keypoints_scene );

  //-- Step 2: Calculate descriptors (feature vectors)
  SurfDescriptorExtractor extractor;

  Mat descriptors_object, descriptors_scene;

  extractor.compute( img_object, keypoints_object, descriptors_object );
  extractor.compute( img_scene, keypoints_scene, descriptors_scene );

  //-- Step 3: Matching descriptor vectors using FLANN matcher
  FlannBasedMatcher matcher;
  std::vector< DMatch > matches;
  matcher.match( descriptors_object, descriptors_scene, matches );

  double max_dist = 0; double min_dist = 100;

  //-- Quick calculation of max and min distances between keypoints
  for( int i = 0; i < descriptors_object.rows; i++ )
  { double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
  }

  printf("-- Max dist : %f \n", max_dist );
  printf("-- Min dist : %f \n", min_dist );

  //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
  std::vector< DMatch > good_matches;

  for( int i = 0; i < descriptors_object.rows; i++ )
  { if( matches[i].distance < 3*min_dist )
     { good_matches.push_back( matches[i]); }
  }

  Mat img_matches;
  drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
               good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
               vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

  //-- Localize the object
  std::vector<Point2f> obj;
  std::vector<Point2f> scene;

  for( int i = 0; i < good_matches.size(); i++ )
  {
    //-- Get the keypoints from the good matches
    obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
    scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
  }

  Mat H = findHomography( obj, scene, CV_RANSAC );

  //-- Get the corners from the image_1 ( the object to be "detected" )
  std::vector<Point2f> obj_corners(4);
  obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
  obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
  std::vector<Point2f> scene_corners(4);

  perspectiveTransform( obj_corners, scene_corners, H);

  //-- Draw lines between the corners (the mapped object in the scene - image_2 )
  line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
  line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );

  //-- Show detected matches
  imshow( "Good Matches & Object detection", img_matches );

  waitKey(0);
  return 0;
  }

  /** @function readme */
  void readme()
  { std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }

解释

结果

  1. 检测到的目标结果 (用绿色标记出来的部分)

翻译者

Shuai Zheng, <[email protected]>, http://www.cbsr.ia.ac.cn/users/szheng/

from: http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/features2d/feature_homography/feature_homography.html#feature-homography

时间: 2024-10-10 04:03:02

OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体的相关文章

编程题:指向二维数组元素的指针变量。功能:已知二维数组a[2][3],输入输出全部元素。

#include<stdio.h> void main() { int a[2][3],i,j; int *p; /*用坐标法输入二维数组元素*/ for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); /*用指针法输出二维数组元素*/ p=a[0];         /*等价于p&a[0][0];*/ for(;p<a[0]+6;p++)   printf("%4d&quo

使用OpenCV查找二值图中最大连通区域

http://blog.csdn.net/shaoxiaohu1/article/details/40272875 使用OpenCV查找二值图中最大连通区域 标签: OpenCVfindCoutours 2014-10-19 22:31 2802人阅读 评论(0) 收藏 举报  分类: 图像与OpenCV(15)  版权声明:本文为shaoxiaohu原创文章,欢迎转载,请注明出处,谢谢. 上一篇博文中介绍了matlab查找最大连通区域的方法,OpenCV函数中也有类似的函数与之对应,findC

Ubuntu 14.04 下使用 OpenCV 图片二值化处理

参考: OpenCV - Ubuntu 14.04 64 bit 图片二值化工具 Ubuntu 14.04 下使用 OpenCV 图片二值化处理 TBD. 原文地址:https://www.cnblogs.com/qq952693358/p/8996719.html

二十五、防止表单重复提交

二十五.防止表单重复提交 防止表单重复提交: 有两种方式: 利用重定向<result type = "redirect"/> 使用拦截器 编写jsp页面 <s:form action="regist"> ????????<s:textfield name="name" label="姓名"></s:textfield> ????????<s:token/> ?????

已知一个数组,将所有元素乘二存储到原数组

/** * */package Student_System;import java.util.*;/**Homework11 * *ArrayDemo05 * 已知一个数组,将所有元素乘二存储到原数组 * @author 读你一世* * QQ: 1816274408 *2017年4月13日下午10:05:22 * */public class ArrayDemo05 { public static void main(String[] args){// 确定数组元素,便于赋值 Scanner

Hive架构层面优化之二合理利用中间结果集(单Job)

是针对单个作业,针对本job再怎么优化也不会影响到其他job: Hadoop的负载主要有两部分:CPU负载和IO负载: 问题:机器io开销很大,但是机器的cpu开销较小,另外map输出文件也较大,怎么办? 解决办法:通过设置map的中间输出进行压缩就可以了,这个不会影响最终reduce的输出. 集群中的机器一旦选定了,那么CPU就没的改变了,所以集群的最主要的负载还是IO负载: 压缩技术虽然可以降低IO负载,但是同时也加重了CPU负载,治标不治本,CPU加重了,整体性能还是上不去:如果当前CPU

OpenCV教程二 - Mat对象与它各种用法

学习OpenCV大家都会遇到一个对象叫做Mat,此对象非常神奇,支持各种操作.很多初学者因此被搞得头晕脑胀,它各种用法太多太杂,搞得初学者应接不暇,感觉有心无力.无处下手之感.这里我们首先要正本清源,从Mat对象的产生原因说起,然后再把Mat各种神奇用法一一梳理总结. Mat对象起源: 当OpenCV 1.0发布时候没有Mat对象,是个C语言风格的数据结构IPlImage来表示内存中图像对象,但是OpenCV开发者在做复杂图像处理算法分析与计算时候,创建了很多IplImage这样的数据结构,偶尔

opencv 删除二值化图像中面积较小的连通域

对于上图的二值化图像,要去除左下角和右上角的噪点,方法:使用opencv去掉黑色面积较小的连通域. 代码 CvSeq* contour = NULL; double minarea = 100.0; double tmparea = 0.0; CFileDialog dlg(true); if (dlg.DoModal()==IDOK) { CvMemStorage* storage = cvCreateMemStorage(0); IplImage* img_src= cvLoadImage(

opencv统计二值图黑白像素个数

#include "iostream" #include "queue" #include "Windows.h" #include <opencv2/ml/ml.hpp> #include "opencv2/opencv.hpp" #include "Windows.h" #include "opencv2/core/core.hpp" #include "ope