首页 > 其他分享 >采用 opencv surf 算子进行特征匹配

采用 opencv surf 算子进行特征匹配

时间:2023-01-20 11:38:51浏览次数:60  
标签:surf dist img -- descriptors matches opencv 算子 include


​opencv docu​​​​源码在git​

目录结构
.
├── build
├── CMakeLists.txt
├── main.cpp
├── t1.jpg
└── t2.jpg
/*
* @file SURF_FlannMatcher
* @brief SURF detector + descriptor + FLANN Matcher
* @author A. Huaman
*/
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/xfeatures2d.hpp"
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
void readme();
/*
* @function main
* @brief Main function
*/
int main( int argc, char** argv )
{
if( argc != 3 )
{ readme(); return -1; }
Mat img_1 = imread( argv[1], IMREAD_GRAYSCALE );
Mat img_2 = imread( argv[2], IMREAD_GRAYSCALE );
if( !img_1.data || !img_2.data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
//-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
int minHessian = 400;
Ptr<SURF> detector = SURF::create();

detector->setHessianThreshold(minHessian);
std::vector<KeyPoint> keypoints_1, keypoints_2;
Mat descriptors_1, descriptors_2;
detector->detectAndCompute( img_1, Mat(), keypoints_1, descriptors_1 );
detector->detectAndCompute( img_2, Mat(), keypoints_2, descriptors_2 );
//-- Step 2: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches );
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.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 2*min_dist,
//-- or a small arbitrary value ( 0.02 ) in the event that min_dist is very
//-- small)
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_1.rows; i++ )
{ if( matches[i].distance <= max(2*min_dist, 0.02) )
{ good_matches.push_back( matches[i]); }
}
//-- Draw only "good" matches
Mat img_matches;
drawMatches( img_1, keypoints_1, img_2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//-- Show detected matches
imshow( "Good Matches", img_matches );
for( int i = 0; i < (int)good_matches.size(); i++ )
{ printf( "-- Good Match [%d] Keypoint 1: %d -- Keypoint 2: %d \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx ); }
waitKey(0);
return 0;
}
/*
* @function readme
*/
void readme()
{ std::cout << " Usage: ./SURF_FlannMatcher <img1> <img2>" << std::endl; }
cd build
cmake ../
make
./DisplayImage ../t1.jpg ../t2.jpg

采用 opencv surf 算子进行特征匹配_人工智能


标签:surf,dist,img,--,descriptors,matches,opencv,算子,include
From: https://blog.51cto.com/u_15202985/6020568

相关文章

  • opencv视频操作
    importcv2#开启电脑摄像头读取cap=cv2.VideoCapture(0)#视频帧率fps=int(cap.get(cv2.CAP_PROP_FPS))#视频的宽width=int(cap.get(cv2.CAP_PROP_FRAME_WID......
  • C++ OpenCV
    准备工作Ubuntu系统(虚拟机和物理机、服务器都可以)OpenCV3.4.1压缩包OpenCVcontrib3.4.1压缩包版本信息GCC版本Ubuntu11.3.0-1ubuntu1~22.04G++版本U......
  • OpenCV Mat类详解
    1.Mat类常用成员函数和成员变量        由于Mat类使用的非常广泛,使用的形式也非常之多,这里只对较为常用的成员函数和成员变量做出了整理;1.1构造函数(1)默认构......
  • opencv官网例程(4.7.0版本)运行示例
    使用官网https://opencv.org/releases/下载4.7版本下载好后添加环境变量到安装路径下以及在系统变量中添加OpenCV_DIR(也可以用set的方式从Cmakelist里添加)打开我......
  • Opencv 仿射变换(getRotationMatrix2D)
    getRotationMatrix2D()此函数给定一个旋转中心的坐标,旋转角度和缩放因子,返回一个仿射变换矩阵,可以使用Mat接收。imagewatch示意图如上当考虑缩放因子时,计算矩阵如官......
  • opencv与anaconda冲突
    sudocmake../opencv-4.x-DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++-DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc-DCMAKE_BUILD_TYPE=RELEASE-DCMAKE_INSTALL_P......
  • Qt中的QImage类和OpenCV中的cv::Mat类之间的相互转换
    一、QImage转cv::MatQPixmappixmap=currentImage->pixmap();QImageimage=pixmap.toImage();//QImageimage("d:/dev/test.jpg");image=image.con......
  • 如何在 Nuxt 3 中使用 wavesurfer.js
    安装wavesurfer.js在项目中安装wavesurfer.jsnpminstall--savewavesurfer.js常规方式引入如果你的根目录中没有components目录则需要创建该目录,并在此目录中创......
  • 读写显示图像(opencv pyplot)
    1importcv2ascv2importnumpyasnp3frommatplotlibimportpyplotasplt45if__name__=='__main__':67#读取图片8img=cv.imrea......
  • python opencv遍历图像数据集是否存在错误
    python3.9的环境,opencv3.4:平时在准备图像数据集是,有可能其中有个别图像错误引起在深度学习训练到一半时报错,所有先检查一下数据集中的图像是否有错误图像:importosimportc......