Ë
     \;jÂ2  ã                   ó´   — 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        údG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\skimage/restoration/inpaint.pyÚ_get_neighborhoodr      s:   € Ü—
‘
˜6 F™?¨AÓ.€IÜ—
‘
˜6 F™?¨QÑ.°Ó9€IØ�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Ø€JˆvÑÜœ Ó,Ó-€Jô �x‰x˜
Ó#€HØ˜8Ñ$€Iä�x‰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        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ä—‘˜Ÿ
™
¬$Ô/€IØ78€IŒu�V˜f˜WÓ%Ð'¨$¯)©)Ñ3Ñ4Ø Ñ$€MØ�*˜tÑ#€Kä—8‘8˜MÓ*€LÜ—‘ Ó.€JÜ�~‰~˜kÓ*€HÜ�^‰^˜Z¨Ð2Ó3€Fä—‘˜+Ó&€JÜ¼¸\È:Ô9VÔWÑ9V±°°A”b—n‘n a¨ VÕ,Ð9VÒWÓX€Hð   1Ñ$€IÜ
�,‰,�t˜Y¬r¯x©x¸jÔ
I€CØ�T‘—‘“€Jô ×Ñ˜dÓ#€FÜ×Ñ 	Ó*€HØ¤"×"2Ñ"2°3¸(±?Ó"CÑCÐô —H‘HÐ/´r·w±wÔ?€MÜ—‘Ð-¨zÐ:À#Ç)Á)ÔL€JÜ—h‘h˜z´·±Ô9€OÜ—h‘h˜z´·±Ô9€OÜ—8‘8˜J¨c¯i©iÔ8€Lð €Jð —‘˜RÓ €IÜ×#Ñ# C§K¡K°°ZÐ0@Ó$AÓB€HØ€IØ€KØ€IÜ—8‘8˜L¨qÔ1€LÜ& |×4Ñˆ	�6ä& v¨v°t·z±zÓB‰
ˆˆdô ˜4 $™;Ó'ˆ
Ü˜F T™MÓ*ˆØ$Ÿ.™.¨*°kÐ)BÀLÓQ‰ˆ�%ØÐÜ!0Ø˜K¨s¯y©yô"ÑˆAˆx˜ð 6>¸uÐ4EˆJ˜
 KÐ0Ñ1ð ˜d¢1¤b§j¡j =Ñ1Ñ1ˆÜ×&Ñ& x°·±Ó<ˆð ˆÜ˜5 'Ö*‰GˆD�!Ø˜Š|Ø/8� Ñ,Ø/0� Ñ,Ø,0�˜[Ñ)Ø˜qÑ ‘à˜9¢a˜<Ó(¨D°8¸Aºq¸D±>Ñ,AÑAÓ(Ø˜‘
‘ð +ó Ø'0ˆM˜)Ñ$Ø˜‰NŠIð? 5ðD ˜A‘€IØ€OØ#ÐÜ!àØØàØØØØàØØØØó!€Gð( " ( 7Ð+€MØ˜H˜W˜H¢a˜KÑ(€Jð ˜Ÿ™Ð!€HÜ×%Ñ%Ø	˜¨Ð9Ð:À(ô€Nð
 $¢A v IÑ.€Nô �(‰(�F˜JÐ'¨s¯y©yÔ
9€CØ&€Cˆ’qÐÑô �^ S°eÈ	ÔR€FØ‡{�{�aÒØšœ2Ÿ:™:˜Ñ&ˆà€Cˆ�MØ€Jùóc 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 : bool, 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   øè ø€ ð á A‘H�B˜ô ”c˜"Ÿ(™( VÑ+¨QÓ/´°R·W±W¸vÑ5EÀtÓ1L×MÙ Aù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ª   Å   s]  ø€ ðd ‡z�z�A‚~ÜÐ<Ó=Ð=à tÐ+€LÙ(4�E—K‘K  Ñ$¸%¿+¹+€MØ˜Ÿ
™
Ò"ÜÐAÓBÐBä	‡u�u×Ñ˜5Ô!ÜÐ9Ó:Ð:ä× Ñ  Ó'€Eô ×-Ñ-¨e¯k©kÓ:€KØ�L‰L˜¨5ˆLÓ1€Eà�;‰;”t %ˆ;Ó(€DÙØ�cœ2Ÿ:™:�oÑ&ˆÜ
�'‰'�%˜sÔ
#€Cð €FØ�f‘*˜q‘.Ð" T§Y¡YÑ.€JØ�)˜dŸi™iÑ'€KÜ+:Ø�K s§y¡yô,Ñ(€O�X˜yð
 Ÿ;™; r™?Ðð ˜Ñ€Gð ˜$˜‘<€LØ×Ñ AÐÓ&¨×(8Ñ(8¸aÐ(8Ó(@ÐA€Fâä×.Ñ.¨t¯y©y¸!Ó<ˆÜ×*Ñ*¨4¸6ÔBˆÜ˜\Ó*ˆØ˜Ñˆä×&Ñ& |Ó4ˆä$-¨k¸1Ö$=Ñ ˆJ˜äó ä # H¨l×.@Ñ.@Ô Aóó ˆFð
 ' vÑ.°*Ñ<ˆKà”u˜T“{�nÑ$ˆFà�v‘;×#Ñ#Ó%ˆDô —x‘xØ48¸±L×4HÒ4HÓIÑ4H¨q�Ð*Ó*Ð4HÑIóˆHô !Ÿf™f W¨x¸¼R¿Z¹Z¸Ñ/HÑ%HÈqÔQˆOä-Ø�f‘ØØØØØôð ˆC�ŠKñ9 %>ô> —8‘8ÀÀFÁ×@SÒ@SÓTÑ@S¸1˜QÐ"6Ó6Ð@SÑTÓUˆÜŸ&™& ¨8°C¼¿¹°OÑ+DÑ!DÈ1ÔMˆä)Ø�4˜˜o¨y¸/ô	
ô
 ‡G�GˆC�v˜a‘y¨¨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   