bilateral.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*====================================================================*
  2. - Copyright (C) 2001 Leptonica. All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions
  6. - are met:
  7. - 1. Redistributions of source code must retain the above copyright
  8. - notice, this list of conditions and the following disclaimer.
  9. - 2. Redistributions in binary form must reproduce the above
  10. - copyright notice, this list of conditions and the following
  11. - disclaimer in the documentation and/or other materials
  12. - provided with the distribution.
  13. -
  14. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
  18. - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *====================================================================*/
  26. #ifndef LEPTONICA_BILATERAL_H
  27. #define LEPTONICA_BILATERAL_H
  28. /*!
  29. * \file bilateral.h
  30. *
  31. * <pre>
  32. * Contains the following struct
  33. * struct L_Bilateral
  34. *
  35. *
  36. * For a tutorial introduction to bilateral filters, which apply a
  37. * gaussian blur to smooth parts of the image while preserving edges, see
  38. * http://people.csail.mit.edu/sparis/bf_course/slides/03_definition_bf.pdf
  39. *
  40. * We give an implementation of a bilateral filtering algorithm given in:
  41. * "Real-Time O(1) Bilateral Filtering," by Yang, Tan and Ahuja, CVPR 2009
  42. * which is at:
  43. * http://vision.ai.uiuc.edu/~qyang6/publications/cvpr-09-qingxiong-yang.pdf
  44. * This is based on an earlier algorithm by Sylvain Paris and Frédo Durand:
  45. * http://people.csail.mit.edu/sparis/publi/2006/eccv/
  46. * Paris_06_Fast_Approximation.pdf
  47. *
  48. * The kernel of the filter is a product of a spatial gaussian and a
  49. * monotonically decreasing function of the difference in intensity
  50. * between the source pixel and the neighboring pixel. The intensity
  51. * part of the filter gives higher influence for pixels with intensities
  52. * that are near to the source pixel, and the spatial part of the
  53. * filter gives higher weight to pixels that are near the source pixel.
  54. * This combination smooths in relatively uniform regions, while
  55. * maintaining edges.
  56. *
  57. * The advantage of the appoach of Yang et al is that it is separable,
  58. * so the computation time is linear in the gaussian filter size.
  59. * Furthermore, it is possible to do much of the computation as a reduced
  60. * scale, which gives a good approximation to the full resolution version
  61. * but greatly speeds it up.
  62. *
  63. * The bilateral filtered value at x is:
  64. *
  65. * sum[y in N(x)]: spatial(|y - x|) * range(|I(x) - I(y)|) * I(y)
  66. * I'(x) = --------------------------------------------------------------
  67. * sum[y in N(x)]: spatial(|y - x|) * range(|I(x) - I(y)|)
  68. *
  69. * where I() is the input image, I'() is the filtered image, N(x) is the
  70. * set of pixels around x in the filter support, and spatial() and range()
  71. * are gaussian functions:
  72. * spatial(x) = exp(-x^2 / (2 * s_s^2))
  73. * range(x) = exp(-x^2 / (2 * s_r^2))
  74. * and s_s and s_r and the standard deviations of the two gaussians.
  75. *
  76. * Yang et al use a separable approximation to this, by defining a set
  77. * of related but separable functions J(k,x), that we call Principal
  78. * Bilateral Components (PBC):
  79. *
  80. * sum[y in N(x)]: spatial(|y - x|) * range(|k - I(y)|) * I(y)
  81. * J(k,x) = -----------------------------------------------------------
  82. * sum[y in N(x)]: spatial(|y - x|) * range(|k - I(y)|)
  83. *
  84. * which are computed quickly for a set of n values k[p], p = 0 ... n-1.
  85. * Then each output pixel is found using a linear interpolation:
  86. *
  87. * I'(x) = (1 - q) * J(k[p],x) + q * J(k[p+1],x)
  88. *
  89. * where J(k[p],x) and J(k[p+1],x) are PBC for which
  90. * k[p] <= I(x) and k[p+1] >= I(x), and
  91. * q = (I(x) - k[p]) / (k[p+1] - k[p]).
  92. *
  93. * We can also subsample I(x), create subsampled versions of J(k,x),
  94. * which are then interpolated between for I'(x).
  95. *
  96. * We generate 'pixsc', by optionally downscaling the input image
  97. * (using area mapping by the factor 'reduction'), and then adding
  98. * a mirrored border to avoid boundary cases. This is then used
  99. * to compute 'ncomps' PBCs.
  100. *
  101. * The 'spatial_stdev' is also downscaled by 'reduction'. The size
  102. * of the 'spatial' array is 4 * (reduced 'spatial_stdev') + 1.
  103. * The size of the 'range' array is 256.
  104. * </pre>
  105. */
  106. /*------------------------------------------------------------------------*
  107. * Bilateral filter *
  108. *------------------------------------------------------------------------*/
  109. /*! Bilateral filter */
  110. struct L_Bilateral
  111. {
  112. struct Pix *pixs; /*!< clone of source pix */
  113. struct Pix *pixsc; /*!< downscaled pix with mirrored border */
  114. l_int32 reduction; /*!< 1, 2 or 4x for intermediates */
  115. l_float32 spatial_stdev; /*!< stdev of spatial gaussian */
  116. l_float32 range_stdev; /*!< stdev of range gaussian */
  117. l_float32 *spatial; /*!< 1D gaussian spatial kernel */
  118. l_float32 *range; /*!< one-sided gaussian range kernel */
  119. l_int32 minval; /*!< min value in 8 bpp pix */
  120. l_int32 maxval; /*!< max value in 8 bpp pix */
  121. l_int32 ncomps; /*!< number of intermediate results */
  122. l_int32 *nc; /*!< set of k values (size ncomps) */
  123. l_int32 *kindex; /*!< mapping from intensity to lower k */
  124. l_float32 *kfract; /*!< mapping from intensity to fract k */
  125. struct Pixa *pixac; /*!< intermediate result images (PBC) */
  126. l_uint32 ***lineset; /*!< lineptrs for pixac */
  127. };
  128. typedef struct L_Bilateral L_BILATERAL;
  129. #endif /* LEPTONICA_BILATERAL_H */