MatchShapesExample.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections.Generic;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.ImgprocModule;
  6. using OpenCVForUnity.UnityUtils;
  7. namespace OpenCVForUnityExample
  8. {
  9. /// <summary>
  10. /// MatchShapes Example
  11. /// http://docs.opencv.org/3.1.0/d5/d45/tutorial_py_contours_more_functions.html
  12. /// </summary>
  13. public class MatchShapesExample : MonoBehaviour
  14. {
  15. // Use this for initialization
  16. void Start ()
  17. {
  18. //srcMat
  19. Texture2D srcTexture = Resources.Load ("matchshapes") as Texture2D;
  20. Mat srcMat = new Mat (srcTexture.height, srcTexture.width, CvType.CV_8UC1);
  21. Utils.texture2DToMat (srcTexture, srcMat);
  22. Debug.Log ("srcMat.ToString() " + srcMat.ToString ());
  23. Imgproc.threshold (srcMat, srcMat, 127, 255, Imgproc.THRESH_BINARY);
  24. //dstMat
  25. Texture2D dstTexture = Resources.Load ("matchshapes") as Texture2D;
  26. Mat dstMat = new Mat (dstTexture.height, dstTexture.width, CvType.CV_8UC3);
  27. Utils.texture2DToMat (dstTexture, dstMat);
  28. Debug.Log ("dstMat.ToString() " + dstMat.ToString ());
  29. List<MatOfPoint> srcContours = new List<MatOfPoint> ();
  30. Mat srcHierarchy = new Mat ();
  31. /// Find srcContours
  32. Imgproc.findContours (srcMat, srcContours, srcHierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_NONE);
  33. Debug.Log ("srcContours.Count " + srcContours.Count);
  34. for (int i = 0; i < srcContours.Count; i++) {
  35. Imgproc.drawContours (dstMat, srcContours, i, new Scalar (255, 0, 0), 2, 8, srcHierarchy, 0, new Point ());
  36. }
  37. for (int i = 0; i < srcContours.Count; i++) {
  38. double returnVal = Imgproc.matchShapes (srcContours [1], srcContours [i], Imgproc.CV_CONTOURS_MATCH_I1, 0);
  39. Debug.Log ("returnVal " + i + " " + returnVal);
  40. Point point = new Point ();
  41. float[] radius = new float[1];
  42. Imgproc.minEnclosingCircle (new MatOfPoint2f (srcContours [i].toArray ()), point, radius);
  43. Debug.Log ("point.ToString() " + point.ToString ());
  44. Debug.Log ("radius.ToString() " + radius [0]);
  45. Imgproc.circle (dstMat, point, 5, new Scalar (0, 0, 255), -1);
  46. Imgproc.putText (dstMat, " " + returnVal, point, Imgproc.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar (0, 255, 0), 1, Imgproc.LINE_AA, false);
  47. }
  48. Texture2D texture = new Texture2D (dstMat.cols (), dstMat.rows (), TextureFormat.RGBA32, false);
  49. Utils.matToTexture2D (dstMat, texture);
  50. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  51. }
  52. // Update is called once per frame
  53. void Update ()
  54. {
  55. }
  56. /// <summary>
  57. /// Raises the back button click event.
  58. /// </summary>
  59. public void OnBackButtonClick ()
  60. {
  61. SceneManager.LoadScene ("OpenCVForUnityExample");
  62. }
  63. }
  64. }