ConnectedComponentsExample.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /// ConnectedComponents Example
  11. /// An example of Connected-component labeling using the Imgproc.connectedComponentsWithStats function.
  12. /// Referring to http://qiita.com/wakaba130/items/9d921b8b3eb812e4f197.
  13. /// </summary>
  14. public class ConnectedComponentsExample : MonoBehaviour
  15. {
  16. // Use this for initialization
  17. void Start ()
  18. {
  19. Texture2D imgTexture = Resources.Load ("matchshapes") as Texture2D;
  20. Mat srcMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC1);
  21. Utils.texture2DToMat (imgTexture, srcMat);
  22. Debug.Log ("srcMat.ToString() " + srcMat.ToString ());
  23. Mat dstMat = new Mat (srcMat.size (), CvType.CV_8UC3);
  24. //labeling
  25. Mat labels = new Mat ();
  26. Mat stats = new Mat ();
  27. Mat centroids = new Mat ();
  28. int total = Imgproc.connectedComponentsWithStats (srcMat, labels, stats, centroids);
  29. Debug.Log ("labels.ToString() " + labels.ToString ());
  30. Debug.Log ("stats.ToString() " + stats.ToString ());
  31. Debug.Log ("centroids.ToString() " + centroids.ToString ());
  32. Debug.Log ("total " + total);
  33. // determine drawing color
  34. List<Scalar> colors = new List<Scalar> (total);
  35. colors.Add (new Scalar (0, 0, 0));
  36. for (int i = 1; i < total; ++i) {
  37. colors.Add (new Scalar (Random.Range (0, 255), Random.Range (0, 255), Random.Range (0, 255)));
  38. }
  39. // draw labels
  40. for (int i = 0; i < dstMat.rows (); ++i) {
  41. for (int j = 0; j < dstMat.cols (); ++j) {
  42. Scalar color = colors [(int)labels.get (i, j) [0]];
  43. dstMat.put (i, j, color.val [0], color.val [1], color.val [2]);
  44. }
  45. }
  46. // draw rectangle
  47. for (int i = 1; i < total; ++i) {
  48. int x = (int)stats.get (i, Imgproc.CC_STAT_LEFT) [0];
  49. int y = (int)stats.get (i, Imgproc.CC_STAT_TOP) [0];
  50. int height = (int)stats.get (i, Imgproc.CC_STAT_HEIGHT) [0];
  51. int width = (int)stats.get (i, Imgproc.CC_STAT_WIDTH) [0];
  52. OpenCVForUnity.CoreModule.Rect rect = new OpenCVForUnity.CoreModule.Rect (x, y, width, height);
  53. Imgproc.rectangle (dstMat, rect.tl (), rect.br (), new Scalar (0, 255, 0), 2);
  54. }
  55. // draw centroids
  56. for (int i = 1; i < total; ++i) {
  57. int x = (int)centroids.get (i, 0) [0];
  58. int y = (int)centroids.get (i, 1) [0];
  59. Imgproc.circle (dstMat, new Point (x, y), 3, new Scalar (255, 0, 0), -1);
  60. }
  61. // draw index of label
  62. for (int i = 1; i < total; ++i) {
  63. int x = (int)stats.get (i, Imgproc.CC_STAT_LEFT) [0];
  64. int y = (int)stats.get (i, Imgproc.CC_STAT_TOP) [0];
  65. Imgproc.putText (dstMat, "" + i, new Point (x + 5, y + 15), Imgproc.FONT_HERSHEY_COMPLEX, 0.5, new Scalar (255, 255, 0), 2);
  66. }
  67. Texture2D texture = new Texture2D (dstMat.cols (), dstMat.rows (), TextureFormat.RGBA32, false);
  68. Utils.matToTexture2D (dstMat, texture);
  69. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  70. }
  71. // Update is called once per frame
  72. void Update ()
  73. {
  74. }
  75. /// <summary>
  76. /// Raises the back button click event.
  77. /// </summary>
  78. public void OnBackButtonClick ()
  79. {
  80. SceneManager.LoadScene ("OpenCVForUnityExample");
  81. }
  82. }
  83. }