ThinPlateSplineShapeTransformerExample.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.ImgprocModule;
  6. using OpenCVForUnity.ShapeModule;
  7. using OpenCVForUnity.UnityUtils;
  8. namespace OpenCVForUnitySample
  9. {
  10. /// <summary>
  11. /// ThinPlateSplineShapeTransformer Example
  12. /// An example of Thin Plate Spline (TPS) Warping using the ThinPlateSplineShapeTransformer class.
  13. /// </summary>
  14. public class ThinPlateSplineShapeTransformerExample : MonoBehaviour
  15. {
  16. // Use this for initialization
  17. void Start ()
  18. {
  19. //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
  20. Utils.setDebugMode (true);
  21. Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
  22. Mat img = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);
  23. Utils.texture2DToMat (imgTexture, img);
  24. Debug.Log ("imgMat.ToString() " + img.ToString ());
  25. OpenCVForUnity.ShapeModule.ThinPlateSplineShapeTransformer tps = Shape.createThinPlateSplineShapeTransformer (0);
  26. MatOfPoint2f sourcePoints = new MatOfPoint2f (
  27. new Point (0, 0),
  28. new Point (512, 0),
  29. new Point (0, 512),
  30. new Point (250, 200),
  31. new Point (400, 400),
  32. new Point (200, 400),
  33. new Point (512, 512)
  34. );
  35. MatOfPoint2f targetPoints = new MatOfPoint2f (
  36. new Point (0, 0),
  37. new Point (512, 0),
  38. new Point (0, 599),
  39. new Point (250, 120),
  40. new Point (450, 450),
  41. new Point (100, 450),
  42. new Point (512, 512)
  43. );
  44. MatOfDMatch matches = new MatOfDMatch (
  45. new DMatch (0, 0, 0),
  46. new DMatch (1, 1, 0),
  47. new DMatch (2, 2, 0),
  48. new DMatch (3, 3, 0),
  49. new DMatch (4, 4, 0),
  50. new DMatch (5, 5, 0),
  51. new DMatch (6, 6, 0)
  52. );
  53. //http://stackoverflow.com/questions/32207085/shape-transformers-and-interfaces-opencv3-0
  54. Core.transpose (sourcePoints, sourcePoints);
  55. Core.transpose (targetPoints, targetPoints);
  56. Debug.Log ("sourcePoints " + sourcePoints.ToString ());
  57. Debug.Log ("targetPoints " + targetPoints.ToString ());
  58. tps.estimateTransformation (targetPoints, sourcePoints, matches);
  59. MatOfPoint2f transPoints = new MatOfPoint2f ();
  60. tps.applyTransformation (sourcePoints, transPoints);
  61. Debug.Log ("sourcePoints " + sourcePoints.dump ());
  62. Debug.Log ("targetPoints " + targetPoints.dump ());
  63. Debug.Log ("transPoints " + transPoints.dump ());
  64. Mat res = new Mat ();
  65. tps.warpImage (img, res);
  66. //plot points
  67. Point[] sourcePointsArray = sourcePoints.toArray ();
  68. Point[] targetPointsArray = targetPoints.toArray ();
  69. for (int i = 0; i < sourcePointsArray.Length; i++) {
  70. Imgproc.arrowedLine (res, sourcePointsArray [i], targetPointsArray [i], new Scalar (255, 255, 0, 255), 3, Imgproc.LINE_AA, 0, 0.2);
  71. Imgproc.circle (res, sourcePointsArray [i], 10, new Scalar (255, 0, 0, 255), -1);
  72. Imgproc.circle (res, targetPointsArray [i], 10, new Scalar (0, 0, 255, 255), -1);
  73. }
  74. Texture2D texture = new Texture2D (res.cols (), res.rows (), TextureFormat.RGBA32, false);
  75. Utils.matToTexture2D (res, texture);
  76. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  77. Utils.setDebugMode (false);
  78. }
  79. // Update is called once per frame
  80. void Update ()
  81. {
  82. }
  83. /// <summary>
  84. /// Raises the back button click event.
  85. /// </summary>
  86. public void OnBackButtonClick ()
  87. {
  88. SceneManager.LoadScene ("OpenCVForUnityExample");
  89. }
  90. }
  91. }