PyMatting
CONTENTS
PyMatting
API Reference
Alpha Estimation
Cutout Function
Foreground Estimation
Matting Laplacians
Preconditioners
Solvers
Utility Functions
Examples
Benchmarks
Biblography
PyPI
GitHub
Foreground Estimation
pymatting.estimate_foreground_cf
🔗
Signature
estimate_foreground_cf
(
image
,
alpha
,
regularization
=
1e-5
,
rtol
=
1e-5
,
neighbors
=
[
(
-
1
,
0
)
,
(
1
,
0
)
,
(
0
,
-
1
)
,
(
0
,
1
)
]
,
return_background
=
False
,
foreground_guess
=
None
,
background_guess
=
None
,
ichol_kwargs
=
{
}
,
cg_kwargs
=
{
}
)
Function Description
Estimates the foreground of an image given alpha matte and image.
This method is based on the publication
[LLW07]
.
Parameters
image
(
numpy.ndarray
)
Input image with shape
\(h \times w \times d\)
alpha
(
numpy.ndarray
)
Input alpha matte with shape
\(h \times w\)
regularization
(
float
)
Regularization strength
\(\epsilon\)
, defaults to
\(10^{-5}\)
neighbors
(
list of tuples of ints
)
List of relative positions that define the neighborhood of a pixel
return_background
(
bool
)
Whether to return the estimated background in addition to the foreground
foreground_guess
(
numpy.ndarray
)
An initial guess for the foreground image in order to accelerate convergence.
Using input image by default.
background_guess
(
numpy.ndarray
)
An initial guess for the background image.
Using input image by default.
ichol_kwargs
(
dictionary
)
Keyword arguments for the incomplete Cholesky preconditioner
cg_kwargs
(
dictionary
)
Keyword arguments for the conjugate gradient descent solver
Returns
F
(
numpy.ndarray
)
Extracted foreground
B
(
numpy.ndarray
)
Extracted background (not returned by default)
Example
>>>
from
pymatting
import
*
>>>
image
=
load_image
(
"data/lemur/lemur.png"
,
"RGB"
)
>>>
alpha
=
load_image
(
"data/lemur/lemur_alpha.png"
,
"GRAY"
)
>>>
F
=
estimate_foreground_cf
(
image
,
alpha
,
return_background
=
False
)
>>>
F
,
B
=
estimate_foreground_cf
(
image
,
alpha
,
return_background
=
True
)
See Also
stack_images: This function can be used to place the foreground on a new background.
pymatting.estimate_foreground_ml
🔗
Signature
estimate_foreground_ml
(
image
,
alpha
,
regularization
=
1e-5
,
n_small_iterations
=
10
,
n_big_iterations
=
2
,
small_size
=
32
,
return_background
=
False
,
gradient_weight
=
1.0
)
Function Description
Estimates the foreground of an image given its alpha matte.
See
[GUCH20]
for reference.
Parameters
image
(
numpy.ndarray
)
Input image with shape
\(h \times w \times d\)
alpha
(
numpy.ndarray
)
Input alpha matte shape
\(h \times w\)
regularization
(
float
)
Regularization strength
\(\epsilon\)
, defaults to
\(10^{-5}\)
.
Higher regularization results in smoother colors.
n_small_iterations
(
int
)
Number of iterations performed on small scale, defaults to
\(10\)
n_big_iterations
(
int
)
Number of iterations performed on large scale, defaults to
\(2\)
small_size
(
int
)
Threshold that determines at which size
n_small_iterations
should be used
return_background
(
bool
)
Whether to return the estimated background in addition to the foreground
gradient_weight
(
float
)
Larger values enforce smoother foregrounds, defaults to
\(1\)
Returns
F
(
numpy.ndarray
)
Extracted foreground
B
(
numpy.ndarray
)
Extracted background
Example
>>>
from
pymatting
import
*
>>>
image
=
load_image
(
"data/lemur/lemur.png"
,
"RGB"
)
>>>
alpha
=
load_image
(
"data/lemur/lemur_alpha.png"
,
"GRAY"
)
>>>
F
=
estimate_foreground_ml
(
image
,
alpha
,
return_background
=
False
)
>>>
F
,
B
=
estimate_foreground_ml
(
image
,
alpha
,
return_background
=
True
)
See Also
stack_images: This function can be used to place the foreground on a new background.
pymatting.estimate_foreground_ml_cupy
🔗
Signature
estimate_foreground_ml_cupy
(
input_image
,
input_alpha
,
regularization
=
1e-5
,
n_small_iterations
=
10
,
n_big_iterations
=
2
,
small_size
=
32
,
block_size
=
(
32
,
32
)
,
return_background
=
False
,
to_numpy
=
True
)
Function Description
See the
estimate_foreground
method for documentation.
pymatting.estimate_foreground_ml_pyopencl
🔗
Signature
estimate_foreground_ml_pyopencl
(
input_image
,
input_alpha
,
regularization
=
1e-5
,
n_small_iterations
=
10
,
n_big_iterations
=
2
,
small_size
=
32
,
return_background
=
False
)
Function Description
See the
estimate_foreground
method for documentation.