
    9i>                     \    S r SSKrSSKJr  SSKJr  SSKJr  SS jr	 SSS	.S
 jjr
SS jrg)z%Implementations restoration functions    N)convolve   )_supported_float_type   )uftc                    Uc,  [         R                  " U R                  U R                  US9u  p6[        R
                  " U5      (       d  [         R                  " X0R                  US9n[        U R                  5      nU R                  USS9n UR                  R                  USS9nUR                  R                  USS9nUR                  UR                  :w  a   [         R                  " XR                  US9nOUn[        R                  " U5      [        R                  " U5      S-  U[        R                  " U5      S-  -  -   -  n	U(       a7  [         R                  " U	[         R                  " U 5      -  U R                  S9n
O-[         R                  " U	[         R                   " U 5      -  5      n
U(       a  SXS:  '   SXS:  '   U
$ )u  Wiener-Hunt deconvolution

Return the deconvolution with a Wiener-Hunt approach (i.e. with
Fourier diagonalisation).

Parameters
----------
image : ndarray
   Input degraded image (can be n-dimensional).
psf : ndarray
   Point Spread Function. This is assumed to be the impulse
   response (input image space) if the data-type is real, or the
   transfer function (Fourier space) if the data-type is
   complex. There is no constraints on the shape of the impulse
   response. The transfer function must be of shape
   `(N1, N2, ..., ND)` if `is_real is True`,
   `(N1, N2, ..., ND // 2 + 1)` otherwise (see `np.fft.rfftn`).
balance : float
   The regularisation parameter value that tunes the balance
   between the data adequacy that improve frequency restoration
   and the prior adequacy that reduce frequency restoration (to
   avoid noise artifacts).
reg : ndarray, optional
   The regularisation operator. The Laplacian by default. It can
   be an impulse response or a transfer function, as for the
   psf. Shape constraint is the same as for the `psf` parameter.
is_real : boolean, optional
   True by default. Specify if ``psf`` and ``reg`` are provided
   with hermitian hypothesis, that is only half of the frequency
   plane is provided (due to the redundancy of Fourier transform
   of real signal). It's apply only if ``psf`` and/or ``reg`` are
   provided as transfer function.  For the hermitian property see
   ``uft`` module or ``np.fft.rfftn``.
clip : boolean, optional
   True by default. If True, pixel values of the result above 1 or
   under -1 are thresholded for skimage pipeline compatibility.

Returns
-------
im_deconv : (M, N) ndarray
   The deconvolved image.

Examples
--------
>>> from skimage import color, data, restoration
>>> img = color.rgb2gray(data.astronaut())
>>> from scipy.signal import convolve2d
>>> psf = np.ones((5, 5)) / 25
>>> img = convolve2d(img, psf, 'same')
>>> rng = np.random.default_rng()
>>> img += 0.1 * img.std() * rng.standard_normal(img.shape)
>>> deconvolved_img = restoration.wiener(img, psf, 0.1)

Notes
-----
This function applies the Wiener filter to a noisy and degraded
image by an impulse response (or PSF). If the data model is

.. math:: y = Hx + n

where :math:`n` is noise, :math:`H` the PSF and :math:`x` the
unknown original image, the Wiener filter is

.. math::
   \hat x = F^\dagger \left( |\Lambda_H|^2 + \lambda |\Lambda_D|^2 \right)^{-1}
   \Lambda_H^\dagger F y

where :math:`F` and :math:`F^\dagger` are the Fourier and inverse
Fourier transforms respectively, :math:`\Lambda_H` the transfer
function (or the Fourier transform of the PSF, see [Hunt] below)
and :math:`\Lambda_D` the filter to penalize the restored image
frequencies (Laplacian by default, that is penalization of high
frequency). The parameter :math:`\lambda` tunes the balance
between the data (that tends to increase high frequency, even
those coming from noise), and the regularization.

These methods are then specific to a prior model. Consequently,
the application or the true image nature must correspond to the
prior model. By default, the prior model (Laplacian) introduce
image smoothness or pixel correlation. It can also be interpreted
as high-frequency penalization to compensate the instability of
the solution with respect to the data (sometimes called noise
amplification or "explosive" solution).

Finally, the use of Fourier space implies a circulant property of
:math:`H`, see [2]_.

References
----------
.. [1] François Orieux, Jean-François Giovannelli, and Thomas
       Rodet, "Bayesian estimation of regularization and point
       spread function parameters for Wiener-Hunt deconvolution",
       J. Opt. Soc. Am. A 27, 1593-1607 (2010)

       https://www.osapublishing.org/josaa/abstract.cfm?URI=josaa-27-7-1593

       https://hal.archives-ouvertes.fr/hal-00674508

.. [2] B. R. Hunt "A matrix theory proof of the discrete
       convolution theorem", IEEE Trans. on Audio and
       Electroacoustics, vol. au-19, no. 4, pp. 285-288, dec. 1971
is_realFcopyr   shaper   )r   	laplacianndimr   npiscomplexobjir2tfr   dtypeastyperealconjabsuirfftnurfftnuifftnufftn)imagepsfbalanceregr
   clip_
float_type
trans_funcwiener_filterdeconvs              a/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/restoration/deconvolution.pywienerr)   
   s\   N {uzz5;;H??3ii[[':&u{{3JLL%L0E
((//*5/
1C
((//*5/
1C
yyCIIYYsKKA

GGJ'
za'BFF3K1,<"<<M ]SZZ->>ekkRMCIIe,<<=z {M    )rngc                   SSSSSS.nUR                  U=(       d    0 5        Uc,  [        R                  " U R                  U R                  US9u  p([
        R                  " U5      (       d  [        R                  " X R                  US9n[        U R                  5      n	U R                  U	SS	9n UR                  R                  U	SS	9nUR                  R                  U	SS	9nUR                  UR                  :w  a   [        R                  " XR                  US9n
OUn
[
        R                  " U
R                  U	S
9n[
        R                  " U
R                  U	S
9n[
        R                  nS/S/p[
        R                  " U5      S-  n[
        R                  " U
5      S-  nU(       a  [        R                  " U 5      nO[        R                   " U 5      n[
        R"                  R%                  U5      n['        US   5       GH  nUS   U-  US   U-  -   nUR)                  UR                  5      nUR                  U	SS	9nUR)                  UR                  5      nUR                  U	SS	9n[
        R*                  " SU-  5      USU-  -   -  nUS   [
        R,                  " U
5      -  U-  nUU-  U-   nUS   (       a  US   " U5        UR/                  UR1                  U R2                  S-  S[        R4                  " UUU
-  -
  5      -  5      5        UR/                  UR1                  U R2                  S-
  S-  S[        R4                  " UU-  5      -  5      5        UUS   :  a  UU-   nUUS   S-   :  az  UUUS   -
  -  nUUUS   -
  S-
  -  n[
        R6                  " [
        R                  " UU-
  5      5      [
        R6                  " [
        R                  " U5      5      -  UUS   -
  -  nUnUUS   :  d  GM  XS   :  d  GM    O   UWUS   -
  -  nU(       a  [        R8                  " XR                  S9nO[        R:                  " U5      nU(       a  SXS:  '   SXS:  '   XUS.4$ )u  Unsupervised Wiener-Hunt deconvolution.

Return the deconvolution with a Wiener-Hunt approach, where the
hyperparameters are automatically estimated. The algorithm is a
stochastic iterative process (Gibbs sampler) described in the
reference below. See also ``wiener`` function.

Parameters
----------
image : (M, N) ndarray
   The input degraded image.
psf : ndarray
   The impulse response (input image's space) or the transfer
   function (Fourier space). Both are accepted. The transfer
   function is automatically recognized as being complex
   (``np.iscomplexobj(psf)``).
reg : ndarray, optional
   The regularisation operator. The Laplacian by default. It can
   be an impulse response or a transfer function, as for the psf.
user_params : dict, optional
   Dictionary of parameters for the Gibbs sampler. See below.
clip : boolean, optional
   True by default. If true, pixel values of the result above 1 or
   under -1 are thresholded for skimage pipeline compatibility.
rng : {`numpy.random.Generator`, int}, optional
    Pseudo-random number generator.
    By default, a PCG64 generator is used (see :func:`numpy.random.default_rng`).
    If `rng` is an int, it is used to seed the generator.

    .. versionadded:: 0.19

Returns
-------
x_postmean : (M, N) ndarray
   The deconvolved image (the posterior mean).
chains : dict
   The keys ``noise`` and ``prior`` contain the chain list of
   noise and prior precision respectively.

Other parameters
----------------
The keys of ``user_params`` are:

threshold : float
   The stopping criterion: the norm of the difference between to
   successive approximated solution (empirical mean of object
   samples, see Notes section). 1e-4 by default.
burnin : int
   The number of sample to ignore to start computation of the
   mean. 15 by default.
min_num_iter : int
   The minimum number of iterations. 30 by default.
max_num_iter : int
   The maximum number of iterations if ``threshold`` is not
   satisfied. 200 by default.
callback : callable (None by default)
   A user provided callable to which is passed, if the function
   exists, the current image sample for whatever purpose. The user
   can store the sample, or compute other moments than the
   mean. It has no influence on the algorithm execution and is
   only for inspection.

Examples
--------
>>> from skimage import color, data, restoration
>>> img = color.rgb2gray(data.astronaut())
>>> from scipy.signal import convolve2d
>>> psf = np.ones((5, 5)) / 25
>>> img = convolve2d(img, psf, 'same')
>>> rng = np.random.default_rng()
>>> img += 0.1 * img.std() * rng.standard_normal(img.shape)
>>> deconvolved_img = restoration.unsupervised_wiener(img, psf)

Notes
-----
The estimated image is design as the posterior mean of a
probability law (from a Bayesian analysis). The mean is defined as
a sum over all the possible images weighted by their respective
probability. Given the size of the problem, the exact sum is not
tractable. This algorithm use of MCMC to draw image under the
posterior law. The practical idea is to only draw highly probable
images since they have the biggest contribution to the mean. At the
opposite, the less probable images are drawn less often since
their contribution is low. Finally, the empirical mean of these
samples give us an estimation of the mean, and an exact
computation with an infinite sample set.

References
----------
.. [1] François Orieux, Jean-François Giovannelli, and Thomas
       Rodet, "Bayesian estimation of regularization and point
       spread function parameters for Wiener-Hunt deconvolution",
       J. Opt. Soc. Am. A 27, 1593-1607 (2010)

       https://www.osapublishing.org/josaa/abstract.cfm?URI=josaa-27-7-1593

       https://hal.archives-ouvertes.fr/hal-00674508
g-C6?         N)	thresholdmax_num_itermin_num_iterburnincallbackr	   Fr   r   r   r   r1   r         ?y              ?r4   r3   r2   r0   r   )noiseprior)updater   r   r   r   r   r   r   r   r   r   r   zerosnanr   urfft2ufft2randomdefault_rngrangestandard_normalsqrtr   appendgammasizeimage_quad_normsumuirfft2uifft2)r   r   r!   user_paramsr
   r"   r+   paramsr#   r$   	trans_fct
x_postmeanprev_x_postmeandeltagn_chaingx_chainareg2atf2data_spectrum	iteration	precision_rand1_rand2	excursionr&   x_samplecurrentpreviouss                               r(   unsupervised_wienerr]      s   L F MM+#$
{uzz5;;H??3ii[[':&u{{3JLL%L0E
((//*5/
1C
((//*5/
1C
yyCIIIIc;;@		 )//<JhhyjAO FFE qch FF3K1E66)!D 

5)		%(
))


$C 6.12	 RL4'(2,*>>	 $$]%8%89z6$$]%8%89z6GGC)O,f0DE	 !rwwy'99IE !=09<*:x( 	II

QC''98L(LMM	
 	IIuzzA~*A0C0CHsN0S,ST	

 vh''(83Jx(1,- Ix0@$@AG&)fX6F*F*JKH rvvg012&&
+,-vh//1  % ~..UK=P5Pk 3p y6(+;;<J[[;;?
ZZ
+
%&
>"&(
?#X>??r*   c                    [        U R                  5      nU R                  USS9n UR                  USS9n[        R                  " U R
                  SUS9n[        R                  " U5      nSn[        U5       HE  n	[        XaSS9U-   n
U(       a  [        R                  " X:  SX
-  5      nOX
-  nU[        XSS9-  nMG     U(       a  S	XfS	:  '   S
XfS
:  '   U$ )a  Richardson-Lucy deconvolution.

Parameters
----------
image : ([P, ]M, N) ndarray
   Input degraded image (can be n-dimensional). If you keep the
   default `clip=True` parameter, you may want to normalize
   the image so that its values fall in the [-1, 1] interval to avoid
   information loss.
psf : ndarray
   The point spread function.
num_iter : int, optional
   Number of iterations. This parameter plays the role of
   regularisation.
clip : boolean, optional
   True by default. If true, pixel value of the result above 1 or
   under -1 are thresholded for skimage pipeline compatibility.
filter_epsilon: float, optional
   Value below which intermediate results become 0 to avoid division
   by small numbers.

Returns
-------
im_deconv : ndarray
   The deconvolved image.

Examples
--------
>>> from skimage import img_as_float, data, restoration
>>> camera = img_as_float(data.camera())
>>> from scipy.signal import convolve2d
>>> psf = np.ones((5, 5)) / 25
>>> camera = convolve2d(camera, psf, 'same')
>>> rng = np.random.default_rng()
>>> camera += 0.1 * camera.std() * rng.standard_normal(camera.shape)
>>> deconvolved = restoration.richardson_lucy(camera, psf, 5)

References
----------
.. [1] https://en.wikipedia.org/wiki/Richardson%E2%80%93Lucy_deconvolution
Fr   r6   r5   g-q=same)moder   r   r   )
r   r   r   r   fullr   flipr@   r   where)r   r   num_iterr"   filter_epsilonr$   	im_deconv
psf_mirrorepsr#   convrelative_blurs               r(   richardson_lucyrk   i  s    T 'u{{3JLL%L0E
**Ze*
,CS
;IJ C8_	V4s:HHT%:Au|LM!LMXmfEE	  #$	a- $&	b.!r*   )NTT)NNTT)2   TN)__doc__numpyr   scipy.signalr   _shared.utilsr    r   r)   r]   rk    r*   r(   <module>rs      s9    +  ! 1 AJ @DX@LPX@v?r*   