bilateral.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Contains the following struct
  30. * struct L_Bilateral
  31. *
  32. *
  33. * For a tutorial introduction to bilateral filters, which apply a
  34. * gaussian blur to smooth parts of the image while preserving edges, see
  35. * http://people.csail.mit.edu/sparis/bf_course/slides/03_definition_bf.pdf
  36. *
  37. * We give an implementation of a bilateral filtering algorithm given in:
  38. * "Real-Time O(1) Bilateral Filtering," by Yang, Tan and Ahuja, CVPR 2009
  39. * which is at:
  40. * http://vision.ai.uiuc.edu/~qyang6/publications/cvpr-09-qingxiong-yang.pdf
  41. * This is based on an earlier algorithm by Sylvain Paris and Frédo Durand:
  42. * http://people.csail.mit.edu/sparis/publi/2006/eccv/
  43. * Paris_06_Fast_Approximation.pdf
  44. *
  45. * The kernel of the filter is a product of a spatial gaussian and a
  46. * monotonically decreasing function of the difference in intensity
  47. * between the source pixel and the neighboring pixel. The intensity
  48. * part of the filter gives higher influence for pixels with intensities
  49. * that are near to the source pixel, and the spatial part of the
  50. * filter gives higher weight to pixels that are near the source pixel.
  51. * This combination smooths in relatively uniform regions, while
  52. * maintaining edges.
  53. *
  54. * The advantage of the appoach of Yang et al is that it is separable,
  55. * so the computation time is linear in the gaussian filter size.
  56. * Furthermore, it is possible to do much of the computation as a reduced
  57. * scale, which gives a good approximation to the full resolution version
  58. * but greatly speeds it up.
  59. *
  60. * The bilateral filtered value at x is:
  61. *
  62. * sum[y in N(x)]: spatial(|y - x|) * range(|I(x) - I(y)|) * I(y)
  63. * I'(x) = --------------------------------------------------------------
  64. * sum[y in N(x)]: spatial(|y - x|) * range(|I(x) - I(y)|)
  65. *
  66. * where I() is the input image, I'() is the filtered image, N(x) is the
  67. * set of pixels around x in the filter support, and spatial() and range()
  68. * are gaussian functions:
  69. * spatial(x) = exp(-x^2 / (2 * s_s^2))
  70. * range(x) = exp(-x^2 / (2 * s_r^2))
  71. * and s_s and s_r and the standard deviations of the two gaussians.
  72. *
  73. * Yang et al use a separable approximation to this, by defining a set
  74. * of related but separable functions J(k,x), that we call Principal
  75. * Bilateral Components (PBC):
  76. *
  77. * sum[y in N(x)]: spatial(|y - x|) * range(|k - I(y)|) * I(y)
  78. * J(k,x) = -----------------------------------------------------------
  79. * sum[y in N(x)]: spatial(|y - x|) * range(|k - I(y)|)
  80. *
  81. * which are computed quickly for a set of n values k[p], p = 0 ... n-1.
  82. * Then each output pixel is found using a linear interpolation:
  83. *
  84. * I'(x) = (1 - q) * J(k[p],x) + q * J(k[p+1],x)
  85. *
  86. * where J(k[p],x) and J(k[p+1],x) are PBC for which
  87. * k[p] <= I(x) and k[p+1] >= I(x), and
  88. * q = (I(x) - k[p]) / (k[p+1] - k[p]).
  89. *
  90. * We can also subsample I(x), create subsampled versions of J(k,x),
  91. * which are then interpolated between for I'(x).
  92. *
  93. * We generate 'pixsc', by optionally downscaling the input image
  94. * (using area mapping by the factor 'reduction'), and then adding
  95. * a mirrored border to avoid boundary cases. This is then used
  96. * to compute 'ncomps' PBCs.
  97. *
  98. * The 'spatial_stdev' is also downscaled by 'reduction'. The size
  99. * of the 'spatial' array is 4 * (reduced 'spatial_stdev') + 1.
  100. * The size of the 'range' array is 256.
  101. */
  102. /*------------------------------------------------------------------------*
  103. * Bilateral filter *
  104. *------------------------------------------------------------------------*/
  105. struct L_Bilateral
  106. {
  107. struct Pix *pixs; /* clone of source pix */
  108. struct Pix *pixsc; /* downscaled pix with mirrored border */
  109. l_int32 reduction; /* 1, 2 or 4x for intermediates */
  110. l_float32 spatial_stdev; /* stdev of spatial gaussian */
  111. l_float32 range_stdev; /* stdev of range gaussian */
  112. l_float32 *spatial; /* 1D gaussian spatial kernel */
  113. l_float32 *range; /* one-sided gaussian range kernel */
  114. l_int32 minval; /* min value in 8 bpp pix */
  115. l_int32 maxval; /* max value in 8 bpp pix */
  116. l_int32 ncomps; /* number of intermediate results */
  117. l_int32 *nc; /* set of k values (size ncomps) */
  118. l_int32 *kindex; /* mapping from intensity to lower k */
  119. l_float32 *kfract; /* mapping from intensity to fract k */
  120. struct Pixa *pixac; /* intermediate result images (PBC) */
  121. l_uint32 ***lineset; /* lineptrs for pixac */
  122. };
  123. typedef struct L_Bilateral L_BILATERAL;
  124. #endif /* LEPTONICA_BILATERAL_H */