예제 #1
0
    def test_poisson(self):
        cases = []

        # 1D
        cases.append(((1, ), np.array([[2]])))
        cases.append(((2, ), np.array([[2, -1], [-1, 2]])))
        cases.append(((4, ),
                      np.array([[2, -1, 0, 0], [-1, 2, -1, 0], [0, -1, 2, -1],
                                [0, 0, -1, 2]])))

        # 2D
        cases.append(((1, 1), np.array([[4]])))
        cases.append(((2, 1), np.array([[4, -1], [-1, 4]])))
        cases.append(((1, 2), np.array([[4, -1], [-1, 4]])))
        cases.append(((1, 3), np.array([[4, -1, 0], [-1, 4, -1], [0, -1, 4]])))
        cases.append(((2, 2),
                      np.array([[4, -1, -1, 0], [-1, 4, 0, -1], [-1, 0, 4, -1],
                                [0, -1, -1, 4]])))
        # 3D
        cases.append(((2, 2, 1),
                      np.array([[6, -1, -1, 0], [-1, 6, 0, -1], [-1, 0, 6, -1],
                                [0, -1, -1, 6]])))
        cases.append(((2, 2, 2),
                      np.array([[6, -1, -1, 0, -1, 0, 0, 0],
                                [-1, 6, 0, -1, 0, -1, 0, 0],
                                [-1, 0, 6, -1, 0, 0, -1, 0],
                                [0, -1, -1, 6, 0, 0, 0, -1],
                                [-1, 0, 0, 0, 6, -1, -1, 0],
                                [0, -1, 0, 0, -1, 6, 0, -1],
                                [0, 0, -1, 0, -1, 0, 6, -1],
                                [0, 0, 0, -1, 0, -1, -1, 6]])))

        for grid, expected in cases:
            assert_equal(poisson(grid).toarray(), expected)
예제 #2
0
    def test_poisson(self):
        cases = []

        # 1D
        cases.append(((1,), np.array([[2]])))
        cases.append(((2,), np.array([[2, -1],
                                      [-1, 2]])))
        cases.append(((4,), np.array([[2, -1, 0, 0],
                                      [-1, 2, -1, 0],
                                      [0, -1, 2, -1],
                                      [0, 0, -1, 2]])))

        # 2D
        cases.append(((1, 1), np.array([[4]])))
        cases.append(((2, 1), np.array([[4, -1],
                                        [-1, 4]])))
        cases.append(((1, 2), np.array([[4, -1],
                                        [-1, 4]])))
        cases.append(((1, 3), np.array([[4, -1, 0],
                                        [-1, 4, -1],
                                        [0, -1, 4]])))
        cases.append(((2, 2), np.array([[4, -1, -1, 0],
                                        [-1, 4, 0, -1],
                                        [-1, 0, 4, -1],
                                        [0, -1, -1, 4]])))
        # 3D
        cases.append(((2, 2, 1), np.array([[6, -1, -1, 0],
                                           [-1, 6, 0, -1],
                                           [-1, 0, 6, -1],
                                           [0, -1, -1, 6]])))
        cases.append(((2, 2, 2), np.array([[6, -1, -1, 0, -1, 0, 0, 0],
                                           [-1, 6, 0, -1, 0, -1, 0, 0],
                                           [-1, 0, 6, -1, 0, 0, -1, 0],
                                           [0, -1, -1, 6, 0, 0, 0, -1],
                                           [-1, 0, 0, 0, 6, -1, -1, 0],
                                           [0, -1, 0, 0, -1, 6, 0, -1],
                                           [0, 0, -1, 0, -1, 0, 6, -1],
                                           [0, 0, 0, -1, 0, -1, -1, 6]])))

        for grid, expected in cases:
            assert_equal(poisson(grid).toarray(), expected)
예제 #3
0
파일: lumo.py 프로젝트: tody411/InverseToon
def normalConstraints(A_8U, N0_32F, alpha_th=20, w_sil=1e+10):
    h, w = A_8U.shape

    L = laplacian.poisson((h, w))
    L_lil = L.tolil()

    A_flat = A_8U.flatten()
    sil_ids = np.where(A_flat < alpha_th)

    for sil_id in sil_ids:
        L_lil[sil_id, sil_id] = w_sil

    A = L_lil.tocsr()

    N0_flat = N0_32F.reshape(h * w, 3)
    N0_flat[A_flat > alpha_th, :] = 0.0
    b_all = w_sil * N0_flat
    b = np.zeros(b_all.shape)
    b[A_flat < alpha_th, :] = b_all[A_flat < alpha_th, :]

    return A, b
예제 #4
0
def normalConstraints(A_8U, N0_32F, alpha_th=20, w_sil=1e+10):
    h, w = A_8U.shape

    L = laplacian.poisson((h, w))
    L_lil = L.tolil()

    A_flat = A_8U.flatten()
    sil_ids = np.where(A_flat < alpha_th)

    for sil_id in sil_ids:
        L_lil[sil_id, sil_id] = w_sil

    A = L_lil.tocsr()

    N0_flat = N0_32F.reshape(h * w, 3)
    N0_flat[A_flat > alpha_th, :] = 0.0

    b_all = w_sil * N0_flat
    b = np.zeros(b_all.shape)
    b[A_flat < alpha_th, :] = b_all[A_flat < alpha_th, :]

    return A, b
import numpy as np
from pyamg import smoothed_aggregation_solver
from pyamg.gallery import stencil_grid
from pyamg.gallery.diffusion import diffusion_stencil_2d

n = 100
X, Y = np.meshgrid(np.linspace(0, 1, n), np.linspace(0, 1, n))
stencil = diffusion_stencil_2d(type='FE', epsilon=0.001, theta=np.pi / 3)
A = stencil_grid(stencil, (n, n), format='csr')

from pyamg.gallery.laplacian import poisson
A = poisson((n, n), format='csr')
b = np.random.rand(A.shape[0])
ml = smoothed_aggregation_solver(A,
                                 B=X.reshape(n * n, 1),
                                 strength='symmetric',
                                 aggregate='standard',
                                 smooth=('jacobi', {
                                     'omega': 4.0 / 3.0,
                                     'degree': 2
                                 }),
                                 presmoother=('jacobi', {
                                     'omega': 4.0 / 3.0
                                 }),
                                 postsmoother=('jacobi', {
                                     'omega': 4.0 / 3.0
                                 }),
                                 max_levels=10,
                                 max_coarse=5,
                                 keep=False)