Matting Laplacians

pymatting.cf_laplacian

🔗

Signature

cf_laplacian( image, epsilon=1e-7, radius=1, is_known=None)

Function Description

This function implements the alpha estimator for closed-form alpha matting as proposed by [LLW07].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h\times w \times 3\)
  • epsilon (float)
      Regularization strength, defaults to \(10^{-7}\). Strong regularization improves convergence but results in smoother alpha mattes.
  • radius (int)
      Radius of local window size, defaults to \(1\), i.e. only adjacent pixels are considered. The size of the local window is given as \((2 r + 1)^2\), where \(r\) denotes the radius. A larger radius might lead to violated color line constraints, but also favors further propagation of information within the image.
  • is_known (numpy.ndarray)
      Binary mask of pixels for which to compute the laplacian matrix. Laplacian entries for known pixels will have undefined values.

Returns

  • L (scipy.sparse.spmatrix)
      Matting Laplacian

pymatting.knn_laplacian

🔗

Signature

knn_laplacian( image, n_neighbors=[20, 10], distance_weights=[2.0, 0.1], kernel="binary")

Function Description

This function calculates the KNN matting Laplacian matrix similar to [CLT13]. We use a kernel of 1 instead of a soft kernel by default since the former is faster to compute and both produce almost identical results in all our experiments, which is to be expected as the soft kernel is very close to 1 in most cases.

Parameters

  • image (numpy.ndarray)
      Image with shape \(h\times w \times 3\)
  • n_neighbors (list of ints)
      Number of neighbors to consider. If len(n_neighbors)>1 multiple nearest neighbor calculations are done and merged, defaults to [20, 10], i.e. first 20 neighbors are considered and in the second run \(10\) neighbors. The pixel distances are then weighted by the distance_weights.
  • distance_weights (list of floats)
      Weight of distance in feature vector, defaults to [2.0, 0.1].
  • kernel (str)
      Must be either "binary" or "soft". Default is "binary".

Returns

  • L (scipy.sparse.spmatrix)
      Matting Laplacian matrix

pymatting.make_linear_system

🔗

Signature

make_linear_system( L, trimap, lambda_value=100.0, return_c=False)

Function Description

This function constructs a linear system from a matting Laplacian by constraining the foreground and background pixels with a diagonal matrix C to values in the right-hand-side vector b. The constraints are weighted by a factor \(\lambda\). The linear system is given as
$$ A = L + \lambda C, $$
where \(C=\mathop{Diag}(c)\) having \(c_i = 1\) if pixel i is known and \(c_i = 0\) otherwise. The right-hand-side \(b\) is a vector with entries \(b_i = 1\) is pixel is is a foreground pixel and \(b_i = 0\) otherwise.

Parameters

  • L (scipy.sparse.spmatrix)
      Laplacian matrix, e.g. calculated with lbdm_laplacian function
  • trimap (numpy.ndarray)
      Trimap with shape \(h\times w\)
  • lambda_value (float)
      Constraint penalty, defaults to 100
  • return_c (bool)
      Whether to return the constraint matrix C, defaults to False

Returns

  • A (scipy.sparse.spmatrix)
      Matrix describing the system of linear equations
  • b (numpy.ndarray)
      Vector describing the right-hand side of the system
  • C (numpy.ndarray)
      Vector describing the diagonal entries of the matrix C, only returned if return_c is set to True

pymatting.lbdm_laplacian

🔗

Signature

lbdm_laplacian(image, epsilon=1e-7, radius=1)

Function Description

Calculate a Laplacian matrix based on [ZK09].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h\times w \times 3\)
  • epsilon (float)
      Regularization strength, defaults to \(10^{-7}\). Strong regularization improves convergence but results in smoother alpha mattes.
  • radius (int)
      Radius of local window size, defaults to \(1\), i.e. only adjacent pixels are considered. The size of the local window is given as \((2 r + 1)^2\), where \(r\) denotes the radius. A larger radius might lead to violated color line constraints, but also favors further propagation of information within the image.

Returns

  • L (scipy.sparse.csr_matrix)
      Matting Laplacian

pymatting.lkm_laplacian

🔗

Signature

lkm_laplacian( image, epsilon=1e-7, radius=10, return_diagonal=True)

Function Description

Calculates the Laplacian for large kernel matting [HST10]

Parameters

  • image (numpy.ndarray)
      Image of shape \(h\times w \times 3\)
  • epsilons (float)
      Regularization strength, defaults to \(10^{-7}\)
  • radius (int)
      Radius of local window size, defaults to \(10\), i.e. only adjacent pixels are considered. The size of the local window is given as \((2 r + 1)^2\), where \(r\) denotes the radius. A larger radius might lead to violated color line constraints, but also favors further propagation of information within the image.
  • return_diagonal (bool)
      Whether to also return the diagonal of the laplacian, defaults to True

Returns

  • L_matvec (function)
      Function that applies the Laplacian matrix to a vector
  • diag_L (numpy.ndarray)
      Diagonal entries of the matting Laplacian, only returns if return_diagonal is True

pymatting.rw_laplacian

🔗

Signature

rw_laplacian( image, sigma=0.033, radius=1, regularization=1e-8)

Function Description

This function implements the alpha estimator for random walk alpha matting as described in [GSAW05].

Parameters

  • image (numpy.ndarray)
      Image with shape \(h\times w \times 3\)
  • sigma (float)
      Sigma used to calculate the weights (see Equation 4 in [GSAW05]), defaults to \(0.033\)
  • radius (int)
      Radius of local window size, defaults to \(1\), i.e. only adjacent pixels are considered. The size of the local window is given as \((2 r + 1)^2\), where \(r\) denotes the radius. A larger radius might lead to violated color line constraints, but also favors further propagation of information within the image.
  • regularization (float)
      Regularization strength, defaults to \(10^{-8}\). Strong regularization improves convergence but results in smoother alpha matte.

Returns

  • L (scipy.sparse.spmatrix)
      Matting Laplacian

pymatting.uniform_laplacian

🔗

Signature

uniform_laplacian(image, radius=1)

Function Description

This function returns a Laplacian matrix with all weights equal to one.

Parameters

  • image (numpy.ndarray)
      Image with shape \(h\times w \times 3\)
  • radius (int)
      Radius of local window size, defaults to 1, i.e. only adjacent pixels are considered. The size of the local window is given as \((2 r + 1)^2\), where \(r\) denotes the radius. A larger radius might lead to violated color line constraints, but also favors further propagation of information within the image.

Returns

  • L (scipy.sparse.spmatrix)
      Matting Laplacian