SeamlessCloneExample.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.PhotoModule;
  6. using OpenCVForUnity.UnityUtils;
  7. namespace OpenCVForUnityExample
  8. {
  9. /// <summary>
  10. /// SeamlessClone Example
  11. /// An example of seamless photo synthesis using the Photo.seamlessClone function.
  12. /// </summary>
  13. public class SeamlessCloneExample : MonoBehaviour
  14. {
  15. // Use this for initialization
  16. void Start ()
  17. {
  18. Texture2D srcTexture = Resources.Load ("template") as Texture2D;
  19. Texture2D dstTexture = Resources.Load ("lena") as Texture2D;
  20. Mat src = new Mat (srcTexture.height, srcTexture.width, CvType.CV_8UC3);
  21. Mat dst = new Mat (dstTexture.height, dstTexture.width, CvType.CV_8UC3);
  22. Utils.texture2DToMat (srcTexture, src);
  23. Utils.texture2DToMat (dstTexture, dst);
  24. Mat mask = new Mat (src.rows (), src.cols (), CvType.CV_8UC1, new Scalar (255));
  25. Mat result = new Mat ();
  26. Point point = new Point (315, 450);
  27. Photo.seamlessClone (src, dst, mask, point, result, Photo.NORMAL_CLONE);
  28. Debug.Log ("result ToString " + result.ToString ());
  29. Texture2D texture = new Texture2D (result.cols (), result.rows (), TextureFormat.RGBA32, false);
  30. Utils.matToTexture2D (result, texture);
  31. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  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. }