ConnectedComponentsExample.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. {
  38. colors.Add(new Scalar(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255)));
  39. }
  40. // draw labels
  41. for (int i = 0; i < dstMat.rows(); ++i)
  42. {
  43. for (int j = 0; j < dstMat.cols(); ++j)
  44. {
  45. Scalar color = colors[(int)labels.get(i, j)[0]];
  46. dstMat.put(i, j, color.val[0], color.val[1], color.val[2]);
  47. }
  48. }
  49. // draw rectangle
  50. for (int i = 1; i < total; ++i)
  51. {
  52. int x = (int)stats.get(i, Imgproc.CC_STAT_LEFT)[0];
  53. int y = (int)stats.get(i, Imgproc.CC_STAT_TOP)[0];
  54. int height = (int)stats.get(i, Imgproc.CC_STAT_HEIGHT)[0];
  55. int width = (int)stats.get(i, Imgproc.CC_STAT_WIDTH)[0];
  56. OpenCVForUnity.CoreModule.Rect rect = new OpenCVForUnity.CoreModule.Rect(x, y, width, height);
  57. Imgproc.rectangle(dstMat, rect.tl(), rect.br(), new Scalar(0, 255, 0), 2);
  58. }
  59. // draw centroids
  60. for (int i = 1; i < total; ++i)
  61. {
  62. int x = (int)centroids.get(i, 0)[0];
  63. int y = (int)centroids.get(i, 1)[0];
  64. Imgproc.circle(dstMat, new Point(x, y), 3, new Scalar(255, 0, 0), -1);
  65. }
  66. // draw index of label
  67. for (int i = 1; i < total; ++i)
  68. {
  69. int x = (int)stats.get(i, Imgproc.CC_STAT_LEFT)[0];
  70. int y = (int)stats.get(i, Imgproc.CC_STAT_TOP)[0];
  71. Imgproc.putText(dstMat, "" + i, new Point(x + 5, y + 15), Imgproc.FONT_HERSHEY_COMPLEX, 0.5, new Scalar(255, 255, 0), 2);
  72. }
  73. Texture2D texture = new Texture2D(dstMat.cols(), dstMat.rows(), TextureFormat.RGBA32, false);
  74. Utils.matToTexture2D(dstMat, texture);
  75. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  76. }
  77. // Update is called once per frame
  78. void Update()
  79. {
  80. }
  81. /// <summary>
  82. /// Raises the back button click event.
  83. /// </summary>
  84. public void OnBackButtonClick()
  85. {
  86. SceneManager.LoadScene("OpenCVForUnityExample");
  87. }
  88. }
  89. }