
    #
3jb                         S SK Jr  S SKrS SKrS SKrS SKrS SKJ	r	  S r
S rS rS rSS jrS	 rSS
 jrSS jrS S jrS rS!S jrS rS"S jrS rS rS rS rS#S jrS$S jrS rS rS%S jrS r S r!S r"g)&    )ImageNwrapsc                 0   ^  [        T 5      U 4S j5       nU$ )aD  Creates a new function which operates on each channel

Parameters
----------
single_channel_func: function
    Function that acts on a single color channel

Returns
-------
channel_func: function
    The same function that operates on all color channels

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> from scipy.signal import convolve2d
>>> single_channel_fun = lambda x: convolve2d(x, np.ones((3, 3)), 'valid')
>>> multi_channel_fun = apply_to_channels(single_channel_fun)
>>> I = np.random.rand(480, 320, 3)
>>> multi_channel_fun(I).shape
(478, 318, 3)
c                   > [        U R                  5      S:X  a  T" U /UQ70 UD6$ U R                  nU R                  US   US   S5      n [        R                  " [        U R                  S   5       Vs/ s H'  nT" U S S 2S S 2U4   R                  5       /UQ70 UD6PM)     snSS9nUR                  [        UR                  S S 5      [        USS  5      -   5      $ s  snf )N   r      axis)lenshapereshapenpstackrangecopylist)imageargskwargsr   cresultsingle_channel_funcs         M/home/wildlama/miniconda3/lib/python3.13/site-packages/pymatting/util/util.pymulti_channel_func-apply_to_channels.<locals>.multi_channel_func"   s    u{{q &u>t>v>>KKEMM%(E!Hb9EXX #5;;q>22 (aAg(;(;(=OOO2 F >>$v||BQ'7"84ab	?"JKKs   6.C r   )r   r   s   ` r   apply_to_channelsr   	   s'    2 L  L"     c                 0    [         R                  " SX5      $ )a  Computes the dot product of two vectors.

Parameters
----------
a: numpy.ndarray
    First vector (if np.ndim(a) > 1 the function calculates the product for the two last axes)
b: numpy.ndarray
    Second vector (if np.ndim(b) > 1 the function calculates the product for the two last axes)

Returns
-------
product: scalar
    Dot product of `a` and `b`

Example
-------
>>> import numpy as np
>>> from pymatting import *
>>> a = np.ones(2)
>>> b = np.ones(2)
>>> vec_vec_dot(a,b)
2.0
z...i,...i->...r   einsumabs     r   vec_vec_dotr&   7   s    0 99%q,,r   c                 0    [         R                  " SX5      $ )a  Calculates the matrix vector product for two arrays.

Parameters
----------
A: numpy.ndarray
    Matrix (if np.ndim(A) > 2 the function calculates the product for the two last axes)
b: numpy.ndarray
    Vector (if np.ndim(b) > 1 the function calculates the product for the two last axes)

Returns
-------
product: numpy.ndarray
    Matrix vector product of both arrays

Example
-------
>>> import numpy as np
>>> from pymatting import *
>>> A = np.eye(2)
>>> b = np.ones(2)
>>> mat_vec_dot(A,b)
array([1., 1.])
z...ij,...j->...ir!   )Ar%   s     r   mat_vec_dotr)   R   s    0 99'..r   c                 0    [         R                  " SX5      $ )a  Computes the outer product of two vectors

a: numpy.ndarray
    First vector (if np.ndim(b) > 1 the function calculates the product for the two last axes)
b: numpy.ndarray
    Second vector (if np.ndim(b) > 1 the function calculates the product for the two last axes)

Returns
-------
product: numpy.ndarray
    Outer product of `a` and `b` as numpy.ndarray

Example
-------
>>> import numpy as np
>>> from pymatting import *
>>> a = np.arange(1,3)
>>> b = np.arange(1,3)
>>> vec_vec_outer(a,b)
array([[1, 2],
       [2, 4]])
z	...i,...jr!   r#   s     r   vec_vec_outerr+   m   s    . 99[!''r   c                     US:  d  US:  a  [        S5      eUS:  d  US:  a  [        S5      eX:  a  [        S5      eX:  nX:  nS[        R                  " U 5      -  nSXT'   SXS'   U$ )a  Fixes broken trimap :math:`T` by thresholding the values

.. math::
    T^{\text{fixed}}_{ij}=
    \begin{cases}
        0,&\text{if } T_{ij}<\text{lower\_threshold}\\
        1,&\text{if }T_{ij}>\text{upper\_threshold}\\
        0.5, &\text{otherwise}.\\
    \end{cases}


Parameters
----------
trimap: numpy.ndarray
    Possibly broken trimap
lower_threshold: float
    Threshold used to determine background pixels, defaults to 0.1
upper_threshold: float
    Threshold used to determine foreground pixels, defaults to 0.9

Returns
-------
fixed_trimap: numpy.ndarray
    Trimap having values in :math:`\{0, 0.5, 1\}`

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> trimap = np.array([0,0.1, 0.4, 0.9, 1])
>>> fix_trimap(trimap, 0.2, 0.8)
array([0. , 0. , 0.5, 1. , 1. ])
r   r	   zInvalid lower thresholdzInvalid upper thresholdz4Lower threshold must be smaller than upper thresholdg      ?)
ValueErrorr   	ones_like)trimaplower_thresholdupper_thresholdis_bgis_fgfixeds         r   
fix_trimapr5      s    D o1233o1233(OPP$E$E",,v&&EELELLr   c                 <     [        U 5        g! [         a     gf = f)a  Checks if an object is iterable

Parameters
----------
obj: object
    Object to check

Returns
-------
is_iterable: bool
    Boolean variable indicating whether the object is iterable

Example
-------
>>> from pymatting import *
>>> l = []
>>> isiterable(l)
True
TF)iter	TypeError)objs    r   
isiterabler:      s#    (S	 s    
c                    [         R                  [         R                  [         R                  [         R                  [         R
                  [         R                  [         R                  S.n[        U5      (       d0  [        U R                  U-  5      [        U R                  U-  5      4nU R                  XUR                  5          5      n U $ )N)bicubicbilinearboxhamminglanczosnearestnone)r   BICUBICBILINEARBOXHAMMINGLANCZOSNEARESTr:   intwidthheightresizelower)r   sizeresamplefilterss       r   _resize_pil_imagerQ      s    ==NNyy======G dEKK$&'U\\D-@)ABLLx~~'789ELr   c                     [         R                  " U 5      nUb+  UR                  5       nUS:X  a  SOUnUR                  U5      nUb  [	        XBU5      n[
        R                  " U5      S-  nU$ )zThis function can be used to load an image from a file.

Parameters
----------
path: str
    Path of image to load.
mode: str
    Can be "GRAY", "RGB" or something else (see PIL.convert())

Returns
-------
image: numpy.ndarray
    Loaded image
GRAYLg     o@)r   openupperconvertrQ   r   array)pathmoderN   rO   r   s        r   
load_imager[      sg      JJtEzz|fns$d#!%x8HHUOe#ELr   c                 0   UR                   [        R                  [        R                  [        R                  4;   d   eUR                   [        R                  [        R                  4;   a8  [        R
                  " US-  SS5      R                  [        R                  5      nU(       aE  [        R                  R                  U 5      u  p4[        U5      S:  a  [        R                  " USS9  [        R                  " U5      R                  U 5        g)ax  Given a path, save an image there.

Parameters
----------
path: str
    Where to save the image.
image: numpy.ndarray, dtype in [np.uint8, np.float32, np.float64]
    Image to save.
    Images of float dtypes should be in range [0, 1].
    Images of uint8 dtype should be in range [0, 255]
make_directory: bool
    Whether to create the directories needed for the image path.
   r   T)exist_okN)dtyper   uint8float32float64clipastypeosrY   splitr   makedirsr   	fromarraysave)rY   r   make_directory	directory_s        r   
save_imagerm     s     ;;288RZZ<<<<{{rzz2::..Q,33BHH=ww}}T*	y>AKK	D1	OOE%r   c                    [        U R                  5      S;   d   eU R                  [        R                  [        R
                  [        R                  4;   d   eU R                  [        R
                  [        R                  4;   a8  [        R                  " U S-  SS5      R                  [        R                  5      n [        U R                  5      S:X  a  [        R                  " U /S-  SS9$ U R                  S   S:X  a  [        R                  " U /S-  SS9$ U R                  S   S:X  a  U $ U R                  S   S:X  a  U S	S	2S	S	2S	S24   $ [        S
U R                  5      e)a  Convertes an image to rgb8 color space

Parameters
----------
image: numpy.ndarray
    Image to convert

Returns
-------
image: numpy.ndarray
    Converted image with same height and width as input image but with three color channels
Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> I = np.eye(2)
>>> to_rgb8(I)
array([[[255, 255, 255],
        [  0,   0,   0]],
       [[  0,   0,   0],
        [255, 255, 255]]], dtype=uint8)
)r      r]   r   r   ro   r   r	      NzInvalid image shape:)r   r   r_   r   r`   ra   rb   rc   rd   r   concatenater-   )r   s    r   to_rgb8rr   "  s   . u{{v%%%;;288RZZ<<<<{{rzz2::..Q,33BHH=
5;;1xx!!,,{{1~~~ugk22	Q1		Q1	Q2A2X
+U[[
99r   c           	         U  H8  nUc  M  UR                   [        R                  [        R                  4;   a  M8   e   [	        U 5      nUS:X  a  gUcA  Uc>  [        [        R                  " [        R                  " U5      5      5      nXQ-   S-
  U-  nOUc  XQ-   S-
  U-  nOUc
  XR-   S-
  U-  nU  Vs/ s H  oDc  M  UR                  PM     nn[        S U 5       5      n[        S U 5       5      n[        U V	s/ s H  n	[	        U	5      S:  d  M  U	S   PM     sn	SS9n
U
S:  a  [        U 5       H  u  pUc  M
  [	        UR                  5      S:X  a  USS2SS2[        R                  4   nUR                  S   S:X  a  [        R                  " U/U
-  SS9nUR                  S   S	:X  a<  U
S
:X  a6  [        U[        R                  " UR                  SS UR                   S95      nX@U'   M     Uc  [        S U  5       5      n[        R                   " Xr-  X-  U
4US9n[#        U5       H  n[#        U5       H  nXU-  -   nU[	        U 5      :  a    M*  X   nUc  M%  UR%                  UR                  S   UR                  S   S5      nUUX-  X-  UR                  S   -   2X-  X-  UR                  S   -   24'   M     M     UR                  S   S:X  a  USS2SS2S4   nU$ s  snf s  sn	f )a  Plots a grid of images.

Parameters
----------
images : list of numpy.ndarray
    List of images to plot
nx: int
    Number of rows
ny: int
    Number of columns
dtype: type
    Data type of output array

Returns
-------
grid: numpy.ndarray
   Grid of images with datatype `dtype`
Nr   r	   c              3   *   #    U  H	  oS    v   M     g7f)r   N .0r   s     r   	<genexpr>make_grid.<locals>.<genexpr>v       )&!H&   c              3   *   #    U  H	  oS    v   M     g7f)r	   Nru   rv   s     r   rx   ry   w  rz   r{   r   )defaultr   ro   rp   r_   c              3   B   #    U  H  oc  M  UR                   v   M     g 7f)Nr~   )rw   r   s     r   rx   ry     s     JfU[U[[fs   r
   )r_   r   ra   rb   r   rI   ceilsqrtr   max	enumeratenewaxisrq   stack_imagesonesnextzerosr   r   )imagesnxnyr_   r   nshapeshwr   dir   yxs                  r   	make_gridr   N  s   & ;;2::rzz"::::  	FAAv	zbj$%fqjR	fqjR	fqjR'-CvekekkvFC)&))A)&))A6<6%SZ!^XU1X6<aHA1u!&)HA u{{#q(!!Q

"23E;;q>Q&NNE7Q;Q?E;;q>Q&16(rwwu{{2AekkJE "q	 * }JfJJXXqvqvq)7F2YrAF
ACKIE ekk!nekk!nbI  EAEEKKN22AEAEEKKPQN<R4RR    ||A!1aM] D =s   4K,>K,=K1	K1c                     [        U 5      n[        R                  " US-  SS5      R                  [        R                  5      n[
        R                  " U5      nUR                  5         g)zPlot grid of images.

Parameters
----------
images : list of numpy.ndarray
    List of images to plot
height : int, matrix
    Height in pixels the output grid, defaults to 512

r]   r   N)r   r   rc   rd   r`   r   rh   show)r   grids     r   show_imagesr     sJ     VD774#:q#&--bhh7D??4 DIIKr   c                 6   U(       a  U R                  5       n U R                  5       nU R                  5       nUS:  a  [        R                  " SU-  SS9  US:  a  [        R                  " SU-  SS9  U R
                  [        R                  [        R                  4;  a"  [        R                  " SU R
                  -  SS9  X:  nX:*  nUR                  5       S:X  a  [        S	U-  5      eUR                  5       S:X  a  [        S
U-  5      eXg-  nU) n	XgX4$ )a~  This function splits the trimap into foreground pixels, background pixels, and unknown pixels.

Foreground pixels are pixels where the trimap has values larger than or equal to `fg_threshold` (default: 0.9).
Background pixels are pixels where the trimap has values smaller than or equal to `bg_threshold` (default: 0.1).
Pixels with other values are assumed to be unknown.

Parameters
----------
trimap: numpy.ndarray
    Trimap with shape :math:`h \times w`
flatten: bool
    If true np.flatten is called on the trimap

Returns
-------
is_fg: numpy.ndarray
    Boolean array indicating which pixel belongs to the foreground
is_bg: numpy.ndarray
    Boolean array indicating which pixel belongs to the background
is_known: numpy.ndarray
    Boolean array indicating which pixel is known
is_unknown: numpy.ndarray
    Boolean array indicating which pixel is unknown
bg_threshold: float
    Pixels with smaller trimap values will be considered background.
fg_threshold: float
    Pixels with larger trimap values will be considered foreground.


Example
-------
>>> import numpy as np
>>> from pymatting import *
>>> trimap = np.array([[1,0],[0.5,0.2]])
>>> is_fg, is_bg, is_known, is_unknown = trimap_split(trimap)
>>> is_fg
array([ True, False, False, False])
>>> is_bg
array([False,  True, False, False])
>>> is_known
array([ True,  True, False, False])
>>> is_unknown
array([False, False,  True,  True])
        z:Trimap values should be in [0, 1], but trimap.min() is %s.ro   
stacklevel      ?z:Trimap values should be in [0, 1], but trimap.max() is %s.zfUnexpected trimap.dtype %s. Are you sure that you do not want to use np.float32 or np.float64 instead?r   z7Trimap did not contain background values (values <= %f)z7Trimap did not contain foreground values (values >= %f))flattenminr   warningswarnr_   r   ra   rb   sumr-   )
r/   r   bg_thresholdfg_threshold	min_value	max_valuer3   r2   is_known
is_unknowns
             r   trimap_splitr     s   Z !

I

I3H9T	

 3H9T	

 ||BJJ

33tll	
 "E"Eyy{aET
 	
 yy{aET
 	
 }HJ--r   c                    [        U R                  5      S:w  d  U R                  S   S:w  a+  [        R                  " S[	        U R                  5      -  SS9  U R                  5       nU R                  5       nUS:  a  [        R                  " SU-  SS9  US:  a  [        R                  " SU-  SS9  U R                  [        R                  [        R                  4;  a#  [        R                  " S	U R                  -  SS9  g
g
)aG  Performs a sanity check for input images. Image values should be in the
range [0, 1], the `dtype` should be `np.float32` or `np.float64` and the
image shape should be `(?, ?, 3)`.

Parameters
----------
image: numpy.ndarray
    Image with shape :math:`h \times w \times 3`

Example
-------
>>> import numpy as np
>>> from pymatting import check_image
>>> image = (np.random.randn(64, 64, 2) * 255).astype(np.int32)
>>> sanity_check_image(image)
__main__:1: UserWarning: Expected RGB image of shape (?, ?, 3), but image.shape is (64, 64, 2).
__main__:1: UserWarning: Image values should be in [0, 1], but image.min() is -933.
__main__:1: UserWarning: Image values should be in [0, 1], but image.max() is 999.
__main__:1: UserWarning: Unexpected image.dtype int32. Are you sure that you do not want to use np.float32 or np.float64 instead?

ro   r   z=Expected RGB image of shape (?, ?, 3), but image.shape is %s.r   r   z8Image values should be in [0, 1], but image.min() is %s.r   z8Image values should be in [0, 1], but image.max() is %s.zeUnexpected image.dtype %s. Are you sure that you do not want to use np.float32 or np.float64 instead?N)r   r   r   r   strr   r   r_   r   ra   rb   )r   r   r   s      r   sanity_check_imager     s    . 5;;1A! 3K%++	
 		I		I3FR	

 3FR	

 {{2::rzz22skk	
 3r   c                     [        UR                  5      S:X  a  USS2SS2[        R                  4   nX -  SU-
  U-  -   $ )a  This function composes a new image for given foreground image, background image and alpha matte.

This is done by applying the composition equation

.. math::
    I = \alpha F + (1-\alpha)B.

Parameters
----------
foreground: numpy.ndarray
    Foreground image
background: numpy.ndarray
    Background image
alpha: numpy.ndarray
    Alpha matte

Returns
-------
image: numpy.ndarray
    Composed image as numpy.ndarray

Example
-------
>>> from pymatting import *
>>> foreground = load_image("data/lemur/lemur_foreground.png", "RGB")
>>> background = load_image("data/lemur/beach.png", "RGB")
>>> alpha = load_image("data/lemur/lemur_alpha.png", "GRAY")
>>> I = blend(foreground, background, alpha)
r   Nr	   )r   r   r   r   )
foreground
backgroundalphas      r   blendr   E  sA    < 5;;1aBJJ&'Uj 888r   c                      U  Vs/ s H8  n[        UR                  5      S:X  a  UOUSS2SS2[        R                  4   PM:     n n[        R                  " U SS9$ s  snf )a  This function stacks images along the third axis.
This is useful for combining e.g. rgb color channels or color and alpha channels.

Parameters
----------
*images: numpy.ndarray
    Images to be stacked.

Returns
-------
image: numpy.ndarray
    Stacked images as numpy.ndarray

Example
-------
>>> from pymatting.util.util import stack_images
>>> import numpy as np
>>> I = stack_images(np.random.rand(4,5,3), np.random.rand(4,5,3))
>>> I.shape
(4, 5, 6)
ro   Nr   r   )r   r   r   r   rq   )r   r   s     r   r   r   i  sa    0 E ekk"a'U1a3C-D	D   >>&q))	s   ?Ac                     U R                  [        R                  " U R                  S   U R                  5      5      nU$ )a  Calculate the sum of each row of a matrix

Parameters
----------
A: np.ndarray or scipy.sparse.spmatrix
    Matrix to sum rows of

Returns
-------
row_sums: np.ndarray
    Vector of summed rows

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> A = np.random.rand(2,2)
>>> A
array([[0.62750946, 0.12917617],
       [0.8599449 , 0.5777254 ]])
>>> row_sum(A)
array([0.75668563, 1.4376703 ])
r	   )dotr   r   r   r_   )r(   row_sumss     r   row_sumr     s.    0 uuRWWQWWQZ12HOr   c                     [        U 5      nSX"U:  '   SU-  n[        R                  R                  U5      nUR	                  U 5      n U $ )a  Normalize the rows of a matrix

Rows with sum below threshold are left as-is.

Parameters
----------
A: scipy.sparse.spmatrix
    Matrix to normalize
threshold: float
    Threshold to avoid division by zero

Returns
-------
A: scipy.sparse.spmatrix
    Matrix with normalized rows

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> A = np.arange(4).reshape(2,2)
>>> normalize_rows(A)
array([[0. , 1. ],
       [0.4, 0.6]])
r   )r   scipysparsediagsr   )r(   	thresholdr   row_normalization_factorsDs        r   normalize_rowsr     sM    4 qzH &)H	!" #h45A	aAHr   c                 P   U(       aY  [         R                  " [         R                  " U 5      U5      n[         R                  " [         R                  " U5      U 5      nX44$ [         R                  " U 5      n[         R                  " U5      n[         R                  " X45      u  p4X44$ )a  Calculates image pixel coordinates for an image with a specified shape

Parameters
----------
width: int
    Width of the input image
height: int
    Height of the input image
flatten: bool
    Whether the array containing the coordinates should be flattened or not, defaults to False

Returns
-------
x: numpy.ndarray
    x coordinates
y: numpy.ndarray
    y coordinates

Example
-------
>>> from pymatting import *
>>> x, y = grid_coordinates(2,2)
>>> x
array([[0, 1],
       [0, 1]])
>>> y
array([[0, 0],
       [1, 1]])
)r   tilearangerepeatmeshgrid)rJ   rK   r   r   r   s        r   grid_coordinatesr     sw    < GGBIIe$f-IIbii'/ 4K IIeIIf{{1 4Kr   c                    [         R                  " U5      R                  5       n[        U5      nX-  n[         R                  " Xv-  [         R
                  S9n[         R                  " Xv-  [         R
                  S9n	[         R                  " Xv-  [         R                  S9n
Sn[        XSS9u  p[        X4U5       Ha  u  pn[         R                  " X-   SU S-
  5      n[         R                  " X-   SUS-
  5      nXU -  -   XX-   & UUU -  -   XX-   & UXX-   & X-  nMc     [        R                  R                  XU	44Xw4S9nU$ )aj  Calculates a convolution matrix that can be applied to a vectorized image

Additionally, this function allows to specify which pixels should be used for the convoltion, i.e.

.. math:: \left(I * K\right)_{ij} = \sum_k K_k I_{i+{\Delta_y}_k,j+{\Delta_y}_k},

where :math:`K` is the flattened convolution kernel.

Parameters
----------
width: int
    Width of the input image
height: int
    Height of the input image
kernel: numpy.ndarray
    Convolutional kernel
dx: numpy.ndarray
    Offset in x direction
dy: nunpy.ndarray
    Offset in y direction

Returns
-------
M: scipy.sparse.csr_matrix
    Convolution matrix
r~   r   Tr   r	   )r   )r   asarrayr   r   r   int32rb   r   ziprc   r   r   
csr_matrix)rJ   rK   kerneldxdyweightscountr   i_indsj_indsvalueskr   r   dx2dy2weightx2y2r(   s                       r   sparse_conv_matrix_with_offsetsr     s)   6 jj ((*GLEAXXairxx0FXXairxx0FXXairzz2F	AE48DA0&WWQWa+WWQWa!,E	M15eO15"15	 1 	&)9 :1&IAHr   c                 l    UR                   u  p4[        XCSS9u  pVXTS-  -  nXcS-  -  n[        XX%U5      $ )a  Calculates a convolution matrix that can be applied to a vectorized image

Parameters
----------
width: int
    Width of the input image
height: int
    Height of the input image
kernel: numpy.ndarray
    Convolutional kernel

Returns
-------
M: scipy.sparse.csr_matrix
    Convolution matrix

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> sparse_conv_matrix(3,3,np.ones((3,3)))
<9x9 sparse matrix of type '<class 'numpy.float64'>'
with 49 stored elements in Compressed Sparse Row format>
Tr   r   )r   r   r   )rJ   rK   r   khkwr   r   s          r   sparse_conv_matrixr   '  sB    2 \\FBBD1DAqLAqLA*5&QGGr   c                     U(       a  [        U 5      n U[        U 5      -   n[        R                  R	                  U5      nX@-
  nU$ )a~  Calculates the random walk normalized Laplacian matrix from the weight matrix

Parameters
----------
W: numpy.ndarray
    Array of weights
normalize: bool
    Whether the rows of W should be normalized to 1, defaults to True
regularization: float
    Regularization strength, defaults to 0, i.e. no regularizaion

Returns
-------
L: scipy.sparse.spmatrix
    Laplacian matrix

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> weights_to_laplacian(np.ones((4,4)))
matrix([[ 0.75, -0.25, -0.25, -0.25],
        [-0.25,  0.75, -0.25, -0.25],
        [-0.25, -0.25,  0.75, -0.25],
        [-0.25, -0.25, -0.25,  0.75]])
)r   r   r   r   r   )W	normalizeregularizationr   r   rT   s         r   weights_to_laplacianr   H  s?    6 1#A1A	AHr   c                     [         R                  " U 5      n U R                  5       nU R                  5       nX-
  X!-
  -  $ )aD  Normalizes an array such that all values are between 0 and 1

Parameters
----------
values: numpy.ndarray
    Array to normalize

Returns
-------
result: numpy.ndarray
    Normalized array

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> normalize(np.array([0, 1, 3, 10]))
array([0. , 0.1, 0.3, 1. ])
)r   r   r   r   )r   r$   r%   s      r   r   r   n  s7    ( ZZF

A

AJ15!!r   c                     X-   S-
  U-  $ )zDivides a number x by another integer n and rounds up the result

Parameters
----------
x: int
    Numerator
n: int
    Denominator

Returns
-------
result: int
    Result

Example
-------
>>> from pymatting import *
>>> div_round_up(3,2)
2
r	   ru   )r   r   s     r   div_round_upr     s    * EAI!r   c                    X-
  n[         R                  " X-
  U-  SS9[         R                  " [         R                  " U5      5      -  n[         R                  " USS5      nUSS2SS2S4   n[         R                  " U SU-
  U-  -
  U[         R
                  " U 5      US:g  S9n[         R                  " USS5      n[         R                  " Xu/5      nU$ )a]  Remove background from image with at most two colors.
Might not work if image has more than two colors.

Parameters
----------
image: numpy.ndarray
    RGB input image
fg_color: numpy.ndarray
    RGB Foreground color
bg_color: numpy.ndarray
    RGB Background color

Returns
-------
output: numpy.ndarray
    RGBA output image

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> image = np.random.rand(480, 320, 3)
>>> fg_color = np.random.rand(3)
>>> bg_color = np.random.rand(3)
>>> output = remove_background_bicolor(image, fg_color, bg_color)
>>> print(output.shape)
(480, 320, 4)
r
   r   r   r   Nr	   )outwhere)r   r   squarerc   divide
zeros_likedstack)	r   fg_colorbg_colorfg_bgur   r$   actual_coloroutputs	            r   remove_background_bicolorr     s    < E 	 E)3bffRYYu=M6NNAGGAsC E
 	aDjA99	!a%8#	#aR]]5-AcL 77<c2LYY,-FMr   )皙??)r<   )NNr>   )T)NNN)Tr   r   )r   )F)Tr   )#PILr   numpyr   scipy.sparser   re   r   	functoolsr   r   r&   r)   r+   r5   r:   rQ   r[   rm   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ru   r   r   <module>r      s       	  +\-6/6(40f6&>&6):XTn$V.r2
j!9H*::%P'T/dHB#L"401r   