WrapPerspectiveExample.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /// WrapPerspective Example
  11. /// An example of perspective transformation of a image using the Imgproc.warpPerspective function.
  12. /// </summary>
  13. public class WrapPerspectiveExample : MonoBehaviour
  14. {
  15. // Use this for initialization
  16. void Start ()
  17. {
  18. Texture2D inputTexture = Resources.Load ("lena") as Texture2D;
  19. Mat inputMat = new Mat (inputTexture.height, inputTexture.width, CvType.CV_8UC4);
  20. Utils.texture2DToMat (inputTexture, inputMat);
  21. Debug.Log ("inputMat.ToString() " + inputMat.ToString ());
  22. Mat src_mat = new Mat (4, 1, CvType.CV_32FC2);
  23. Mat dst_mat = new Mat (4, 1, CvType.CV_32FC2);
  24. src_mat.put (0, 0, 0.0, 0.0, inputMat.rows (), 0.0, 0.0, inputMat.cols (), inputMat.rows (), inputMat.cols ());
  25. dst_mat.put (0, 0, 0.0, 0.0, inputMat.rows (), 200.0, 0.0, inputMat.cols (), inputMat.rows (), inputMat.cols () - 200.0);
  26. Mat perspectiveTransform = Imgproc.getPerspectiveTransform (src_mat, dst_mat);
  27. Mat outputMat = inputMat.clone ();
  28. Imgproc.warpPerspective (inputMat, outputMat, perspectiveTransform, new Size (inputMat.rows (), inputMat.cols ()));
  29. Texture2D outputTexture = new Texture2D (outputMat.cols (), outputMat.rows (), TextureFormat.RGBA32, false);
  30. Utils.matToTexture2D (outputMat, outputTexture);
  31. gameObject.GetComponent<Renderer> ().material.mainTexture = outputTexture;
  32. }
  33. // Update is called once per frame
  34. void Update ()
  35. {
  36. }
  37. /// <summary>
  38. /// Raises the back button click event.
  39. /// </summary>
  40. public void OnBackButtonClick ()
  41. {
  42. SceneManager.LoadScene ("OpenCVForUnityExample");
  43. }
  44. }
  45. }