MatchShapesExample.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. {
  36. Imgproc.drawContours(dstMat, srcContours, i, new Scalar(255, 0, 0), 2, 8, srcHierarchy, 0, new Point());
  37. }
  38. for (int i = 0; i < srcContours.Count; i++)
  39. {
  40. double returnVal = Imgproc.matchShapes(srcContours[1], srcContours[i], Imgproc.CV_CONTOURS_MATCH_I1, 0);
  41. Debug.Log("returnVal " + i + " " + returnVal);
  42. Point point = new Point();
  43. float[] radius = new float[1];
  44. Imgproc.minEnclosingCircle(new MatOfPoint2f(srcContours[i].toArray()), point, radius);
  45. Debug.Log("point.ToString() " + point.ToString());
  46. Debug.Log("radius.ToString() " + radius[0]);
  47. Imgproc.circle(dstMat, point, 5, new Scalar(0, 0, 255), -1);
  48. Imgproc.putText(dstMat, " " + returnVal, point, Imgproc.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar(0, 255, 0), 1, Imgproc.LINE_AA, false);
  49. }
  50. Texture2D texture = new Texture2D(dstMat.cols(), dstMat.rows(), TextureFormat.RGBA32, false);
  51. Utils.matToTexture2D(dstMat, texture);
  52. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  53. }
  54. // Update is called once per frame
  55. void Update()
  56. {
  57. }
  58. /// <summary>
  59. /// Raises the back button click event.
  60. /// </summary>
  61. public void OnBackButtonClick()
  62. {
  63. SceneManager.LoadScene("OpenCVForUnityExample");
  64. }
  65. }
  66. }