OpenCVForUnity 根据特征提取匹配图片

10 篇文章 3 订阅

OpenCVForUnity 根据特征提取匹配图片

前言

遇到一个需要匹配图片的需求,就是给定一张图片,然后打开摄像头实时获取画面,如果画面中出现给定图片,则出现视觉效果。
本文只记录图片匹配方法。

感谢会思考的猴子的鼎力相助。

效果

这里相似度 < 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;
        }
    }
}
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值