InpaintExample.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /// Inpaint Example
  11. /// An example of image restoration using the Photo.inpaint function.
  12. /// http://docs.opencv.org/trunk/df/d3d/tutorial_py_inpainting.html
  13. /// </summary>
  14. public class InpaintExample : MonoBehaviour
  15. {
  16. // Use this for initialization
  17. void Start ()
  18. {
  19. Texture2D srcTexture = Resources.Load ("lena") as Texture2D;
  20. Mat srcMat = new Mat (srcTexture.height, srcTexture.width, CvType.CV_8UC3);
  21. Utils.texture2DToMat (srcTexture, srcMat);
  22. Debug.Log ("srcMat.ToString() " + srcMat.ToString ());
  23. Texture2D maskTexture = Resources.Load ("lena_inpaint_mask") as Texture2D;
  24. Mat maskMat = new Mat (maskTexture.height, maskTexture.width, CvType.CV_8UC1);
  25. Utils.texture2DToMat (maskTexture, maskMat);
  26. Debug.Log ("maskMat.ToString() " + maskMat.ToString ());
  27. Mat dstMat = new Mat (srcMat.rows (), srcMat.cols (), CvType.CV_8UC3);
  28. Photo.inpaint (srcMat, maskMat, dstMat, 5, Photo.INPAINT_NS);
  29. Texture2D texture = new Texture2D (dstMat.cols (), dstMat.rows (), TextureFormat.RGBA32, false);
  30. Utils.matToTexture2D (dstMat, 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. }