_denoise.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. import functools
  2. from math import ceil
  3. import numbers
  4. import scipy.stats
  5. import numpy as np
  6. from ..util.dtype import img_as_float
  7. from .._shared import utils
  8. from .._shared.utils import _supported_float_type, warn
  9. from ._denoise_cy import _denoise_bilateral, _denoise_tv_bregman
  10. from .. import color
  11. from ..color.colorconv import ycbcr_from_rgb
  12. __doctest_requires__ = {("denoise_wavelet", "estimate_sigma"): ["pywt"]}
  13. def _gaussian_weight(array, sigma_squared, *, dtype=float):
  14. """Helping function. Define a Gaussian weighting from array and
  15. sigma_square.
  16. Parameters
  17. ----------
  18. array : ndarray
  19. Input array.
  20. sigma_squared : float
  21. The squared standard deviation used in the filter.
  22. dtype : data type object, optional (default : float)
  23. The type and size of the data to be returned.
  24. Returns
  25. -------
  26. gaussian : ndarray
  27. The input array filtered by the Gaussian.
  28. """
  29. return np.exp(-0.5 * (array**2 / sigma_squared), dtype=dtype)
  30. def _compute_color_lut(bins, sigma, max_value, *, dtype=float):
  31. """Helping function. Define a lookup table containing Gaussian filter
  32. values using the color distance sigma.
  33. Parameters
  34. ----------
  35. bins : int
  36. Number of discrete values for Gaussian weights of color filtering.
  37. A larger value results in improved accuracy.
  38. sigma : float
  39. Standard deviation for grayvalue/color distance (radiometric
  40. similarity). A larger value results in averaging of pixels with larger
  41. radiometric differences. Note, that the image will be converted using
  42. the `img_as_float` function and thus the standard deviation is in
  43. respect to the range ``[0, 1]``. If the value is ``None`` the standard
  44. deviation of the ``image`` will be used.
  45. max_value : float
  46. Maximum value of the input image.
  47. dtype : data type object, optional (default : float)
  48. The type and size of the data to be returned.
  49. Returns
  50. -------
  51. color_lut : ndarray
  52. Lookup table for the color distance sigma.
  53. """
  54. values = np.linspace(0, max_value, bins, endpoint=False)
  55. return _gaussian_weight(values, sigma**2, dtype=dtype)
  56. def _compute_spatial_lut(win_size, sigma, *, dtype=float):
  57. """Helping function. Define a lookup table containing Gaussian filter
  58. values using the spatial sigma.
  59. Parameters
  60. ----------
  61. win_size : int
  62. Window size for filtering.
  63. If win_size is not specified, it is calculated as
  64. ``max(5, 2 * ceil(3 * sigma_spatial) + 1)``.
  65. sigma : float
  66. Standard deviation for range distance. A larger value results in
  67. averaging of pixels with larger spatial differences.
  68. dtype : data type object
  69. The type and size of the data to be returned.
  70. Returns
  71. -------
  72. spatial_lut : ndarray
  73. Lookup table for the spatial sigma.
  74. """
  75. grid_points = np.arange(-win_size // 2, win_size // 2 + 1)
  76. rr, cc = np.meshgrid(grid_points, grid_points, indexing='ij')
  77. distances = np.hypot(rr, cc)
  78. return _gaussian_weight(distances, sigma**2, dtype=dtype).ravel()
  79. @utils.channel_as_last_axis()
  80. def denoise_bilateral(
  81. image,
  82. win_size=None,
  83. sigma_color=None,
  84. sigma_spatial=1,
  85. bins=10000,
  86. mode='constant',
  87. cval=0,
  88. *,
  89. channel_axis=None,
  90. ):
  91. """Denoise image using bilateral filter.
  92. Parameters
  93. ----------
  94. image : ndarray, shape (M, N[, 3])
  95. Input image, 2D grayscale or RGB.
  96. win_size : int
  97. Window size for filtering.
  98. If win_size is not specified, it is calculated as
  99. ``max(5, 2 * ceil(3 * sigma_spatial) + 1)``.
  100. sigma_color : float
  101. Standard deviation for grayvalue/color distance (radiometric
  102. similarity). A larger value results in averaging of pixels with larger
  103. radiometric differences. If ``None``, the standard deviation of
  104. ``image`` will be used.
  105. sigma_spatial : float
  106. Standard deviation for range distance. A larger value results in
  107. averaging of pixels with larger spatial differences.
  108. bins : int
  109. Number of discrete values for Gaussian weights of color filtering.
  110. A larger value results in improved accuracy.
  111. mode : {'constant', 'edge', 'symmetric', 'reflect', 'wrap'}
  112. How to handle values outside the image borders. See
  113. `numpy.pad` for detail.
  114. cval : int or float
  115. Used in conjunction with mode 'constant', the value outside
  116. the image boundaries.
  117. channel_axis : int or None, optional
  118. If ``None``, the image is assumed to be grayscale (single-channel).
  119. Otherwise, this parameter indicates which axis of the array corresponds
  120. to channels.
  121. .. versionadded:: 0.19
  122. ``channel_axis`` was added in 0.19.
  123. Returns
  124. -------
  125. denoised : ndarray
  126. Denoised image.
  127. Notes
  128. -----
  129. This is an edge-preserving, denoising filter. It averages pixels based on
  130. their spatial closeness and radiometric similarity [1]_.
  131. Spatial closeness is measured by the Gaussian function of the Euclidean
  132. distance between two pixels and a certain standard deviation
  133. (`sigma_spatial`).
  134. Radiometric similarity is measured by the Gaussian function of the
  135. Euclidean distance between two color values and a certain standard
  136. deviation (`sigma_color`).
  137. Note that, if the image is of any `int` dtype, ``image`` will be
  138. converted using the `img_as_float` function and thus the standard
  139. deviation (`sigma_color`) will be in range ``[0, 1]``.
  140. For more information on scikit-image's data type conversions and how
  141. images are rescaled in these conversions,
  142. see: https://scikit-image.org/docs/stable/user_guide/data_types.html.
  143. References
  144. ----------
  145. .. [1] C. Tomasi and R. Manduchi. "Bilateral Filtering for Gray and Color
  146. Images." IEEE International Conference on Computer Vision (1998)
  147. 839-846. :DOI:`10.1109/ICCV.1998.710815`
  148. Examples
  149. --------
  150. >>> from skimage import data, img_as_float
  151. >>> astro = img_as_float(data.astronaut())
  152. >>> astro = astro[220:300, 220:320]
  153. >>> rng = np.random.default_rng()
  154. >>> noisy = astro + 0.6 * astro.std() * rng.random(astro.shape)
  155. >>> noisy = np.clip(noisy, 0, 1)
  156. >>> denoised = denoise_bilateral(noisy, sigma_color=0.05, sigma_spatial=15,
  157. ... channel_axis=-1)
  158. """
  159. if channel_axis is not None:
  160. if image.ndim != 3:
  161. if image.ndim == 2:
  162. raise ValueError(
  163. "Use ``channel_axis=None`` for 2D grayscale "
  164. "images. The last axis of the input image "
  165. "must be multiple color channels not another "
  166. "spatial dimension."
  167. )
  168. else:
  169. raise ValueError(
  170. f'Bilateral filter is only implemented for '
  171. f'2D grayscale images (image.ndim == 2) and '
  172. f'2D multichannel (image.ndim == 3) images, '
  173. f'but the input image has {image.ndim} dimensions.'
  174. )
  175. elif image.shape[2] not in (3, 4):
  176. if image.shape[2] > 4:
  177. msg = (
  178. f'The last axis of the input image is '
  179. f'interpreted as channels. Input image with '
  180. f'shape {image.shape} has {image.shape[2]} channels '
  181. f'in last axis. ``denoise_bilateral``is implemented '
  182. f'for 2D grayscale and color images only.'
  183. )
  184. warn(msg)
  185. else:
  186. msg = (
  187. f'Input image must be grayscale, RGB, or RGBA; '
  188. f'but has shape {image.shape}.'
  189. )
  190. warn(msg)
  191. else:
  192. if image.ndim > 2:
  193. raise ValueError(
  194. f'Bilateral filter is not implemented for '
  195. f'grayscale images of 3 or more dimensions, '
  196. f'but input image has {image.shape} shape. Use '
  197. f'``channel_axis=-1`` for 2D RGB images.'
  198. )
  199. if win_size is None:
  200. win_size = max(5, 2 * int(ceil(3 * sigma_spatial)) + 1)
  201. min_value = image.min()
  202. max_value = image.max()
  203. if min_value == max_value:
  204. return image
  205. # if image.max() is 0, then dist_scale can have an unverified value
  206. # and color_lut[<int>(dist * dist_scale)] may cause a segmentation fault
  207. # so we verify we have a positive image and that the max is not 0.0.
  208. image = np.atleast_3d(img_as_float(image))
  209. image = np.ascontiguousarray(image)
  210. sigma_color = sigma_color or image.std()
  211. color_lut = _compute_color_lut(bins, sigma_color, max_value, dtype=image.dtype)
  212. range_lut = _compute_spatial_lut(win_size, sigma_spatial, dtype=image.dtype)
  213. out = np.empty(image.shape, dtype=image.dtype)
  214. dims = image.shape[2]
  215. # There are a number of arrays needed in the Cython function.
  216. # It's easier to allocate them outside of Cython so that all
  217. # arrays are in the same type, then just copy the empty array
  218. # where needed within Cython.
  219. empty_dims = np.empty(dims, dtype=image.dtype)
  220. if min_value < 0:
  221. image = image - min_value
  222. max_value -= min_value
  223. _denoise_bilateral(
  224. image,
  225. max_value,
  226. win_size,
  227. sigma_color,
  228. sigma_spatial,
  229. bins,
  230. mode,
  231. cval,
  232. color_lut,
  233. range_lut,
  234. empty_dims,
  235. out,
  236. )
  237. # need to drop the added channels axis for grayscale images
  238. out = np.squeeze(out)
  239. if min_value < 0:
  240. out += min_value
  241. return out
  242. @utils.channel_as_last_axis()
  243. def denoise_tv_bregman(
  244. image, weight=5.0, max_num_iter=100, eps=1e-3, isotropic=True, *, channel_axis=None
  245. ):
  246. r"""Perform total variation denoising using split-Bregman optimization.
  247. Given :math:`f`, a noisy image (input data),
  248. total variation denoising (also known as total variation regularization)
  249. aims to find an image :math:`u` with less total variation than :math:`f`,
  250. under the constraint that :math:`u` remain similar to :math:`f`.
  251. This can be expressed by the Rudin--Osher--Fatemi (ROF) minimization
  252. problem:
  253. .. math::
  254. \min_{u} \sum_{i=0}^{N-1} \left( \left| \nabla{u_i} \right| + \frac{\lambda}{2}(f_i - u_i)^2 \right)
  255. where :math:`\lambda` is a positive parameter.
  256. The first term of this cost function is the total variation;
  257. the second term represents data fidelity. As :math:`\lambda \to 0`,
  258. the total variation term dominates, forcing the solution to have smaller
  259. total variation, at the expense of looking less like the input data.
  260. This code is an implementation of the split Bregman algorithm of Goldstein
  261. and Osher to solve the ROF problem ([1]_, [2]_, [3]_).
  262. Parameters
  263. ----------
  264. image : ndarray
  265. Input image to be denoised (converted using :func:`~.img_as_float`).
  266. weight : float, optional
  267. Denoising weight. It is equal to :math:`\frac{\lambda}{2}`. Therefore,
  268. the smaller the `weight`, the more denoising (at
  269. the expense of less similarity to `image`).
  270. eps : float, optional
  271. Tolerance :math:`\varepsilon > 0` for the stop criterion:
  272. The algorithm stops when :math:`\|u_n - u_{n-1}\|_2 < \varepsilon`.
  273. max_num_iter : int, optional
  274. Maximal number of iterations used for the optimization.
  275. isotropic : bool, optional
  276. Switch between isotropic and anisotropic TV denoising.
  277. channel_axis : int or None, optional
  278. If ``None``, the image is assumed to be grayscale (single-channel).
  279. Otherwise, this parameter indicates which axis of the array corresponds
  280. to channels.
  281. .. versionadded:: 0.19
  282. ``channel_axis`` was added in 0.19.
  283. Returns
  284. -------
  285. u : ndarray
  286. Denoised image.
  287. Notes
  288. -----
  289. Ensure that `channel_axis` parameter is set appropriately for color
  290. images.
  291. The principle of total variation denoising is explained in [4]_.
  292. It is about minimizing the total variation of an image,
  293. which can be roughly described as
  294. the integral of the norm of the image gradient. Total variation
  295. denoising tends to produce cartoon-like images, that is,
  296. piecewise-constant images.
  297. See Also
  298. --------
  299. denoise_tv_chambolle : Perform total variation denoising in nD.
  300. References
  301. ----------
  302. .. [1] Tom Goldstein and Stanley Osher, "The Split Bregman Method For L1
  303. Regularized Problems",
  304. https://ww3.math.ucla.edu/camreport/cam08-29.pdf
  305. .. [2] Pascal Getreuer, "Rudin–Osher–Fatemi Total Variation Denoising
  306. using Split Bregman" in Image Processing On Line on 2012–05–19,
  307. https://www.ipol.im/pub/art/2012/g-tvd/article_lr.pdf
  308. .. [3] https://web.math.ucsb.edu/~cgarcia/UGProjects/BregmanAlgorithms_JacquelineBush.pdf
  309. .. [4] https://en.wikipedia.org/wiki/Total_variation_denoising
  310. """
  311. image = np.atleast_3d(img_as_float(image))
  312. rows = image.shape[0]
  313. cols = image.shape[1]
  314. dims = image.shape[2]
  315. shape_ext = (rows + 2, cols + 2, dims)
  316. out = np.zeros(shape_ext, image.dtype)
  317. if channel_axis is not None:
  318. channel_out = np.zeros(shape_ext[:2] + (1,), dtype=out.dtype)
  319. for c in range(image.shape[-1]):
  320. # the algorithm below expects 3 dimensions to always be present.
  321. # slicing the array in this fashion preserves the channel dimension
  322. # for us
  323. channel_in = np.ascontiguousarray(image[..., c : c + 1])
  324. _denoise_tv_bregman(
  325. channel_in,
  326. image.dtype.type(weight),
  327. max_num_iter,
  328. eps,
  329. isotropic,
  330. channel_out,
  331. )
  332. out[..., c] = channel_out[..., 0]
  333. else:
  334. image = np.ascontiguousarray(image)
  335. _denoise_tv_bregman(
  336. image, image.dtype.type(weight), max_num_iter, eps, isotropic, out
  337. )
  338. return np.squeeze(out[1:-1, 1:-1])
  339. def _denoise_tv_chambolle_nd(image, weight=0.1, eps=2.0e-4, max_num_iter=200):
  340. """Perform total-variation denoising on n-dimensional images.
  341. Parameters
  342. ----------
  343. image : ndarray
  344. n-D input data to be denoised.
  345. weight : float, optional
  346. Denoising weight. The greater `weight`, the more denoising (at
  347. the expense of fidelity to `input`).
  348. eps : float, optional
  349. Relative difference of the value of the cost function that determines
  350. the stop criterion. The algorithm stops when:
  351. (E_(n-1) - E_n) < eps * E_0
  352. max_num_iter : int, optional
  353. Maximal number of iterations used for the optimization.
  354. Returns
  355. -------
  356. out : ndarray
  357. Denoised array of floats.
  358. Notes
  359. -----
  360. Rudin, Osher and Fatemi algorithm.
  361. """
  362. ndim = image.ndim
  363. p = np.zeros((image.ndim,) + image.shape, dtype=image.dtype)
  364. g = np.zeros_like(p)
  365. d = np.zeros_like(image)
  366. i = 0
  367. while i < max_num_iter:
  368. if i > 0:
  369. # d will be the (negative) divergence of p
  370. d = -p.sum(0)
  371. slices_d = [
  372. slice(None),
  373. ] * ndim
  374. slices_p = [
  375. slice(None),
  376. ] * (ndim + 1)
  377. for ax in range(ndim):
  378. slices_d[ax] = slice(1, None)
  379. slices_p[ax + 1] = slice(0, -1)
  380. slices_p[0] = ax
  381. d[tuple(slices_d)] += p[tuple(slices_p)]
  382. slices_d[ax] = slice(None)
  383. slices_p[ax + 1] = slice(None)
  384. out = image + d
  385. else:
  386. out = image
  387. E = (d**2).sum()
  388. # g stores the gradients of out along each axis
  389. # e.g. g[0] is the first order finite difference along axis 0
  390. slices_g = [
  391. slice(None),
  392. ] * (ndim + 1)
  393. for ax in range(ndim):
  394. slices_g[ax + 1] = slice(0, -1)
  395. slices_g[0] = ax
  396. g[tuple(slices_g)] = np.diff(out, axis=ax)
  397. slices_g[ax + 1] = slice(None)
  398. norm = np.sqrt((g**2).sum(axis=0))[np.newaxis, ...]
  399. E += weight * norm.sum()
  400. tau = 1.0 / (2.0 * ndim)
  401. norm *= tau / weight
  402. norm += 1.0
  403. p -= tau * g
  404. p /= norm
  405. E /= float(image.size)
  406. if i == 0:
  407. E_init = E
  408. E_previous = E
  409. else:
  410. if np.abs(E_previous - E) < eps * E_init:
  411. break
  412. else:
  413. E_previous = E
  414. i += 1
  415. return out
  416. def denoise_tv_chambolle(
  417. image, weight=0.1, eps=2.0e-4, max_num_iter=200, *, channel_axis=None
  418. ):
  419. r"""Perform total variation denoising in nD.
  420. Given :math:`f`, a noisy image (input data),
  421. total variation denoising (also known as total variation regularization)
  422. aims to find an image :math:`u` with less total variation than :math:`f`,
  423. under the constraint that :math:`u` remain similar to :math:`f`.
  424. This can be expressed by the Rudin--Osher--Fatemi (ROF) minimization
  425. problem:
  426. .. math::
  427. \min_{u} \sum_{i=0}^{N-1} \left( \left| \nabla{u_i} \right| + \frac{\lambda}{2}(f_i - u_i)^2 \right)
  428. where :math:`\lambda` is a positive parameter.
  429. The first term of this cost function is the total variation;
  430. the second term represents data fidelity. As :math:`\lambda \to 0`,
  431. the total variation term dominates, forcing the solution to have smaller
  432. total variation, at the expense of looking less like the input data.
  433. This code is an implementation of the algorithm proposed by Chambolle
  434. in [1]_ to solve the ROF problem.
  435. Parameters
  436. ----------
  437. image : ndarray
  438. Input image to be denoised. If its dtype is not float, it gets
  439. converted with :func:`~.img_as_float`.
  440. weight : float, optional
  441. Denoising weight. It is equal to :math:`\frac{1}{\lambda}`. Therefore,
  442. the greater the `weight`, the more denoising (at the expense of
  443. fidelity to `image`).
  444. eps : float, optional
  445. Tolerance :math:`\varepsilon > 0` for the stop criterion (compares to
  446. absolute value of relative difference of the cost function :math:`E`):
  447. The algorithm stops when :math:`|E_{n-1} - E_n| < \varepsilon * E_0`.
  448. max_num_iter : int, optional
  449. Maximal number of iterations used for the optimization.
  450. channel_axis : int or None, optional
  451. If ``None``, the image is assumed to be grayscale (single-channel).
  452. Otherwise, this parameter indicates which axis of the array corresponds
  453. to channels.
  454. .. versionadded:: 0.19
  455. ``channel_axis`` was added in 0.19.
  456. Returns
  457. -------
  458. u : ndarray
  459. Denoised image.
  460. Notes
  461. -----
  462. Make sure to set the `channel_axis` parameter appropriately for color
  463. images.
  464. The principle of total variation denoising is explained in [2]_.
  465. It is about minimizing the total variation of an image,
  466. which can be roughly described as
  467. the integral of the norm of the image gradient. Total variation
  468. denoising tends to produce cartoon-like images, that is,
  469. piecewise-constant images.
  470. See Also
  471. --------
  472. denoise_tv_bregman : Perform total variation denoising using split-Bregman
  473. optimization.
  474. References
  475. ----------
  476. .. [1] A. Chambolle, An algorithm for total variation minimization and
  477. applications, Journal of Mathematical Imaging and Vision,
  478. Springer, 2004, 20, 89-97.
  479. .. [2] https://en.wikipedia.org/wiki/Total_variation_denoising
  480. Examples
  481. --------
  482. 2D example on astronaut image:
  483. >>> from skimage import color, data
  484. >>> img = color.rgb2gray(data.astronaut())[:50, :50]
  485. >>> rng = np.random.default_rng()
  486. >>> img += 0.5 * img.std() * rng.standard_normal(img.shape)
  487. >>> denoised_img = denoise_tv_chambolle(img, weight=60)
  488. 3D example on synthetic data:
  489. >>> x, y, z = np.ogrid[0:20, 0:20, 0:20]
  490. >>> mask = (x - 22)**2 + (y - 20)**2 + (z - 17)**2 < 8**2
  491. >>> mask = mask.astype(float)
  492. >>> rng = np.random.default_rng()
  493. >>> mask += 0.2 * rng.standard_normal(mask.shape)
  494. >>> res = denoise_tv_chambolle(mask, weight=100)
  495. """
  496. im_type = image.dtype
  497. if not im_type.kind == 'f':
  498. image = img_as_float(image)
  499. # enforce float16->float32 and float128->float64
  500. float_dtype = _supported_float_type(image.dtype)
  501. image = image.astype(float_dtype, copy=False)
  502. if channel_axis is not None:
  503. channel_axis = channel_axis % image.ndim
  504. _at = functools.partial(utils.slice_at_axis, axis=channel_axis)
  505. out = np.zeros_like(image)
  506. for c in range(image.shape[channel_axis]):
  507. out[_at(c)] = _denoise_tv_chambolle_nd(
  508. image[_at(c)], weight, eps, max_num_iter
  509. )
  510. else:
  511. out = _denoise_tv_chambolle_nd(image, weight, eps, max_num_iter)
  512. return out
  513. def _bayes_thresh(details, var):
  514. """BayesShrink threshold for a zero-mean details coeff array."""
  515. # Equivalent to: dvar = np.var(details) for 0-mean details array
  516. dvar = np.mean(details * details)
  517. eps = np.finfo(details.dtype).eps
  518. thresh = var / np.sqrt(max(dvar - var, eps))
  519. return thresh
  520. def _universal_thresh(img, sigma):
  521. """Universal threshold used by the VisuShrink method"""
  522. return sigma * np.sqrt(2 * np.log(img.size))
  523. def _sigma_est_dwt(detail_coeffs, distribution='Gaussian'):
  524. """Calculate the robust median estimator of the noise standard deviation.
  525. Parameters
  526. ----------
  527. detail_coeffs : ndarray
  528. The detail coefficients corresponding to the discrete wavelet
  529. transform of an image.
  530. distribution : str
  531. The underlying noise distribution.
  532. Returns
  533. -------
  534. sigma : float
  535. The estimated noise standard deviation (see section 4.2 of [1]_).
  536. References
  537. ----------
  538. .. [1] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation
  539. by wavelet shrinkage." Biometrika 81.3 (1994): 425-455.
  540. :DOI:`10.1093/biomet/81.3.425`
  541. """
  542. # Consider regions with detail coefficients exactly zero to be masked out
  543. detail_coeffs = detail_coeffs[np.nonzero(detail_coeffs)]
  544. if distribution.lower() == 'gaussian':
  545. # 75th quantile of the underlying, symmetric noise distribution
  546. denom = scipy.stats.norm.ppf(0.75)
  547. sigma = np.median(np.abs(detail_coeffs)) / denom
  548. else:
  549. raise ValueError("Only Gaussian noise estimation is currently " "supported")
  550. return sigma
  551. def _wavelet_threshold(
  552. image,
  553. wavelet,
  554. method=None,
  555. threshold=None,
  556. sigma=None,
  557. mode='soft',
  558. wavelet_levels=None,
  559. ):
  560. """Perform wavelet thresholding.
  561. Parameters
  562. ----------
  563. image : ndarray (2d or 3d) of ints, uints or floats
  564. Input data to be denoised. `image` can be of any numeric type,
  565. but it is cast into an ndarray of floats for the computation
  566. of the denoised image.
  567. wavelet : string
  568. The type of wavelet to perform. Can be any of the options
  569. pywt.wavelist outputs. For example, this may be any of ``{db1, db2,
  570. db3, db4, haar}``.
  571. method : {'BayesShrink', 'VisuShrink'}, optional
  572. Thresholding method to be used. The currently supported methods are
  573. "BayesShrink" [1]_ and "VisuShrink" [2]_. If it is set to None, a
  574. user-specified ``threshold`` must be supplied instead.
  575. threshold : float, optional
  576. The thresholding value to apply during wavelet coefficient
  577. thresholding. The default value (None) uses the selected ``method`` to
  578. estimate appropriate threshold(s) for noise removal.
  579. sigma : float, optional
  580. The standard deviation of the noise. The noise is estimated when sigma
  581. is None (the default) by the method in [2]_.
  582. mode : {'soft', 'hard'}, optional
  583. An optional argument to choose the type of denoising performed. It
  584. noted that choosing soft thresholding given additive noise finds the
  585. best approximation of the original image.
  586. wavelet_levels : int or None, optional
  587. The number of wavelet decomposition levels to use. The default is
  588. three less than the maximum number of possible decomposition levels
  589. (see Notes below).
  590. Returns
  591. -------
  592. out : ndarray
  593. Denoised image.
  594. References
  595. ----------
  596. .. [1] Chang, S. Grace, Bin Yu, and Martin Vetterli. "Adaptive wavelet
  597. thresholding for image denoising and compression." Image Processing,
  598. IEEE Transactions on 9.9 (2000): 1532-1546.
  599. :DOI:`10.1109/83.862633`
  600. .. [2] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation
  601. by wavelet shrinkage." Biometrika 81.3 (1994): 425-455.
  602. :DOI:`10.1093/biomet/81.3.425`
  603. """
  604. try:
  605. import pywt
  606. except ImportError:
  607. raise ImportError(
  608. 'PyWavelets is not installed. Please ensure it is installed in '
  609. 'order to use this function.'
  610. )
  611. wavelet = pywt.Wavelet(wavelet)
  612. if not wavelet.orthogonal:
  613. warn(
  614. f'Wavelet thresholding was designed for '
  615. f'use with orthogonal wavelets. For nonorthogonal '
  616. f'wavelets such as {wavelet.name},results are '
  617. f'likely to be suboptimal.'
  618. )
  619. # original_extent is used to workaround PyWavelets issue #80
  620. # odd-sized input results in an image with 1 extra sample after waverecn
  621. original_extent = tuple(slice(s) for s in image.shape)
  622. # Determine the number of wavelet decomposition levels
  623. if wavelet_levels is None:
  624. # Determine the maximum number of possible levels for image
  625. wavelet_levels = pywt.dwtn_max_level(image.shape, wavelet)
  626. # Skip coarsest wavelet scales (see Notes in docstring).
  627. wavelet_levels = max(wavelet_levels - 3, 1)
  628. coeffs = pywt.wavedecn(image, wavelet=wavelet, level=wavelet_levels)
  629. # Detail coefficients at each decomposition level
  630. dcoeffs = coeffs[1:]
  631. if sigma is None:
  632. # Estimate the noise via the method in [2]_
  633. detail_coeffs = dcoeffs[-1]['d' * image.ndim]
  634. sigma = _sigma_est_dwt(detail_coeffs, distribution='Gaussian')
  635. if method is not None and threshold is not None:
  636. warn(
  637. f'Thresholding method {method} selected. The '
  638. f'user-specified threshold will be ignored.'
  639. )
  640. if threshold is None:
  641. var = sigma**2
  642. if method is None:
  643. raise ValueError("If method is None, a threshold must be provided.")
  644. elif method == "BayesShrink":
  645. # The BayesShrink thresholds from [1]_ in docstring
  646. threshold = [
  647. {key: _bayes_thresh(level[key], var) for key in level}
  648. for level in dcoeffs
  649. ]
  650. elif method == "VisuShrink":
  651. # The VisuShrink thresholds from [2]_ in docstring
  652. threshold = _universal_thresh(image, sigma)
  653. else:
  654. raise ValueError(f'Unrecognized method: {method}')
  655. if np.isscalar(threshold):
  656. # A single threshold for all coefficient arrays
  657. denoised_detail = [
  658. {
  659. key: pywt.threshold(level[key], value=threshold, mode=mode)
  660. for key in level
  661. }
  662. for level in dcoeffs
  663. ]
  664. else:
  665. # Dict of unique threshold coefficients for each detail coeff. array
  666. denoised_detail = [
  667. {
  668. key: pywt.threshold(level[key], value=thresh[key], mode=mode)
  669. for key in level
  670. }
  671. for thresh, level in zip(threshold, dcoeffs)
  672. ]
  673. denoised_coeffs = [coeffs[0]] + denoised_detail
  674. out = pywt.waverecn(denoised_coeffs, wavelet)[original_extent]
  675. out = out.astype(image.dtype)
  676. return out
  677. def _scale_sigma_and_image_consistently(image, sigma, multichannel, rescale_sigma):
  678. """If the ``image`` is rescaled, also rescale ``sigma`` consistently.
  679. Images that are not floating point will be rescaled via ``img_as_float``.
  680. Half-precision images will be promoted to single precision.
  681. """
  682. if multichannel:
  683. if isinstance(sigma, numbers.Number) or sigma is None:
  684. sigma = [sigma] * image.shape[-1]
  685. elif len(sigma) != image.shape[-1]:
  686. raise ValueError(
  687. "When channel_axis is not None, sigma must be a scalar or have "
  688. "length equal to the number of channels"
  689. )
  690. if image.dtype.kind != 'f':
  691. if rescale_sigma:
  692. range_pre = image.max() - image.min()
  693. image = img_as_float(image)
  694. if rescale_sigma:
  695. range_post = image.max() - image.min()
  696. # apply the same magnitude scaling to sigma
  697. scale_factor = range_post / range_pre
  698. if multichannel:
  699. sigma = [s * scale_factor if s is not None else s for s in sigma]
  700. elif sigma is not None:
  701. sigma *= scale_factor
  702. elif image.dtype == np.float16:
  703. image = image.astype(np.float32)
  704. return image, sigma
  705. def _rescale_sigma_rgb2ycbcr(sigmas):
  706. """Convert user-provided noise standard deviations to YCbCr space.
  707. Notes
  708. -----
  709. If R, G, B are linearly independent random variables and a1, a2, a3 are
  710. scalars, then random variable C:
  711. C = a1 * R + a2 * G + a3 * B
  712. has variance, var_C, given by:
  713. var_C = a1**2 * var_R + a2**2 * var_G + a3**2 * var_B
  714. """
  715. if sigmas[0] is None:
  716. return sigmas
  717. sigmas = np.asarray(sigmas)
  718. rgv_variances = sigmas * sigmas
  719. for i in range(3):
  720. scalars = ycbcr_from_rgb[i, :]
  721. var_channel = np.sum(scalars * scalars * rgv_variances)
  722. sigmas[i] = np.sqrt(var_channel)
  723. return sigmas
  724. @utils.channel_as_last_axis()
  725. def denoise_wavelet(
  726. image,
  727. sigma=None,
  728. wavelet='db1',
  729. mode='soft',
  730. wavelet_levels=None,
  731. convert2ycbcr=False,
  732. method='BayesShrink',
  733. rescale_sigma=True,
  734. *,
  735. channel_axis=None,
  736. ):
  737. """Perform wavelet denoising on an image.
  738. Parameters
  739. ----------
  740. image : ndarray (M[, N[, ...P]][, C]) of ints, uints or floats
  741. Input data to be denoised. `image` can be of any numeric type,
  742. but it is cast into an ndarray of floats for the computation
  743. of the denoised image.
  744. sigma : float or list, optional
  745. The noise standard deviation used when computing the wavelet detail
  746. coefficient threshold(s). When None (default), the noise standard
  747. deviation is estimated via the method in [2]_.
  748. wavelet : str, optional
  749. The type of wavelet to perform and can be any of the options
  750. ``pywt.wavelist`` outputs. The default is `'db1'`. For example,
  751. ``wavelet`` can be any of ``{'db2', 'haar', 'sym9'}`` and many more.
  752. mode : {'soft', 'hard'}, optional
  753. An optional argument to choose the type of denoising performed. It
  754. noted that choosing soft thresholding given additive noise finds the
  755. best approximation of the original image.
  756. wavelet_levels : int or None, optional
  757. The number of wavelet decomposition levels to use. The default is
  758. three less than the maximum number of possible decomposition levels.
  759. convert2ycbcr : bool, optional
  760. If True and channel_axis is set, do the wavelet denoising in the YCbCr
  761. colorspace instead of the RGB color space. This typically results in
  762. better performance for RGB images.
  763. method : {'BayesShrink', 'VisuShrink'}, optional
  764. Thresholding method to be used. The currently supported methods are
  765. "BayesShrink" [1]_ and "VisuShrink" [2]_. Defaults to "BayesShrink".
  766. rescale_sigma : bool, optional
  767. If False, no rescaling of the user-provided ``sigma`` will be
  768. performed. The default of ``True`` rescales sigma appropriately if the
  769. image is rescaled internally.
  770. .. versionadded:: 0.16
  771. ``rescale_sigma`` was introduced in 0.16
  772. channel_axis : int or None, optional
  773. If ``None``, the image is assumed to be grayscale (single-channel).
  774. Otherwise, this parameter indicates which axis of the array corresponds
  775. to channels.
  776. .. versionadded:: 0.19
  777. ``channel_axis`` was added in 0.19.
  778. Returns
  779. -------
  780. out : ndarray
  781. Denoised image.
  782. Notes
  783. -----
  784. The wavelet domain is a sparse representation of the image, and can be
  785. thought of similarly to the frequency domain of the Fourier transform.
  786. Sparse representations have most values zero or near-zero and truly random
  787. noise is (usually) represented by many small values in the wavelet domain.
  788. Setting all values below some threshold to 0 reduces the noise in the
  789. image, but larger thresholds also decrease the detail present in the image.
  790. If the input is 3D, this function performs wavelet denoising on each color
  791. plane separately.
  792. .. versionchanged:: 0.16
  793. For floating point inputs, the original input range is maintained and
  794. there is no clipping applied to the output. Other input types will be
  795. converted to a floating point value in the range [-1, 1] or [0, 1]
  796. depending on the input image range. Unless ``rescale_sigma = False``,
  797. any internal rescaling applied to the ``image`` will also be applied
  798. to ``sigma`` to maintain the same relative amplitude.
  799. Many wavelet coefficient thresholding approaches have been proposed. By
  800. default, ``denoise_wavelet`` applies BayesShrink, which is an adaptive
  801. thresholding method that computes separate thresholds for each wavelet
  802. sub-band as described in [1]_.
  803. If ``method == "VisuShrink"``, a single "universal threshold" is applied to
  804. all wavelet detail coefficients as described in [2]_. This threshold
  805. is designed to remove all Gaussian noise at a given ``sigma`` with high
  806. probability, but tends to produce images that appear overly smooth.
  807. Although any of the wavelets from ``PyWavelets`` can be selected, the
  808. thresholding methods assume an orthogonal wavelet transform and may not
  809. choose the threshold appropriately for biorthogonal wavelets. Orthogonal
  810. wavelets are desirable because white noise in the input remains white noise
  811. in the subbands. Biorthogonal wavelets lead to colored noise in the
  812. subbands. Additionally, the orthogonal wavelets in PyWavelets are
  813. orthonormal so that noise variance in the subbands remains identical to the
  814. noise variance of the input. Example orthogonal wavelets are the Daubechies
  815. (e.g. 'db2') or symmlet (e.g. 'sym2') families.
  816. References
  817. ----------
  818. .. [1] Chang, S. Grace, Bin Yu, and Martin Vetterli. "Adaptive wavelet
  819. thresholding for image denoising and compression." Image Processing,
  820. IEEE Transactions on 9.9 (2000): 1532-1546.
  821. :DOI:`10.1109/83.862633`
  822. .. [2] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation
  823. by wavelet shrinkage." Biometrika 81.3 (1994): 425-455.
  824. :DOI:`10.1093/biomet/81.3.425`
  825. Examples
  826. --------
  827. >>> from skimage import color, data
  828. >>> img = img_as_float(data.astronaut())
  829. >>> img = color.rgb2gray(img)
  830. >>> rng = np.random.default_rng()
  831. >>> img += 0.1 * rng.standard_normal(img.shape)
  832. >>> img = np.clip(img, 0, 1)
  833. >>> denoised_img = denoise_wavelet(img, sigma=0.1, rescale_sigma=True)
  834. """
  835. multichannel = channel_axis is not None
  836. if method not in ["BayesShrink", "VisuShrink"]:
  837. raise ValueError(
  838. f'Invalid method: {method}. The currently supported '
  839. f'methods are "BayesShrink" and "VisuShrink".'
  840. )
  841. # floating-point inputs are not rescaled, so don't clip their output.
  842. clip_output = image.dtype.kind != 'f'
  843. if convert2ycbcr and not multichannel:
  844. raise ValueError("convert2ycbcr requires channel_axis to be set")
  845. image, sigma = _scale_sigma_and_image_consistently(
  846. image, sigma, multichannel, rescale_sigma
  847. )
  848. if multichannel:
  849. if convert2ycbcr:
  850. out = color.rgb2ycbcr(image)
  851. # convert user-supplied sigmas to the new colorspace as well
  852. if rescale_sigma:
  853. sigma = _rescale_sigma_rgb2ycbcr(sigma)
  854. for i in range(3):
  855. # renormalizing this color channel to live in [0, 1]
  856. _min, _max = out[..., i].min(), out[..., i].max()
  857. scale_factor = _max - _min
  858. if scale_factor == 0:
  859. # skip any channel containing only zeros!
  860. continue
  861. channel = out[..., i] - _min
  862. channel /= scale_factor
  863. sigma_channel = sigma[i]
  864. if sigma_channel is not None:
  865. sigma_channel /= scale_factor
  866. out[..., i] = denoise_wavelet(
  867. channel,
  868. wavelet=wavelet,
  869. method=method,
  870. sigma=sigma_channel,
  871. mode=mode,
  872. wavelet_levels=wavelet_levels,
  873. rescale_sigma=rescale_sigma,
  874. )
  875. out[..., i] = out[..., i] * scale_factor
  876. out[..., i] += _min
  877. out = color.ycbcr2rgb(out)
  878. else:
  879. out = np.empty_like(image)
  880. for c in range(image.shape[-1]):
  881. out[..., c] = _wavelet_threshold(
  882. image[..., c],
  883. wavelet=wavelet,
  884. method=method,
  885. sigma=sigma[c],
  886. mode=mode,
  887. wavelet_levels=wavelet_levels,
  888. )
  889. else:
  890. out = _wavelet_threshold(
  891. image,
  892. wavelet=wavelet,
  893. method=method,
  894. sigma=sigma,
  895. mode=mode,
  896. wavelet_levels=wavelet_levels,
  897. )
  898. if clip_output:
  899. clip_range = (-1, 1) if image.min() < 0 else (0, 1)
  900. out = np.clip(out, *clip_range, out=out)
  901. return out
  902. def estimate_sigma(image, average_sigmas=False, *, channel_axis=None):
  903. """
  904. Robust wavelet-based estimator of the (Gaussian) noise standard deviation.
  905. Parameters
  906. ----------
  907. image : ndarray
  908. Image for which to estimate the noise standard deviation.
  909. average_sigmas : bool, optional
  910. If true, average the channel estimates of `sigma`. Otherwise return
  911. a list of sigmas corresponding to each channel.
  912. channel_axis : int or None, optional
  913. If ``None``, the image is assumed to be grayscale (single-channel).
  914. Otherwise, this parameter indicates which axis of the array corresponds
  915. to channels.
  916. .. versionadded:: 0.19
  917. ``channel_axis`` was added in 0.19.
  918. Returns
  919. -------
  920. sigma : float or list
  921. Estimated noise standard deviation(s). If `multichannel` is True and
  922. `average_sigmas` is False, a separate noise estimate for each channel
  923. is returned. Otherwise, the average of the individual channel
  924. estimates is returned.
  925. Notes
  926. -----
  927. This function assumes the noise follows a Gaussian distribution. The
  928. estimation algorithm is based on the median absolute deviation of the
  929. wavelet detail coefficients as described in section 4.2 of [1]_.
  930. References
  931. ----------
  932. .. [1] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation
  933. by wavelet shrinkage." Biometrika 81.3 (1994): 425-455.
  934. :DOI:`10.1093/biomet/81.3.425`
  935. Examples
  936. --------
  937. >>> import skimage.data
  938. >>> from skimage import img_as_float
  939. >>> img = img_as_float(skimage.data.camera())
  940. >>> sigma = 0.1
  941. >>> rng = np.random.default_rng()
  942. >>> img = img + sigma * rng.standard_normal(img.shape)
  943. >>> sigma_hat = estimate_sigma(img, channel_axis=None)
  944. """
  945. try:
  946. import pywt
  947. except ImportError:
  948. raise ImportError(
  949. 'PyWavelets is not installed. Please ensure it is installed in '
  950. 'order to use this function.'
  951. )
  952. if channel_axis is not None:
  953. channel_axis = channel_axis % image.ndim
  954. _at = functools.partial(utils.slice_at_axis, axis=channel_axis)
  955. nchannels = image.shape[channel_axis]
  956. sigmas = [
  957. estimate_sigma(image[_at(c)], channel_axis=None) for c in range(nchannels)
  958. ]
  959. if average_sigmas:
  960. sigmas = np.mean(sigmas)
  961. return sigmas
  962. elif image.shape[-1] <= 4:
  963. msg = (
  964. f'image is size {image.shape[-1]} on the last axis, '
  965. f'but channel_axis is None. If this is a color image, '
  966. f'please set channel_axis=-1 for proper noise estimation.'
  967. )
  968. warn(msg)
  969. coeffs = pywt.dwtn(image, wavelet='db2')
  970. detail_coeffs = coeffs['d' * image.ndim]
  971. return _sigma_est_dwt(detail_coeffs, distribution='Gaussian')