MSERExample.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using OpenCVForUnity.CoreModule;
  6. using OpenCVForUnity.Features2dModule;
  7. using OpenCVForUnity.ImgprocModule;
  8. using OpenCVForUnity.UnityUtils;
  9. namespace OpenCVForUnityExample
  10. {
  11. /// <summary>
  12. /// MSER Example
  13. /// An example of region detection using the MSER extraction algorithm.
  14. /// </summary>
  15. public class MSERExample : MonoBehaviour
  16. {
  17. // Use this for initialization
  18. void Start ()
  19. {
  20. Texture2D imgTexture = Resources.Load ("chessboard") as Texture2D;
  21. Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC3);
  22. Utils.texture2DToMat (imgTexture, imgMat);
  23. Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
  24. MSER mserExtractor = MSER.create ();
  25. mserExtractor.setDelta (5);
  26. mserExtractor.setMinArea (60);
  27. mserExtractor.setMaxArea (14400);
  28. List<MatOfPoint> mserContours = new List<MatOfPoint> ();
  29. MatOfRect mserBbox = new MatOfRect ();
  30. mserExtractor.detectRegions (imgMat, mserContours, mserBbox);
  31. for (int i = 0; i < mserContours.Count; i++) {
  32. Imgproc.drawContours (imgMat, mserContours, i, new Scalar (Random.Range (0, 255), Random.Range (0, 255), Random.Range (0, 255)), 4);
  33. }
  34. Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
  35. Utils.matToTexture2D (imgMat, texture);
  36. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  37. }
  38. // Update is called once per frame
  39. void Update ()
  40. {
  41. }
  42. /// <summary>
  43. /// Raises the back button click event.
  44. /// </summary>
  45. public void OnBackButtonClick ()
  46. {
  47. SceneManager.LoadScene ("OpenCVForUnityExample");
  48. }
  49. }
  50. }