RetinaFastToneMapping.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.BioinspiredModule
  7. {
  8. // C++: class RetinaFastToneMapping
  9. /**
  10. * a wrapper class which allows the tone mapping algorithm of Meylan&al(2007) to be used with OpenCV.
  11. *
  12. * This algorithm is already implemented in thre Retina class (retina::applyFastToneMapping) but used it does not require all the retina model to be allocated. This allows a light memory use for low memory devices (smartphones, etc.
  13. * As a summary, these are the model properties:
  14. * <ul>
  15. * <li>
  16. * 2 stages of local luminance adaptation with a different local neighborhood for each.
  17. * </li>
  18. * <li>
  19. * first stage models the retina photorecetors local luminance adaptation
  20. * </li>
  21. * <li>
  22. * second stage models th ganglion cells local information adaptation
  23. * </li>
  24. * <li>
  25. * compared to the initial publication, this class uses spatio-temporal low pass filters instead of spatial only filters.
  26. * this can help noise robustness and temporal stability for video sequence use cases.
  27. * </li>
  28. * </ul>
  29. *
  30. * for more information, read to the following papers :
  31. * Meylan L., Alleysson D., and Susstrunk S., A Model of Retinal Local Adaptation for the Tone Mapping of Color Filter Array Images, Journal of Optical Society of America, A, Vol. 24, N 9, September, 1st, 2007, pp. 2807-2816Benoit A., Caplier A., Durette B., Herault, J., "USING HUMAN VISUAL SYSTEM MODELING FOR BIO-INSPIRED LOW LEVEL IMAGE PROCESSING", Elsevier, Computer Vision and Image Understanding 114 (2010), pp. 758-773, DOI: http://dx.doi.org/10.1016/j.cviu.2010.01.011
  32. * regarding spatio-temporal filter and the bigger retina model :
  33. * Vision: Images, Signals and Neural Networks: Models of Neural Processing in Visual Perception (Progress in Neural Processing),By: Jeanny Herault, ISBN: 9814273686. WAPI (Tower ID): 113266891.
  34. */
  35. public class RetinaFastToneMapping : Algorithm
  36. {
  37. protected override void Dispose(bool disposing)
  38. {
  39. try
  40. {
  41. if (disposing)
  42. {
  43. }
  44. if (IsEnabledDispose)
  45. {
  46. if (nativeObj != IntPtr.Zero)
  47. bioinspired_RetinaFastToneMapping_delete(nativeObj);
  48. nativeObj = IntPtr.Zero;
  49. }
  50. }
  51. finally
  52. {
  53. base.Dispose(disposing);
  54. }
  55. }
  56. protected internal RetinaFastToneMapping(IntPtr addr) : base(addr) { }
  57. // internal usage only
  58. public static new RetinaFastToneMapping __fromPtr__(IntPtr addr) { return new RetinaFastToneMapping(addr); }
  59. //
  60. // C++: void cv::bioinspired::RetinaFastToneMapping::applyFastToneMapping(Mat inputImage, Mat& outputToneMappedImage)
  61. //
  62. /**
  63. * applies a luminance correction (initially High Dynamic Range (HDR) tone mapping)
  64. *
  65. * using only the 2 local adaptation stages of the retina parvocellular channel : photoreceptors
  66. * level and ganlion cells level. Spatio temporal filtering is applied but limited to temporal
  67. * smoothing and eventually high frequencies attenuation. This is a lighter method than the one
  68. * available using the regular retina::run method. It is then faster but it does not include
  69. * complete temporal filtering nor retina spectral whitening. Then, it can have a more limited
  70. * effect on images with a very high dynamic range. This is an adptation of the original still
  71. * image HDR tone mapping algorithm of David Alleyson, Sabine Susstruck and Laurence Meylan's
  72. * work, please cite: -&gt; Meylan L., Alleysson D., and Susstrunk S., A Model of Retinal Local
  73. * Adaptation for the Tone Mapping of Color Filter Array Images, Journal of Optical Society of
  74. * America, A, Vol. 24, N 9, September, 1st, 2007, pp. 2807-2816
  75. *
  76. * param inputImage the input image to process RGB or gray levels
  77. * param outputToneMappedImage the output tone mapped image
  78. */
  79. public void applyFastToneMapping(Mat inputImage, Mat outputToneMappedImage)
  80. {
  81. ThrowIfDisposed();
  82. if (inputImage != null) inputImage.ThrowIfDisposed();
  83. if (outputToneMappedImage != null) outputToneMappedImage.ThrowIfDisposed();
  84. bioinspired_RetinaFastToneMapping_applyFastToneMapping_10(nativeObj, inputImage.nativeObj, outputToneMappedImage.nativeObj);
  85. }
  86. //
  87. // C++: void cv::bioinspired::RetinaFastToneMapping::setup(float photoreceptorsNeighborhoodRadius = 3.f, float ganglioncellsNeighborhoodRadius = 1.f, float meanLuminanceModulatorK = 1.f)
  88. //
  89. /**
  90. * updates tone mapping behaviors by adjusing the local luminance computation area
  91. *
  92. * param photoreceptorsNeighborhoodRadius the first stage local adaptation area
  93. * param ganglioncellsNeighborhoodRadius the second stage local adaptation area
  94. * param meanLuminanceModulatorK the factor applied to modulate the meanLuminance information
  95. * (default is 1, see reference paper)
  96. */
  97. public void setup(float photoreceptorsNeighborhoodRadius, float ganglioncellsNeighborhoodRadius, float meanLuminanceModulatorK)
  98. {
  99. ThrowIfDisposed();
  100. bioinspired_RetinaFastToneMapping_setup_10(nativeObj, photoreceptorsNeighborhoodRadius, ganglioncellsNeighborhoodRadius, meanLuminanceModulatorK);
  101. }
  102. /**
  103. * updates tone mapping behaviors by adjusing the local luminance computation area
  104. *
  105. * param photoreceptorsNeighborhoodRadius the first stage local adaptation area
  106. * param ganglioncellsNeighborhoodRadius the second stage local adaptation area
  107. * (default is 1, see reference paper)
  108. */
  109. public void setup(float photoreceptorsNeighborhoodRadius, float ganglioncellsNeighborhoodRadius)
  110. {
  111. ThrowIfDisposed();
  112. bioinspired_RetinaFastToneMapping_setup_11(nativeObj, photoreceptorsNeighborhoodRadius, ganglioncellsNeighborhoodRadius);
  113. }
  114. /**
  115. * updates tone mapping behaviors by adjusing the local luminance computation area
  116. *
  117. * param photoreceptorsNeighborhoodRadius the first stage local adaptation area
  118. * (default is 1, see reference paper)
  119. */
  120. public void setup(float photoreceptorsNeighborhoodRadius)
  121. {
  122. ThrowIfDisposed();
  123. bioinspired_RetinaFastToneMapping_setup_12(nativeObj, photoreceptorsNeighborhoodRadius);
  124. }
  125. /**
  126. * updates tone mapping behaviors by adjusing the local luminance computation area
  127. *
  128. * (default is 1, see reference paper)
  129. */
  130. public void setup()
  131. {
  132. ThrowIfDisposed();
  133. bioinspired_RetinaFastToneMapping_setup_13(nativeObj);
  134. }
  135. //
  136. // C++: static Ptr_RetinaFastToneMapping cv::bioinspired::RetinaFastToneMapping::create(Size inputSize)
  137. //
  138. public static RetinaFastToneMapping create(Size inputSize)
  139. {
  140. return RetinaFastToneMapping.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(bioinspired_RetinaFastToneMapping_create_10(inputSize.width, inputSize.height)));
  141. }
  142. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  143. const string LIBNAME = "__Internal";
  144. #else
  145. const string LIBNAME = "opencvforunity";
  146. #endif
  147. // C++: void cv::bioinspired::RetinaFastToneMapping::applyFastToneMapping(Mat inputImage, Mat& outputToneMappedImage)
  148. [DllImport(LIBNAME)]
  149. private static extern void bioinspired_RetinaFastToneMapping_applyFastToneMapping_10(IntPtr nativeObj, IntPtr inputImage_nativeObj, IntPtr outputToneMappedImage_nativeObj);
  150. // C++: void cv::bioinspired::RetinaFastToneMapping::setup(float photoreceptorsNeighborhoodRadius = 3.f, float ganglioncellsNeighborhoodRadius = 1.f, float meanLuminanceModulatorK = 1.f)
  151. [DllImport(LIBNAME)]
  152. private static extern void bioinspired_RetinaFastToneMapping_setup_10(IntPtr nativeObj, float photoreceptorsNeighborhoodRadius, float ganglioncellsNeighborhoodRadius, float meanLuminanceModulatorK);
  153. [DllImport(LIBNAME)]
  154. private static extern void bioinspired_RetinaFastToneMapping_setup_11(IntPtr nativeObj, float photoreceptorsNeighborhoodRadius, float ganglioncellsNeighborhoodRadius);
  155. [DllImport(LIBNAME)]
  156. private static extern void bioinspired_RetinaFastToneMapping_setup_12(IntPtr nativeObj, float photoreceptorsNeighborhoodRadius);
  157. [DllImport(LIBNAME)]
  158. private static extern void bioinspired_RetinaFastToneMapping_setup_13(IntPtr nativeObj);
  159. // C++: static Ptr_RetinaFastToneMapping cv::bioinspired::RetinaFastToneMapping::create(Size inputSize)
  160. [DllImport(LIBNAME)]
  161. private static extern IntPtr bioinspired_RetinaFastToneMapping_create_10(double inputSize_width, double inputSize_height);
  162. // native support for java finalize()
  163. [DllImport(LIBNAME)]
  164. private static extern void bioinspired_RetinaFastToneMapping_delete(IntPtr nativeObj);
  165. }
  166. }