Alpha Estimation

pymatting.estimate_alpha_cf

🔗

Signature

estimate_alpha_cf( image, trimap, preconditioner=None, laplacian_kwargs={}, cg_kwargs={})

Function Description

Estimate alpha from an input image and an input trimap using Closed-Form Alpha Matting as proposed by [LLW07].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h \times w \times d\) for which the alpha matte should be estimated
  • trimap (numpy.ndarray)
      Trimap with shape \(h \times w\) of the image
  • preconditioner (function or scipy.sparse.linalg.LinearOperator)
      Function or sparse matrix that applies the preconditioner to a vector (default: ichol)
  • laplacian_kwargs (dictionary)
      Arguments passed to the cf_laplacian function
  • cg_kwargs (dictionary)
      Arguments passed to the cg solver
  • is_known (numpy.ndarray)
      Binary mask of pixels for which to compute the laplacian matrix. Providing this parameter might improve performance if few pixels are unknown.

Returns

  • alpha (numpy.ndarray)
      Estimated alpha matte

Example

>>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_cf( ... image, ... trimap, ... laplacian_kwargs={"epsilon": 1e-6}, ... cg_kwargs={"maxiter":2000})

pymatting.estimate_alpha_knn

🔗

Signature

estimate_alpha_knn( image, trimap, preconditioner=None, laplacian_kwargs={}, cg_kwargs={})

Function Description

Estimate alpha from an input image and an input trimap using KNN Matting similar to [CLT13]. See knn_laplacian for more details.

Parameters

  • image (numpy.ndarray)
      Image with shape \(h \times w \times d\) for which the alpha matte should be estimated
  • trimap (numpy.ndarray)
      Trimap with shape \(h \times w\) of the image
  • preconditioner (function or scipy.sparse.linalg.LinearOperator)
      Function or sparse matrix that applies the preconditioner to a vector (default: jacobi)
  • laplacian_kwargs (dictionary)
      Arguments passed to the knn_laplacian function
  • cg_kwargs (dictionary)
      Arguments passed to the cg solver

Returns

  • alpha (numpy.ndarray)
      Estimated alpha matte

Example

>>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_knn( ... image, ... trimap, ... laplacian_kwargs={"n_neighbors": [15, 10]}, ... cg_kwargs={"maxiter":2000})

pymatting.estimate_alpha_lbdm

🔗

Signature

estimate_alpha_lbdm( image, trimap, preconditioner=None, laplacian_kwargs={}, cg_kwargs={})

Function Description

Estimate alpha from an input image and an input trimap using Learning Based Digital Matting as proposed by [ZK09].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h \times w \times d\) for which the alpha matte should be estimated
  • trimap (numpy.ndarray)
      Trimap with shape \(h \times w\) of the image
  • preconditioner (function or scipy.sparse.linalg.LinearOperator)
      Function or sparse matrix that applies the preconditioner to a vector (default: ichol)
  • laplacian_kwargs (dictionary)
      Arguments passed to the lbdm_laplacian function
  • cg_kwargs (dictionary)
      Arguments passed to the cg solver

Returns

  • alpha (numpy.ndarray)
      Estimated alpha matte

Example

>>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_lbdm( ... image, ... trimap, ... laplacian_kwargs={"epsilon": 1e-6}, ... cg_kwargs={"maxiter":2000})

pymatting.estimate_alpha_lkm

🔗

Signature

estimate_alpha_lkm( image, trimap, laplacian_kwargs={}, cg_kwargs={})

Function Description

Estimate alpha from an input image and an input trimap as described in Fast Matting Using Large Kernel Matting Laplacian Matrices by [HST10].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h \times w \times d\) for which the alpha matte should be estimated
  • trimap (numpy.ndarray)
      Trimap with shape \(h \times w\) of the image
  • laplacian_kwargs (dictionary)
      Arguments passed to the lkm_laplacian function
  • cg_kwargs (dictionary)
      Arguments passed to the cg solver

Returns

  • alpha (numpy.ndarray)
      Estimated alpha matte

Example

>>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_lkm( ... image, ... trimap, ... laplacian_kwargs={"epsilon": 1e-6, "radius": 15}, ... cg_kwargs={"maxiter":2000})

pymatting.estimate_alpha_rw

🔗

Signature

estimate_alpha_rw( image, trimap, preconditioner=None, laplacian_kwargs={}, cg_kwargs={})

Function Description

Estimate alpha from an input image and an input trimap using Learning Based Digital Matting as proposed by [GSAW05].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h \times w \times d\) for which the alpha matte should be estimated
  • trimap (numpy.ndarray)
      Trimap with shape \(h \times w\) of the image
  • preconditioner (function or scipy.sparse.linalg.LinearOperator)
      Function or sparse matrix that applies the preconditioner to a vector (default: jacobi)
  • laplacian_kwargs (dictionary)
      Arguments passed to the rw_laplacian function
  • cg_kwargs (dictionary)
      Arguments passed to the cg solver

Returns

  • alpha (numpy.ndarray)
      Estimated alpha matte

Example

>>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_rw( .... image, ... trimap, ... laplacian_kwargs={"sigma": 0.03}, ... cg_kwargs={"maxiter":2000})

pymatting.estimate_alpha_sm

🔗

Signature

estimate_alpha_sm( image, trimap, return_foreground_background=False, trimap_expansion_radius=10, trimap_expansion_threshold=0.02, sample_gathering_angles=4, sample_gathering_weights=(3.0, 2.0, 1.0, 4.0), sample_gathering_Np_radius=1, sample_refinement_radius=5, local_smoothing_radius1=5, local_smoothing_radius2=5, local_smoothing_radius3=5, local_smoothing_sigma_sq1=None, local_smoothing_sigma_sq2=0.1, local_smoothing_sigma_sq3=None)

Function Description

Estimate alpha from an input image and an input trimap using Shared Matting as proposed by [GO10].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h \times w \times d\) for which the alpha matte should be estimated
  • trimap (numpy.ndarray)
      Trimap with shape \(h \times w\) of the image
  • return_foreground_background (numpy.ndarray)
      Whether to return foreground and background estimate. They will be computed either way
  • trimap_expansion_radius (int)
      How much to expand trimap.
  • trimap_expansion_threshold (float)
      Which pixel colors are similar enough to expand trimap into
  • sample_gathering_angles (int)
      In how many directions to search for new samples.
  • sample_gathering_weights (Tuple[float, float, float, float])
      Weights for various cost functions
  • sample_gathering_Np_radius (int)
      Radius of Np function
  • sample_refinement_radius (int)
      Search region for better neighboring samples
  • local_smoothing_radius1 (int)
      Radius for foreground/background smoothing
  • local_smoothing_radius2 (int)
      Radius for confidence computation
  • local_smoothing_radius3 (int)
      Radius for low frequency alpha computation
  • local_smoothing_sigma_sq1 (float)
      Squared sigma value for foreground/background smoothing Defaults to (2 * local_smoothing_radius1 + 1)**2 / (9 * pi) if not given
  • local_smoothing_sigma_sq2 (float)
      Squared sigma value for confidence computation
  • local_smoothing_sigma_sq3 (float)
      Squared sigma value for low frequency alpha computation Defaults to (2 * local_smoothing_radius3 + 1)**2 / (9 * pi) if not given

Returns

  • alpha (numpy.ndarray)
      Estimated alpha matte
  • foreground (numpy.ndarray)
      Estimated foreground
  • background (numpy.ndarray)
      Estimated background

Example

>>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha, foreground, background = estimate_alpha_sm( ... image, ... trimap, ... return_foreground_background=True, ... sample_gathering_angles=4)