GrayCodePattern.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #if !UNITY_WEBGL
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.UtilsModule;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace OpenCVForUnity.Structured_lightModule
  8. {
  9. // C++: class GrayCodePattern
  10. /**
  11. * Class implementing the Gray-code pattern, based on CITE: UNDERWORLD.
  12. *
  13. * The generation of the pattern images is performed with Gray encoding using the traditional white and black colors.
  14. *
  15. * The information about the two image axes x, y is encoded separately into two different pattern sequences.
  16. * A projector P with resolution (P_res_x, P_res_y) will result in Ncols = log 2 (P_res_x) encoded pattern images representing the columns, and
  17. * in Nrows = log 2 (P_res_y) encoded pattern images representing the rows.
  18. * For example a projector with resolution 1024x768 will result in Ncols = 10 and Nrows = 10.
  19. *
  20. * However, the generated pattern sequence consists of both regular color and color-inverted images: inverted pattern images are images
  21. * with the same structure as the original but with inverted colors.
  22. * This provides an effective method for easily determining the intensity value of each pixel when it is lit (highest value) and
  23. * when it is not lit (lowest value). So for a a projector with resolution 1024x768, the number of pattern images will be Ncols * 2 + Nrows * 2 = 40.
  24. *
  25. */
  26. public class GrayCodePattern : StructuredLightPattern
  27. {
  28. protected override void Dispose(bool disposing)
  29. {
  30. try
  31. {
  32. if (disposing)
  33. {
  34. }
  35. if (IsEnabledDispose)
  36. {
  37. if (nativeObj != IntPtr.Zero)
  38. structured_1light_GrayCodePattern_delete(nativeObj);
  39. nativeObj = IntPtr.Zero;
  40. }
  41. }
  42. finally
  43. {
  44. base.Dispose(disposing);
  45. }
  46. }
  47. protected internal GrayCodePattern(IntPtr addr) : base(addr) { }
  48. // internal usage only
  49. public static new GrayCodePattern __fromPtr__(IntPtr addr) { return new GrayCodePattern(addr); }
  50. //
  51. // C++: static Ptr_GrayCodePattern cv::structured_light::GrayCodePattern::create(int width, int height)
  52. //
  53. /**
  54. * Constructor
  55. * param width automatically generated
  56. * param height automatically generated
  57. * return automatically generated
  58. */
  59. public static GrayCodePattern create(int width, int height)
  60. {
  61. return GrayCodePattern.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(structured_1light_GrayCodePattern_create_10(width, height)));
  62. }
  63. //
  64. // C++: size_t cv::structured_light::GrayCodePattern::getNumberOfPatternImages()
  65. //
  66. /**
  67. * Get the number of pattern images needed for the graycode pattern.
  68. *
  69. * return The number of pattern images needed for the graycode pattern.
  70. *
  71. */
  72. public long getNumberOfPatternImages()
  73. {
  74. ThrowIfDisposed();
  75. return structured_1light_GrayCodePattern_getNumberOfPatternImages_10(nativeObj);
  76. }
  77. //
  78. // C++: void cv::structured_light::GrayCodePattern::setWhiteThreshold(size_t value)
  79. //
  80. /**
  81. * Sets the value for white threshold, needed for decoding.
  82. *
  83. * White threshold is a number between 0-255 that represents the minimum brightness difference required for valid pixels, between the graycode pattern and its inverse images; used in getProjPixel method.
  84. *
  85. * param value The desired white threshold value.
  86. *
  87. */
  88. public void setWhiteThreshold(long value)
  89. {
  90. ThrowIfDisposed();
  91. structured_1light_GrayCodePattern_setWhiteThreshold_10(nativeObj, value);
  92. }
  93. //
  94. // C++: void cv::structured_light::GrayCodePattern::setBlackThreshold(size_t value)
  95. //
  96. /**
  97. * Sets the value for black threshold, needed for decoding (shadowsmasks computation).
  98. *
  99. * Black threshold is a number between 0-255 that represents the minimum brightness difference required for valid pixels, between the fully illuminated (white) and the not illuminated images (black); used in computeShadowMasks method.
  100. *
  101. * param value The desired black threshold value.
  102. *
  103. */
  104. public void setBlackThreshold(long value)
  105. {
  106. ThrowIfDisposed();
  107. structured_1light_GrayCodePattern_setBlackThreshold_10(nativeObj, value);
  108. }
  109. //
  110. // C++: void cv::structured_light::GrayCodePattern::getImagesForShadowMasks(Mat& blackImage, Mat& whiteImage)
  111. //
  112. /**
  113. * Generates the all-black and all-white images needed for shadowMasks computation.
  114. *
  115. * To identify shadow regions, the regions of two images where the pixels are not lit by projector's light and thus where there is not coded information,
  116. * the 3DUNDERWORLD algorithm computes a shadow mask for the two cameras views, starting from a white and a black images captured by each camera.
  117. * This method generates these two additional images to project.
  118. *
  119. * param blackImage The generated all-black CV_8U image, at projector's resolution.
  120. * param whiteImage The generated all-white CV_8U image, at projector's resolution.
  121. */
  122. public void getImagesForShadowMasks(Mat blackImage, Mat whiteImage)
  123. {
  124. ThrowIfDisposed();
  125. if (blackImage != null) blackImage.ThrowIfDisposed();
  126. if (whiteImage != null) whiteImage.ThrowIfDisposed();
  127. structured_1light_GrayCodePattern_getImagesForShadowMasks_10(nativeObj, blackImage.nativeObj, whiteImage.nativeObj);
  128. }
  129. //
  130. // C++: bool cv::structured_light::GrayCodePattern::getProjPixel(vector_Mat patternImages, int x, int y, Point& projPix)
  131. //
  132. /**
  133. * For a (x,y) pixel of a camera returns the corresponding projector pixel.
  134. *
  135. * The function decodes each pixel in the pattern images acquired by a camera into their corresponding decimal numbers representing the projector's column and row,
  136. * providing a mapping between camera's and projector's pixel.
  137. *
  138. * param patternImages The pattern images acquired by the camera, stored in a grayscale vector < Mat >.
  139. * param x x coordinate of the image pixel.
  140. * param y y coordinate of the image pixel.
  141. * param projPix Projector's pixel corresponding to the camera's pixel: projPix.x and projPix.y are the image coordinates of the projector's pixel corresponding to the pixel being decoded in a camera.
  142. * return automatically generated
  143. */
  144. public bool getProjPixel(List<Mat> patternImages, int x, int y, Point projPix)
  145. {
  146. ThrowIfDisposed();
  147. Mat patternImages_mat = Converters.vector_Mat_to_Mat(patternImages);
  148. double[] projPix_out = new double[2];
  149. bool retVal = structured_1light_GrayCodePattern_getProjPixel_10(nativeObj, patternImages_mat.nativeObj, x, y, projPix_out);
  150. if (projPix != null) { projPix.x = projPix_out[0]; projPix.y = projPix_out[1]; }
  151. return retVal;
  152. }
  153. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  154. const string LIBNAME = "__Internal";
  155. #else
  156. const string LIBNAME = "opencvforunity";
  157. #endif
  158. // C++: static Ptr_GrayCodePattern cv::structured_light::GrayCodePattern::create(int width, int height)
  159. [DllImport(LIBNAME)]
  160. private static extern IntPtr structured_1light_GrayCodePattern_create_10(int width, int height);
  161. // C++: size_t cv::structured_light::GrayCodePattern::getNumberOfPatternImages()
  162. [DllImport(LIBNAME)]
  163. private static extern long structured_1light_GrayCodePattern_getNumberOfPatternImages_10(IntPtr nativeObj);
  164. // C++: void cv::structured_light::GrayCodePattern::setWhiteThreshold(size_t value)
  165. [DllImport(LIBNAME)]
  166. private static extern void structured_1light_GrayCodePattern_setWhiteThreshold_10(IntPtr nativeObj, long value);
  167. // C++: void cv::structured_light::GrayCodePattern::setBlackThreshold(size_t value)
  168. [DllImport(LIBNAME)]
  169. private static extern void structured_1light_GrayCodePattern_setBlackThreshold_10(IntPtr nativeObj, long value);
  170. // C++: void cv::structured_light::GrayCodePattern::getImagesForShadowMasks(Mat& blackImage, Mat& whiteImage)
  171. [DllImport(LIBNAME)]
  172. private static extern void structured_1light_GrayCodePattern_getImagesForShadowMasks_10(IntPtr nativeObj, IntPtr blackImage_nativeObj, IntPtr whiteImage_nativeObj);
  173. // C++: bool cv::structured_light::GrayCodePattern::getProjPixel(vector_Mat patternImages, int x, int y, Point& projPix)
  174. [DllImport(LIBNAME)]
  175. [return: MarshalAs(UnmanagedType.U1)]
  176. private static extern bool structured_1light_GrayCodePattern_getProjPixel_10(IntPtr nativeObj, IntPtr patternImages_mat_nativeObj, int x, int y, double[] projPix_out);
  177. // native support for java finalize()
  178. [DllImport(LIBNAME)]
  179. private static extern void structured_1light_GrayCodePattern_delete(IntPtr nativeObj);
  180. }
  181. }
  182. #endif