ContourFitting.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.XimgprocModule
  7. {
  8. // C++: class ContourFitting
  9. /**
  10. * Class for ContourFitting algorithms.
  11. * ContourFitting match two contours \( z_a \) and \( z_b \) minimizing distance
  12. * \( d(z_a,z_b)=\sum (a_n - s b_n e^{j(n \alpha +\phi )})^2 \) where \( a_n \) and \( b_n \) are Fourier descriptors of \( z_a \) and \( z_b \) and s is a scaling factor and \( \phi \) is angle rotation and \( \alpha \) is starting point factor adjustement
  13. */
  14. public class ContourFitting : Algorithm
  15. {
  16. protected override void Dispose(bool disposing)
  17. {
  18. try
  19. {
  20. if (disposing)
  21. {
  22. }
  23. if (IsEnabledDispose)
  24. {
  25. if (nativeObj != IntPtr.Zero)
  26. ximgproc_ContourFitting_delete(nativeObj);
  27. nativeObj = IntPtr.Zero;
  28. }
  29. }
  30. finally
  31. {
  32. base.Dispose(disposing);
  33. }
  34. }
  35. protected internal ContourFitting(IntPtr addr) : base(addr) { }
  36. // internal usage only
  37. public static new ContourFitting __fromPtr__(IntPtr addr) { return new ContourFitting(addr); }
  38. //
  39. // C++: void cv::ximgproc::ContourFitting::estimateTransformation(Mat src, Mat dst, Mat& alphaPhiST, double& dist, bool fdContour = false)
  40. //
  41. /**
  42. * Fit two closed curves using fourier descriptors. More details in CITE: PersoonFu1977 and CITE: BergerRaghunathan1998
  43. *
  44. * param src Contour defining first shape.
  45. * param dst Contour defining second shape (Target).
  46. * param alphaPhiST : \( \alpha \)=alphaPhiST(0,0), \( \phi \)=alphaPhiST(0,1) (in radian), s=alphaPhiST(0,2), Tx=alphaPhiST(0,3), Ty=alphaPhiST(0,4) rotation center
  47. * param dist distance between src and dst after matching.
  48. * param fdContour false then src and dst are contours and true src and dst are fourier descriptors.
  49. */
  50. public void estimateTransformation(Mat src, Mat dst, Mat alphaPhiST, double[] dist, bool fdContour)
  51. {
  52. ThrowIfDisposed();
  53. if (src != null) src.ThrowIfDisposed();
  54. if (dst != null) dst.ThrowIfDisposed();
  55. if (alphaPhiST != null) alphaPhiST.ThrowIfDisposed();
  56. double[] dist_out = new double[1];
  57. ximgproc_ContourFitting_estimateTransformation_10(nativeObj, src.nativeObj, dst.nativeObj, alphaPhiST.nativeObj, dist_out, fdContour);
  58. if (dist != null) dist[0] = (double)dist_out[0];
  59. }
  60. /**
  61. * Fit two closed curves using fourier descriptors. More details in CITE: PersoonFu1977 and CITE: BergerRaghunathan1998
  62. *
  63. * param src Contour defining first shape.
  64. * param dst Contour defining second shape (Target).
  65. * param alphaPhiST : \( \alpha \)=alphaPhiST(0,0), \( \phi \)=alphaPhiST(0,1) (in radian), s=alphaPhiST(0,2), Tx=alphaPhiST(0,3), Ty=alphaPhiST(0,4) rotation center
  66. * param dist distance between src and dst after matching.
  67. */
  68. public void estimateTransformation(Mat src, Mat dst, Mat alphaPhiST, double[] dist)
  69. {
  70. ThrowIfDisposed();
  71. if (src != null) src.ThrowIfDisposed();
  72. if (dst != null) dst.ThrowIfDisposed();
  73. if (alphaPhiST != null) alphaPhiST.ThrowIfDisposed();
  74. double[] dist_out = new double[1];
  75. ximgproc_ContourFitting_estimateTransformation_11(nativeObj, src.nativeObj, dst.nativeObj, alphaPhiST.nativeObj, dist_out);
  76. if (dist != null) dist[0] = (double)dist_out[0];
  77. }
  78. //
  79. // C++: void cv::ximgproc::ContourFitting::setCtrSize(int n)
  80. //
  81. /**
  82. * set number of Fourier descriptors used in estimateTransformation
  83. *
  84. * param n number of Fourier descriptors equal to number of contour points after resampling.
  85. */
  86. public void setCtrSize(int n)
  87. {
  88. ThrowIfDisposed();
  89. ximgproc_ContourFitting_setCtrSize_10(nativeObj, n);
  90. }
  91. //
  92. // C++: void cv::ximgproc::ContourFitting::setFDSize(int n)
  93. //
  94. /**
  95. * set number of Fourier descriptors when estimateTransformation used vector<Point>
  96. *
  97. * param n number of fourier descriptors used for optimal curve matching.
  98. */
  99. public void setFDSize(int n)
  100. {
  101. ThrowIfDisposed();
  102. ximgproc_ContourFitting_setFDSize_10(nativeObj, n);
  103. }
  104. //
  105. // C++: int cv::ximgproc::ContourFitting::getCtrSize()
  106. //
  107. /**
  108. * return number of fourier descriptors
  109. */
  110. public int getCtrSize()
  111. {
  112. ThrowIfDisposed();
  113. return ximgproc_ContourFitting_getCtrSize_10(nativeObj);
  114. }
  115. //
  116. // C++: int cv::ximgproc::ContourFitting::getFDSize()
  117. //
  118. /**
  119. * return number of fourier descriptors used for optimal curve matching
  120. */
  121. public int getFDSize()
  122. {
  123. ThrowIfDisposed();
  124. return ximgproc_ContourFitting_getFDSize_10(nativeObj);
  125. }
  126. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  127. const string LIBNAME = "__Internal";
  128. #else
  129. const string LIBNAME = "opencvforunity";
  130. #endif
  131. // C++: void cv::ximgproc::ContourFitting::estimateTransformation(Mat src, Mat dst, Mat& alphaPhiST, double& dist, bool fdContour = false)
  132. [DllImport(LIBNAME)]
  133. private static extern void ximgproc_ContourFitting_estimateTransformation_10(IntPtr nativeObj, IntPtr src_nativeObj, IntPtr dst_nativeObj, IntPtr alphaPhiST_nativeObj, double[] dist_out, [MarshalAs(UnmanagedType.U1)] bool fdContour);
  134. [DllImport(LIBNAME)]
  135. private static extern void ximgproc_ContourFitting_estimateTransformation_11(IntPtr nativeObj, IntPtr src_nativeObj, IntPtr dst_nativeObj, IntPtr alphaPhiST_nativeObj, double[] dist_out);
  136. // C++: void cv::ximgproc::ContourFitting::setCtrSize(int n)
  137. [DllImport(LIBNAME)]
  138. private static extern void ximgproc_ContourFitting_setCtrSize_10(IntPtr nativeObj, int n);
  139. // C++: void cv::ximgproc::ContourFitting::setFDSize(int n)
  140. [DllImport(LIBNAME)]
  141. private static extern void ximgproc_ContourFitting_setFDSize_10(IntPtr nativeObj, int n);
  142. // C++: int cv::ximgproc::ContourFitting::getCtrSize()
  143. [DllImport(LIBNAME)]
  144. private static extern int ximgproc_ContourFitting_getCtrSize_10(IntPtr nativeObj);
  145. // C++: int cv::ximgproc::ContourFitting::getFDSize()
  146. [DllImport(LIBNAME)]
  147. private static extern int ximgproc_ContourFitting_getFDSize_10(IntPtr nativeObj);
  148. // native support for java finalize()
  149. [DllImport(LIBNAME)]
  150. private static extern void ximgproc_ContourFitting_delete(IntPtr nativeObj);
  151. }
  152. }