Texture2DToMatExample.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.UnityUtils;
  6. namespace OpenCVForUnityExample
  7. {
  8. /// <summary>
  9. /// Texture2DToMat Example
  10. /// An example of converting a Texture2D image to OpenCV's Mat format.
  11. /// </summary>
  12. public class Texture2DToMatExample : MonoBehaviour
  13. {
  14. // Use this for initialization
  15. void Start ()
  16. {
  17. //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
  18. Utils.setDebugMode (true);
  19. Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
  20. Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);
  21. Utils.texture2DToMat (imgTexture, imgMat);
  22. Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
  23. Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
  24. Utils.matToTexture2D (imgMat, texture);
  25. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  26. Utils.setDebugMode (false);
  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. }