Advertisement
Guest User

FeatureExample

a guest
Feb 13th, 2020
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.ImgprocModule;
  6. using OpenCVForUnity.Features2dModule;
  7. using OpenCVForUnity.UnityUtils;
  8. using System.Collections.Generic;
  9.  
  10. namespace OpenCVForUnityExample
  11. {
  12.     /// <summary>
  13.     /// Feature2D Example
  14.     /// An example of descriptor extraction and descriptor matching.
  15.     /// http://docs.opencv.org/3.1.0/d5/dde/tutorial_feature_description.html
  16.     /// </summary>
  17.     public class Feature2DExample : MonoBehaviour
  18.     {
  19.         // Use this for initialization
  20.         void Update ()
  21.         {
  22.             Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
  23.            
  24.             Mat img1Mat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC3);
  25.             Utils.texture2DToMat (imgTexture, img1Mat);
  26.             Debug.Log ("img1Mat.ToString() " + img1Mat.ToString ());
  27.  
  28.             Mat img2Mat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC3);
  29.             Utils.texture2DToMat (imgTexture, img2Mat);
  30.             Debug.Log ("img2Mat.ToString() " + img2Mat.ToString ());
  31.  
  32.  
  33.             float angle = UnityEngine.Random.Range (0, 360), scale = 1.0f;
  34.  
  35.             Point center = new Point (img2Mat.cols () * 0.5f, img2Mat.rows () * 0.5f);
  36.  
  37.             Mat affine_matrix = Imgproc.getRotationMatrix2D (center, angle, scale);
  38.  
  39.             Imgproc.warpAffine (img1Mat, img2Mat, affine_matrix, img2Mat.size ());
  40.  
  41.  
  42.             ORB detector = ORB.create ();
  43.             ORB extractor = ORB.create ();
  44.  
  45.             MatOfKeyPoint keypoints1 = new MatOfKeyPoint ();
  46.             Mat descriptors1 = new Mat ();
  47.  
  48.             detector.detect (img1Mat, keypoints1);
  49.             extractor.compute (img1Mat, keypoints1, descriptors1);
  50.  
  51.             MatOfKeyPoint keypoints2 = new MatOfKeyPoint ();
  52.             Mat descriptors2 = new Mat ();
  53.        
  54.             detector.detect (img2Mat, keypoints2);
  55.             extractor.compute (img2Mat, keypoints2, descriptors2);
  56.  
  57.  
  58.             DescriptorMatcher matcher = DescriptorMatcher.create (DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
  59.             MatOfDMatch matches = new MatOfDMatch ();
  60.  
  61.             List<MatOfDMatch> knnMatches = new List<MatOfDMatch>();
  62.             matcher.knnMatch (descriptors1, descriptors2, knnMatches, 2);
  63.  
  64.             var arrayKnnMatches = knnMatches.ToArray();
  65.             List<DMatch> goodMatches = new List<DMatch>();
  66.             for (int i = 0; i < arrayKnnMatches.Length; ++i)
  67.             {
  68.                 var array = arrayKnnMatches[i].toArray();
  69.                 if (array[0].distance < 0.7 * array[1].distance)
  70.                 {
  71.                     goodMatches.Add(array[0]);
  72.                 }
  73.             }
  74.  
  75.             Mat resultImg = new Mat ();
  76.             matches.fromArray(goodMatches.ToArray());
  77.  
  78.             Features2d.drawMatches (img1Mat, keypoints1, img2Mat, keypoints2, matches, resultImg);
  79.  
  80.  
  81.             Texture2D texture = new Texture2D (resultImg.cols (), resultImg.rows (), TextureFormat.RGBA32, false);
  82.        
  83.             Utils.matToTexture2D (resultImg, texture);
  84.  
  85.             gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  86.         }
  87.    
  88.         // Update is called once per frame
  89.  
  90.  
  91.         /// <summary>
  92.         /// Raises the back button click event.
  93.         /// </summary>
  94.         public void OnBackButtonClick ()
  95.         {
  96.             SceneManager.LoadScene ("OpenCVForUnityExample");
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement