Пример #1
0
 def __init__(self, image, num_pixels=1000):
     self._image = to32F(image)
     self._num_pixels = num_pixels
     self._pixels = None
     self._coords = None
     self._computeCoordinates()
     self._pixels = self._image2pixels(image)
Пример #2
0
def silhouetteNormal(A_8U, sigma=7.0):
    height, width = A_8U.shape[0], A_8U.shape[1]

    A_8U_blur = cv2.GaussianBlur(A_8U, (0, 0), sigma)
    A_8U_blur = to32F(A_8U_blur)

    gx = cv2.Sobel(A_8U_blur, cv2.CV_64F, 1, 0, ksize=5)
    gy = cv2.Sobel(A_8U_blur, cv2.CV_64F, 0, 1, ksize=5)

    N_32F = np.zeros((height, width, 3), dtype=np.float32)

    N_32F[:, :, 0] = -gx
    N_32F[:, :, 1] = gy
    N_32F[:, :, 2] = A_8U_blur

    gxy_norm = np.zeros((height, width))

    gxy_norm[:, :] = np.sqrt(gx[:, :] * gx[:, :] + gy[:, :] * gy[:, :])

    Nxy_norm = np.zeros((height, width))
    Nxy_norm[:, :] = np.sqrt(1.0 - A_8U_blur[:, :])

    wgxy = np.zeros((height, width))
    wgxy[:, :] = Nxy_norm[:, :] / (0.001 + gxy_norm[:, :])

    N_32F[:, :, 0] = wgxy[:, :] * N_32F[:, :, 0]
    N_32F[:, :, 1] = wgxy[:, :] * N_32F[:, :, 1]

    return N_32F
Пример #3
0
def cleanNormal(N_32F, A_8U):
    # N_32F = cv2.bilateralFilter(N_32F, 0, 0.1, 5)
    h, w = N_32F.shape[:2]

    plt.subplot(1, 2, 1)
    plt.gray()
    plt.imshow(normVectors(N_32F.reshape(-1, 3)).reshape(h, w))

    plt.subplot(1, 2, 2)
    plt.gray()
    A_32F = to32F(A_8U)
    A_32F = cv2.GaussianBlur(A_32F, (0, 0), 3.0)
    A_32F = np.clip(10.0 * (A_32F - 0.5) + 0.5, 0.0, 1.0)
    A_32F = cv2.GaussianBlur(A_32F, (0, 0), 3.0)
    N_fix = A_32F > 0.9
    N_bg = A_32F < 0.25
    A_32F = np.clip(10.0 * (A_32F - 0.5) + 0.5, 0.0, 1.0)
    A_8U = to8U(A_32F)
#     plt.imshow(A_8U)
#     plt.show()

    N_32F_blur = cv2.GaussianBlur(N_32F, (0, 0), 3.0)
    for i in xrange(10):
        N_32F_blur = cv2.GaussianBlur(N_32F_blur, (0, 0), 3.0)
        N_32F_blur[N_fix, :] = N_32F[N_fix, :]

    N_32F = N_32F_blur
    # N_32F[N_bg, 2] = 0.0
    N_32F_normalized = normalizeImage(N_32F)

    #A_8U = np.uint8(np.clip(1000.0 * N_32F_normalized[:, :, 2], 0.0, 255.0))
    # A_8U = cv2.bilateralFilter(A_8U, 0, 70, 5)

    return N_32F_normalized, A_8U
Пример #4
0
def loadData(data_name, L,
             borders=[0.5, 0.8],
             colors=[np.array([0.2, 0.2, 0.5]), np.array([0.3, 0.3, 0.6]), np.array([0.5, 0.5, 0.8])]):

    N_32F, A_8U = normal.loadData(data_name)
    C_32F = toon.diffuse(N_32F, L, borders, colors)

    C_32F = setAlpha(C_32F, to32F(A_8U))
    return C_32F
def estimateResultFunc(data_name, target_name, N_32F, L_g, I_32F, A_8U, method_name, estimate_func):
    L_g = normalizeVector(L_g)
    silhouette_curve, S_8U = silhoutteCurve(A_8U)
    N_sil = silhouetteNormal(A_8U)
    silhouette_curve.setNormalImage(N_sil)

    N_sil = silhouette_curve.normals()

    cvs_sil = silhouette_curve.CVs()

    I_sil = np.array([I_32F[cv[1], cv[0]] for cv in cvs_sil])

    input_data = {"N_sil": N_sil, "I_sil": I_sil, "cvs_sil": cvs_sil, "I": I_32F, "A": A_8U}

    fig = plt.figure(figsize=(8, 8))
    fig.suptitle("Light estimation: %s" % method_name)
    fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.82, wspace=0.02, hspace=0.4)

    plt.subplot(2, 2, 1)
    plt.title("Ground truth")
    showLightSphere(plt, L_g)
    plt.axis("off")

    plt.subplot(2, 2, 2)
    plt.title("Illumination")

    A_32F = to32F(A_8U)
    I_org = I_32F * A_32F
    plt.gray()
    plt.imshow(I_org)
    plt.axis("off")

    output_data = estimate_func(input_data)
    L = output_data["L"]
    L = normalizeVector(L)
    L_error = angleError(L_g, L)
    plt.subplot(2, 2, 3)
    plt.title("Estimated error\n %4.1f $^\circ$" % L_error)
    showLightSphere(plt, L)
    plt.axis("off")

    I_est = lambert.diffuse(N_32F, L) * A_32F
    plt.subplot(2, 2, 4)
    plt.gray()
    plt.imshow(I_est)
    plt.axis("off")

    result_dir = resultDir("LightEstimation/%s" % data_name)
    result_file = resultFile(result_dir, "%s_%s" % (target_name, method_name))
    plt.savefig(result_file)
Пример #6
0
def colorToNormal(C_8U, fill_background=False):
    rgb_8U = rgb(C_8U)
    A_8U = alpha(C_8U)

    C_32F = to32F(rgb_8U)

    N_32F = 2.0 * C_32F - 1.0
    N_32F = cv2.bilateralFilter(N_32F, 0, 0.1, 5)

    if fill_background:
        N_32F[A_8U < 10, :] = np.array([0.0, 0.0, 0.0])

    N_32F_normalized = normalizeImage(N_32F)

    return N_32F_normalized