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

3 篇文章 0 订阅

C:\Windows\System32目录需要添加
opencv库
需要添加引用三个库:
opencv_xfeatures2d341d.lib
opencv_world341d.lib
opencv_calib3d341d.lib

#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"
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;

void readme();

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.empty() || img_scene.empty())
	{
		cout << "Image is invalid" << endl;
		return -1;
	}
	int minHessian = 400;
	Ptr<SURF> detector = SURF::create(minHessian);
	vector<KeyPoint> keypoints_src, keypoints_dst;
	Mat descriptor_object, descriptor_scene;
	detector->detectAndCompute(img_object, Mat(), keypoints_src, descriptor_object);
	detector->detectAndCompute(img_scene, Mat(), keypoints_dst, descriptor_scene);
	FlannBasedMatcher matcher;
	vector<DMatch> matches;
	matcher.match(descriptor_object, descriptor_scene, matches);
	// find good matched points
	double minDist = 100, maxDist = 0;
	for (int i = 0; i < descriptor_object.rows; i++)
	{
		double dist = matches[i].distance;
		if (dist > maxDist)
			maxDist = dist;
		if (dist < minDist)
			minDist = dist;
	}
	cout << "--Max dist : " << maxDist << endl;
	cout << "--Min dist : " << minDist << endl;


	vector<DMatch> goodMatches;
	for (int i = 0; i < descriptor_object.rows; i++)
	{
		double dist = matches[i].distance;
		if (dist < max(3 * minDist, 0.02))
		{
			goodMatches.push_back(matches[i]);
		}
	}

	Mat img_matches;
	drawMatches(img_object, keypoints_src, img_scene, keypoints_dst, goodMatches, img_matches, Scalar::all(-1), 
		Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
	
	vector<Point2f> obj;
	vector<Point2f> scene;
	for (int i = 0; i < goodMatches.size(); i++)
	{
		obj.push_back(keypoints_src[goodMatches[i].queryIdx].pt);
		scene.push_back(keypoints_dst[goodMatches[i].trainIdx].pt);
	}
	Mat H = findHomography(obj, scene, CV_RANSAC);

	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);

	vector<Point2f> scene_corners(4);
	perspectiveTransform(obj_corners, scene_corners, H);

	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);
	imshow("Good Matches & Object detection", img_matches);
	waitKey(0);
	return 0;
}

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

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值