WrapPerspectiveExample.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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("face") as Texture2D;
  19. Mat inputMat = new Mat(inputTexture.height, inputTexture.width, CvType.CV_8UC4);
  20. Mat outputMat = inputMat.clone();
  21. Utils.texture2DToMat(inputTexture, inputMat);
  22. Debug.Log("inputMat.ToString() " + inputMat.ToString());
  23. Mat src_mat = new Mat(4, 1, CvType.CV_32FC2);
  24. Mat dst_mat = new Mat(4, 1, CvType.CV_32FC2);
  25. src_mat.put(0, 0, 0.0, 0.0, inputMat.cols(), 0.0, 0.0, inputMat.rows(), inputMat.cols(), inputMat.rows());
  26. dst_mat.put(0, 0, 0.0, 0.0, inputMat.cols(), 200.0, 0.0, inputMat.rows(), inputMat.cols(), inputMat.rows() - 200.0);
  27. Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src_mat, dst_mat);
  28. Debug.Log("perspectiveTransform " + perspectiveTransform.dump());
  29. Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(inputMat.cols(), inputMat.rows()));
  30. Texture2D outputTexture = new Texture2D(outputMat.cols(), outputMat.rows(), TextureFormat.RGBA32, false);
  31. Utils.matToTexture2D(outputMat, outputTexture);
  32. gameObject.GetComponent<Renderer>().material.mainTexture = outputTexture;
  33. }
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. }
  38. /// <summary>
  39. /// Raises the back button click event.
  40. /// </summary>
  41. public void OnBackButtonClick()
  42. {
  43. SceneManager.LoadScene("OpenCVForUnityExample");
  44. }
  45. }
  46. }