opencv-Features2D+Homography to find a known object

2024-04-24 15:38

本文主要是介绍opencv-Features2D+Homography to find a known object,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方地址
#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/xfeatures2d.hpp"
using namespacecv;
using namespacecv::xfeatures2d;
voidreadme();
/* @function main */
intmain(intargc,char** argv )
{
if( argc != 3 )
{ readme();return-1; }
Matimg_object =imread( argv[1],IMREAD_GRAYSCALE);
Matimg_scene =imread( argv[2],IMREAD_GRAYSCALE);
if( !img_object.data|| !img_scene.data)
{ std::cout<<" --(!) Error reading images "<< std::endl;return-1; }
//-- Step 1: Detect the keypoints and extract descriptors using SURF
intminHessian = 400;
Ptr<SURF>detector =SURF::create( minHessian );
std::vector<KeyPoint> keypoints_object, keypoints_scene;
Matdescriptors_object, descriptors_scene;
detector->detectAndCompute( img_object,Mat(), keypoints_object, descriptors_object );
detector->detectAndCompute( img_scene,Mat(), keypoints_scene, descriptors_scene );
//-- Step 2: Matching descriptor vectors using FLANN matcher
FlannBasedMatchermatcher;
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
doublemax_dist = 0;doublemin_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for(inti = 0; i < descriptors_object.rows; i++ )
{doubledist = 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(inti = 0; i < descriptors_object.rows; i++ )
{if( matches[i].distance <= 3*min_dist )
{ good_matches.push_back( matches[i]); }
}
Matimg_matches;
drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
good_matches, img_matches,Scalar::all(-1),Scalar::all(-1),
std::vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//-- Localize the object
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for(size_ti = 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 );
}
MatH =findHomography( obj, scene,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);
return0;
}
/* @function readme */
voidreadme()
{ std::cout <<" Usage: ./SURF_descriptor <img1> <img2>"<< std::endl; }








    这篇关于opencv-Features2D+Homography to find a known object的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/932138

    相关文章

    使用Python和OpenCV库实现实时颜色识别系统

    《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

    OpenCV实现实时颜色检测的示例

    《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

    Python中OpenCV与Matplotlib的图像操作入门指南

    《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

    C/C++中OpenCV 矩阵运算的实现

    《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元

    C/C++的OpenCV 进行图像梯度提取的几种实现

    《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

    C/C++和OpenCV实现调用摄像头

    《C/C++和OpenCV实现调用摄像头》本文主要介绍了C/C++和OpenCV实现调用摄像头,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录准备工作1. 打开摄像头2. 读取视频帧3. 显示视频帧4. 释放资源5. 获取和设置摄像头属性

    c/c++的opencv图像金字塔缩放实现

    《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O

    c/c++的opencv实现图片膨胀

    《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

    qtcreater配置opencv遇到的坑及实践记录

    《qtcreater配置opencv遇到的坑及实践记录》我配置opencv不管是按照网上的教程还是deepseek发现都有些问题,下面是我的配置方法以及实践成功的心得,感兴趣的朋友跟随小编一起看看吧... 目录电脑环境下载环境变量配置qmake加入外部库测试配置我配置opencv不管是按照网上的教程还是de

    python+OpenCV反投影图像的实现示例详解

    《python+OpenCV反投影图像的实现示例详解》:本文主要介绍python+OpenCV反投影图像的实现示例详解,本文通过实例代码图文并茂的形式给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前言二、什么是反投影图像三、反投影图像的概念四、反向投影的工作原理一、利用反向投影backproj