
    0-Phq1                         d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
Z
ddlmZ ddlmZ dd	lmZ d
 ZefdZd Z ej                    dddd            ZdS )    N)sparse)spsolve)laplace   )utils)label   )_build_matrix_innerc                 p    t          j        | |z
  d          }t          j        | |z   dz   |          }||fS )Nr   r	   )npmaximumminimum)nd_idxradiusnd_shape	bounds_lo	bounds_his        [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/restoration/inpaint.py_get_neighborhoodr      s<    
6F?A..I
6F?Q.99Ii    c                     t          j        | |          }d||<   t          t          |                    }t          j        |          }||         }t          j        |d          }|||fS )Ndtyper	   r   axis)r   zerosr   wherestack)shapecenterr   
neigh_coefcoef_idx	coef_valss         r   _get_neigh_coefr$      sq    %u---JJv,,--J x
##H8$Ixq)))Hx**r   c                    |j         d         }|j         d         dz  }t          j        |j         t                    }d|t	          ||           f|j        z  <   ||z  }	| |z  }
t          j        |	          }t          j        |	          }t          j        |
          }t          j        ||f          }t          j        |
          }t          d t          ||          D                       }|dk    }t          j        ||t          j        d          }||                                         }t          j        |          }t          j        |          }|t          j        ||k              z
  }t          j        |t          j                  }t          j        ||f|j                  }t          j        |t          j                  }t          j        |t          j                  }t          j        ||j                  }i }|                    d          }t          j        |                    d|f                    }d}d} d}!t          j        |d	          }t/          |          D ]\  }!}"t1          |"||j                   \  }#}$t          |$|#z
            }%t          |"|#z
            }&|                    |%|&fd
          \  }'}(|'$t5          |%|&|j                  \  })}'}(|'|(f||%|&f<   |'|#ddt          j        f         z   }'t          j        |'|j                   }*d}+t          |(|*          D ]H\  },}-||-         r|!|| <   |-|| <   |,|| <   | dz  } "||ddfxx         |,||-ddf         z  z  cc<   |+dz  }+I|+r
|!||<   |dz  } |!dz   }.|}/| }0t;          |.|/|0||||||||||          }1|d|1         }|d|1ddf         }||j        f}2t?          j         |||ff|2          }3|3dd|f         }3t          j        ||f|j                  }4||4|ddf<   tC          |3|4dd          }5|5j        dk    r|5ddt          j        f         }5|5||<   |S )a  Solve a (sparse) linear system corresponding to biharmonic inpainting.

    This function creates a linear system of the form:

    ``A @ u = b``

    where ``A`` is a sparse matrix, ``b`` is a vector enforcing smoothness and
    boundary constraints and ``u`` is the vector of inpainted values to be
    (uniquely) determined by solving the linear system.

    ``A`` is a sparse matrix of shape (n_mask, n_mask) where ``n_mask``
    corresponds to the number of non-zero values in ``mask`` (i.e. the number
    of pixels to be inpainted). Each row in A will have a number of non-zero
    values equal to the number of non-zero values in the biharmonic kernel,
    ``neigh_coef_full``. In practice, biharmonic kernels with reduced extent
    are used at the image borders. This matrix, ``A`` is the same for all
    image channels (since the same inpainting mask is currently used for all
    channels).

    ``u`` is a dense matrix of shape ``(n_mask, n_channels)`` and represents
    the vector of unknown values for each channel.

    ``b`` is a dense matrix of shape ``(n_mask, n_channels)`` and represents
    the desired output of convolving the solution with the biharmonic kernel.
    At mask locations where there is no overlap with known values, ``b`` will
    have a value of 0. This enforces the biharmonic smoothness constraint in
    the interior of inpainting regions. For regions near the boundary that
    overlap with known values, the entries in ``b`` enforce boundary conditions
    designed to avoid discontinuity with the known values.
    r   r   r   c                 @    g | ]\  }}t          j        ||f          S  )r   concatenate).0bcs      r   
<listcomp>z5_inpaint_biharmonic_single_region.<locals>.<listcomp>Q   s*    WWWAbnaV,,WWWr   constant)outputmoder	   r   )NNN)r   FMMD_ATA)use_umfpack
permc_spec)"r   r   onesboolslicendimr   flatnonzeror)   tuplezipndiconvolveuint8sumcount_nonzeroemptyintpr   r   reshapeascontiguousarrayr   	enumerater   getr$   newaxisravel_multi_indexr
   sizer   	csr_arrayr   )6imagemaskoutneigh_coef_fullr#   raveled_offsets
n_channelsr   	edge_maskboundary_maskcenter_maskboundary_pts
boundary_icenter_imask_i
center_ptsmask_pts	structuretmp
nnz_matrixn_maskn_structnnz_rhs_vector_maxrow_idx_known
data_knownrow_idx_unknowncol_idx_unknowndata_unknown
coef_cache	mask_flatout_flat	idx_knownidx_unknown	mask_pt_nr   b_lob_hi
coef_shapecoef_centerr"   coefs_index1dnvalscoefi	row_startknown_start_idxunknown_start_idxnnz_rhssp_shapematrix_unknownrhsresults6                                                         r   !_inpaint_biharmonic_single_regionr|   !   s   D 2J"1%*F
$///I78IuVfW%%'$)34$M*t#K8M**L..J~k**H^Z233F+&&JWW\:9V9VWWWXXH  1$I
,tYrxj
I
I
ICTJ d##F	**H""23(?"C"CC H/rw???M-z:#)LLLJhz999Ohz999O8Jci888L J R  I#CKKZ0@$A$ABBHIKI8Lq111L&|44  	6&vvtzBB
d 4$;''
FTM**$..*k)BLQQ%!0Ksy" " "Ax 6>u4EJ
K01 d111bj=11&x<< 5'** 	 	GD!| /8,/0,,0[)q 9aaa<(((D8AqqqD>,AA(((
 	'0M)$NI AIO#!! G( "(7(+MHWHaaaK(J !H%	9:(  N
 $AAAvI.N (FJ'sy
9
9
9C&Cqqq ^Se	RRRF{a2:&CMJr   F)split_into_regionschannel_axisc          	         | j         dk     rt          d          |du}|r| j        dd         n| j        }||j        k    rt          d          t          j                            |           rt          d          t          j        |           } t          j
        | j                  }|                     |d          } |                    t          d          }|s| d	t          j        f         } t          j        | d
          }ddz  dz   f|j         z  }f|j         z  }	t!          ||	|j                  \  }
}}|j        d         |z
  }| |          }|                    d          |                    d          f}|rKt)          j        |j         d          }t)          j        ||          }t/          |          }||z  }t)          j        |          }t3          |d          D ]\  }}t5          fdt7          ||j                  D                       }||         |k    }|t9          d          fz  }||                                         }t          j        fd|d         j        D                       }t          j        ||d	t          j        f         z  d          }t?          | |         |||
||           |||<   njt          j        fd|d         j        D                       }t          j        ||d	t          j        f         z  d          }t?          | |||
||           t          j         ||d         |d         |           |s|d         }|S )a   Inpaint masked points in image with biharmonic equations.

    Parameters
    ----------
    image : (M[, N[, ..., P]][, C]) ndarray
        Input image.
    mask : (M[, N[, ..., P]]) ndarray
        Array of pixels to be inpainted. Have to be the same shape as one
        of the 'image' channels. Unknown pixels have to be represented with 1,
        known pixels - with 0.
    split_into_regions : boolean, optional
        If True, inpainting is performed on a region-by-region basis. This is
        likely to be slower, but will have reduced memory requirements.
    channel_axis : int or None, optional
        If None, the image is assumed to be a grayscale (single channel) image.
        Otherwise, this parameter indicates which axis of the array corresponds
        to channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.

    Returns
    -------
    out : (M[, N[, ..., P]][, C]) ndarray
        Input image with masked pixels inpainted.

    References
    ----------
    .. [1]  S.B.Damelin and N.S.Hoang. "On Surface Completion and Image
            Inpainting by Biharmonic Functions: Numerical Aspects",
            International Journal of Mathematics and Mathematical Sciences,
            Vol. 2018, Article ID 3950312
            :DOI:`10.1155/2018/3950312`
    .. [2]  C. K. Chui and H. N. Mhaskar, MRA Contextual-Recovery Extension of
            Smooth Functions on Manifolds, Appl. and Comp. Harmonic Anal.,
            28 (2010), 104-113,
            :DOI:`10.1016/j.acha.2009.04.004`

    Examples
    --------
    >>> img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    >>> mask = np.zeros_like(img)
    >>> mask[2, 2:] = 1
    >>> mask[1, 3:] = 1
    >>> mask[0, 4:] = 1
    >>> out = inpaint_biharmonic(img, mask)
    r	   z!Input array has to be at least 1DNr&   z&Input arrays have to be the same shapezMasked arrays are not supportedF)copy.C)orderr   r   r   r   )rY   c              3      K   | ]C\  }}t          t          |j        z
  d           t          |j        z   |                    V  DdS )r   N)r6   maxstartminstop)r*   slrH   r   s      r   	<genexpr>z%inpaint_biharmonic.<locals>.<genexpr>*  sg        B c"(V+Q//RWv5Et1L1LMM     r   c                     g | ]}|z  S r(   r(   r*   schannel_stride_bytess     r   r-   z&inpaint_biharmonic.<locals>.<listcomp>7  s    IIIq**IIIr   ).r   c                     g | ]}|z  S r(   r(   r   s     r   r-   z&inpaint_biharmonic.<locals>.<listcomp>G  s    TTT1Q"66TTTr   )a_mina_maxrL   )!r7   
ValueErrorr   r   maisMaskedArray	TypeErrorskimageimg_as_floatr   _supported_float_typer   astyper5   rF   r   r$   stridesr   r   r;   generate_binary_structurebinary_dilationr   find_objectsrD   r9   r:   r6   arrayr>   r|   clip)rJ   rK   r}   r~   multichannelimg_baseshapefloat_dtyperL   rl   rm   rM   r"   r#   offsetsknown_pointslimitskernelmask_dilatedmask_labeledbbox_slices
idx_regionbb_sliceroi_slmask_regionotmpostridesrN   r   r   s                              @@r   inpaint_biharmonicr      s   d zA~~<===t+L(4EEK$$%+M
""ABBB	u5!! ;9::: ''E -ek::KLL5L11E;;t%;((D 'c2:o&
'%s
#
#
#C Ff*q."TY.J)di'K+:Ksy, , ,(OXy
 ;r? G $<LA&&(8(8a(8(@(@AF -
.ty!<<*46BBB\**&|44$-k1$=$= 	 	 J     #Hl.@ A A    F
 'v.*<KuT{{n$Fv;##%%D xIIIIDL4HIII H !fWxRZ/H%HqQQQO-f   CKK9	> 8TTTTF@STTTUU&8CO+D!D1MMM)4oy/	
 	
 	

 GCvayq	s;;;; &kJr   )numpyr   scipyr   scipy.sparse.linalgr   scipy.ndimagendimager;   r   r   _sharedr   measurer   _inpaintr
   r   floatr$   r|   channel_as_last_axisr   r(   r   r   <module>r      s             ' ' ' ' ' '       ! ! ! ! ! !              ) ) ) ) ) )      */ + + + +a a aH :?d N N N N N N Nr   