前言
遇到一个需要匹配图片的需求,就是给定一张图片,然后打开摄像头实时获取画面,如果画面中出现给定图片,则出现视觉效果。
本文只记录图片匹配方法。
感谢会思考的猴子的鼎力相助。
效果
这里
相似度 < 15 的点有多少: 77
中的15
需要根据实际情况进行修改,值越低说明越相似。
匹配的点
越多,则说明图片相似特征越多。
源码
原理在注释中
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.Features2dModule;
using OpenCVForUnity.UnityUtils;
using System.Linq;
using System.Collections.Generic;
namespace OpenCVForUnityExample
{
/// <summary>
/// 2D特征提取
/// </summary>
public class Feature2DTestExample : MonoBehaviour
{
void Start()
{
//加载第一张图
Texture2D imgTexture = Resources.Load("MainT") as Texture2D;
Mat img1Mat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3);
Utils.texture2DToMat(imgTexture, img1Mat);
Debug.Log("img1Mat.ToString() " + img1Mat.ToString());
//加载第二张图
Texture2D tempTexture = Resources.Load("MainT2") as Texture2D;
Mat img2Mat = new Mat(tempTexture.height, tempTexture.width, CvType.CV_8UC3);
Utils.texture2DToMat(tempTexture, img2Mat);
Debug.Log("img2Mat.ToString() " + img2Mat.ToString());
//随机一个旋转角度
float angle = UnityEngine.Random.Range(0, 360), scale = 1.0f;
//拿到图片中心点
Point center = new Point(img2Mat.cols() * 0.5f, img2Mat.rows() * 0.5f);
//获取旋转矩阵
Mat affine_matrix = Imgproc.getRotationMatrix2D(center, angle, scale);
//图像仿射变换
Imgproc.warpAffine(img2Mat, img2Mat, affine_matrix, img2Mat.size());
//创建探测器和提取器
ORB detector = ORB.create();
ORB extractor = ORB.create();
//关键点1
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
//描述Mat1
Mat descriptors1 = new Mat();
//识别并计算 第一张图
detector.detect(img1Mat, keypoints1);
extractor.compute(img1Mat, keypoints1, descriptors1);
//关键点2
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
//描述Mat2
Mat descriptors2 = new Mat();
//识别并计算 第二张图
detector.detect(img2Mat, keypoints2);
extractor.compute(img2Mat, keypoints2, descriptors2);
//创建描述符匹配器
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
//匹配计算结果容器
MatOfDMatch matches = new MatOfDMatch();
//用匹配器计算描述符
matcher.match(descriptors1, descriptors2, matches);
//统计出distance(相似程度)小于15的所有点 这里其实不需要排序
int count = matches.toList().OrderBy(_ => _.distance).Count(_=>_.distance<15);
//matches.toList().ForEach(_=>Debug.Log(_.distance));
Debug.Log("相似度 < 15 的点有多少: " + count);
//如果小于5个点认定为不相似
if(count<5){
return;
}
//这里还原一下,之后画图用
matches.fromList(matches.toList().OrderBy(_ => _.distance).Where(_=>_.distance<15).ToList());
//结果图
Mat resultImg = new Mat();
//绘制匹配点图
Features2d.drawMatches(img1Mat, keypoints1, img2Mat, keypoints2, matches, resultImg);
//创建一个Texture2D
Texture2D texture = new Texture2D(resultImg.cols(), resultImg.rows(), TextureFormat.RGBA32, false);
//转换
Utils.matToTexture2D(resultImg, texture);
//贴图
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
}
}
}