ThresholdExample.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.ImgprocModule;
  6. using OpenCVForUnity.UnityUtils;
  7. namespace OpenCVForUnityExample
  8. {
  9. /// <summary>
  10. /// Threshold Example
  11. /// An example of image binarization using the Imgproc.threshold function.
  12. /// http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
  13. /// </summary>
  14. public class ThresholdExample : MonoBehaviour
  15. {
  16. // Use this for initialization
  17. void Start ()
  18. {
  19. Texture2D imgTexture = Resources.Load ("chessboard") as Texture2D;
  20. Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC1);
  21. Utils.texture2DToMat (imgTexture, imgMat);
  22. Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
  23. Imgproc.threshold (imgMat, imgMat, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
  24. Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
  25. Utils.matToTexture2D (imgMat, texture);
  26. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  27. }
  28. // Update is called once per frame
  29. void Update ()
  30. {
  31. }
  32. /// <summary>
  33. /// Raises the back button click event.
  34. /// </summary>
  35. public void OnBackButtonClick ()
  36. {
  37. SceneManager.LoadScene ("OpenCVForUnityExample");
  38. }
  39. }
  40. }