
    hhq1                         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y)    N)sparse)spsolve)laplace   )utils)label   )_build_matrix_innerc                 t    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        Y/var/www/html/dev/engine/venv/lib/python3.12/site-packages/skimage/restoration/inpaint.py_get_neighborhoodr      s:    

6F?A.I

6F?Q.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$      sb    %u-JJv,-J xx
#H8$Ixx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        t        ||      D cg c]  \  }}t        j                  ||f       c}}      }|dk7  }t        j                  ||t        j                  d      }||   j                         }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 }|j)                  d      }t        j*                  |j)                  d|f            } d}!d}"d}#t        j,                  |d      }t/        |      D ]  \  }#}$t1        |$||j                         \  }%}&t        |&|%z
        }'t        |$|%z
        }(|j3                  |'|(fd	      \  })}*|)%t5        |'|(|j&                        \  }+})}*|)|*f||'|(f<   |)|%d
d
t        j6                  f   z   })t        j8                  |)|j                         },d}-t        |*|,      D ]?  \  }.}/||/   r|#||"<   |/||"<   |.||"<   |"dz  }" ||!d
d
fxx   |.| |/d
d
f   z  z  cc<   |-dz  }-A |-s	|#||!<   |!dz  }! |#dz   }0|!}1|"}2t;        |0|1|2||||| |||||      }3|d
|3 }|d
|3d
d
f   }||j<                  f}4t?        j@                  |||ff|4      }5|5d
d
|f   }5t        j$                  ||f|j&                        }6||6|d
d
f<   tC        |5|6dd      }7|7j
                  dk(  r|7d
d
t        j6                  f   }7|7||<   |S c c}}w )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   constant)outputmoder	   r   )NNN)r   FMMD_ATA)use_umfpack
permc_spec)"r   r   onesboolslicendimr   flatnonzeroconcatenatetuplezipndiconvolveuint8sumcount_nonzeroemptyintpr   r   reshapeascontiguousarrayr   	enumerater   getr$   newaxisravel_multi_indexr
   sizer   	csr_arrayr   )8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bc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results8                                                           r   !_inpaint_biharmonic_single_regionrx   !   s   D 2J""1%*F

$/I78IuVfW%'$))34$M*t#K88M*L.J~~k*H^^Z23F+&J\:9VWAbnnaV,WXH  1$I
,,tYrxxj
ICTJ d#F	*H""2"23(?"CC HH/rww?M-z:#))LJhhz9Ohhz9O88Jcii8L J R I##CKKZ0@$ABHIKI88Lq1L&|4 	6&vvtzzB
d 4$;'
FTM*$..*k)BLQ%!0Ksyy"Ax 6>u4EJ
K01 d1bjj=11&&x< 5'* 	GD!|/8,/0,,0[)q 9a<(D8AqD>,AA(
	 '0M)$NI?D AIO#!!G( "(7+MHWHaK(J !H%%	9:(N
 $AvI.N ((FJ'syy
9C&Cq ^Se	RF{{a2::&CMJc Xs   /!R
F)split_into_regionschannel_axisc          	         | j                   dk  rt        d      |du}|r| j                  dd n| j                  }||j                  k7  rt        d      t        j                  j                  |       rt        d      t        j                  |       } t        j                  | j                        }| j                  |d      } |j                  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
  }| |    }|j%                  d      |j'                  d      f}|r;t)        j*                  |j                   d      }t)        j,                  ||      }t/        |      }||z  }t)        j0                  |      }t3        |d      D ]  \  }}t5        fdt7        ||j                        D              }||   |k(  }|t9        d      fz  }||   j                         }t        j:                  |d   j"                  D cg c]  }||z  	 c}      }t        j<                  ||d	t        j                  f   z  d      }t?        | |   |||
||       |||<    npt        j:                  |d   j"                  D cg c]  }||z  	 c}      }t        j<                  ||d	t        j                  f   z  d      }t?        | |||
||       t        j@                  ||d   |d   |       |s|d   }|S c c}w c c}w )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   )rU   c              3      K   | ]A  \  }}t        t        |j                  z
  d       t        |j                  z   |             C yw)r   N)r/   maxstartminstop).0slrB   r   s      r   	<genexpr>z%inpaint_biharmonic.<locals>.<genexpr>*  sD      B c"((V+Q/RWWv5Et1LMs   AA
).r   )a_mina_maxrF   )!r0   
ValueErrorr   r   maisMaskedArray	TypeErrorskimageimg_as_floatr   _supported_float_typer   astyper.   r@   r|   r$   stridesr   r   r5   generate_binary_structurebinary_dilationr   find_objectsr>   r3   r4   r/   arrayr8   rx   clip)rD   rE   ry   rz   multichannelimg_baseshapefloat_dtyperF   rh   ri   rG   r"   r#   channel_stride_bytesoffsetsknown_pointslimitskernelmask_dilatedmask_labeledbbox_slices
idx_regionbb_sliceroi_slmask_regionotmpsostridesrH   r   s                                @r   inpaint_biharmonicr      sV   d zzA~<==t+L(4EKK$%++M

"ABB	uu5!9::  'E --ekk:KLL5L1E;;t%;(Dc2::o&
''%s
#C Ff*q."TYY.J)dii'K+:Ksyy,(OXy
 ;;r? G $<LA&(8(8a(8(@AF..tyy!<**46B\*&&|4$-k1$= 	 J  #Hl.@.@ A F
 'v.*<KuT{n$Fv;##%D xx48L4H4HIq**IH !ffWxRZZ/H%HqQO-f CK9	> 88F@S@ST1Q"66TU&&8CO+D!D1M)4oy/	

 GGCvayq	s;&kJ; J  Us   2M%
-M*)numpyr   scipyr   scipy.sparse.linalgr   scipy.ndimagendimager5   r   r   _sharedr   measurer   _inpaintr
   r   floatr$   rx   channel_as_last_axisr    r   r   <module>r      s\      '  !    )  */ +aH :?d N Nr   