def computeErrorTables(Lg=normalizeVector(np.array([-0.2, 0.3, 0.6]))):
    colormap_files = colorMapFiles()
    shape_names = shapeNames()

    num_materials = len(colormap_files)
    num_shapes = len(shape_names)

    L_errors = np.zeros((num_shapes, num_materials))

    Ms = []
    for colormap_file in colormap_files:
        M_32F = loadColorMap(colormap_file)
        Ms.append(M_32F)

    for si, shape_name in enumerate(shape_names):
        Ng_data = shapeFile(shape_name)

        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
        N0_data = loadNormal(N0_file)
        N0_32F, A_8U = N0_data

        for mi, M_32F in enumerate(Ms):
            C0_32F = ColorMapShader(M_32F).diffuseShading(Lg, Ng_32F)
            I_32F = luminance(C0_32F)

            L = lightEstimation(I_32F, N0_32F, A_8U)

            L_errors[si, mi] = angleError(Lg, L)

    file_path = shapeResultFile("LightEstimation", "LightEstimationError", file_ext=".npy")
    np.save(file_path, L_errors)
def materialErrorTable():
    shape_name = "ThreeBox"

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    num_materials = len(colorMapFiles())
    C_errors = np.zeros((num_materials, 3))
    N_errors = np.zeros((num_materials, 3))
    I_errors = np.zeros((num_materials, 3))

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data
    A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)

    for mi, color_map_file in enumerate(colorMapFiles()):
        M_32F = loadColorMap(color_map_file)

        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        toon_sfs = ToonSFS(L, C0_32F, A_8U)
        toon_sfs.setInitialNormal(N0_32F)
        toon_sfs.setNumIterations(iterations=20)
        toon_sfs.setWeights(w_lap=9.0)
        toon_sfs.run()

        N_toon = toon_sfs.normal()
        C_toon = toon_sfs.shading()

        C_lumo, N_lumo = lumoSFS(C0_32F, L, N0_32F, A_8U)
        C_wu, N_wu = wuSFS(C0_32F, L, N0_32F, A_8U)

        C_error_toon, N_error_toon, I_error_toon = computeErrors(L, C0_32F, C_toon, Ng_32F, N_toon, A_8U)
        C_error_lumo, N_error_lumo, I_error_lumo = computeErrors(L, C0_32F, C_lumo, Ng_32F, N_lumo, A_8U)
        C_error_wu, N_error_wu, I_error_wu = computeErrors(L, C0_32F, C_wu, Ng_32F, N_wu, A_8U)

        C_errors[mi, 0] = np.mean(C_error_toon)
        C_errors[mi, 1] = np.mean(C_error_lumo)
        C_errors[mi, 2] = np.mean(C_error_wu)

        I_errors[mi, 0] = np.mean(I_error_toon)
        I_errors[mi, 1] = np.mean(I_error_lumo)
        I_errors[mi, 2] = np.mean(I_error_wu)

        N_errors[mi, 0] = np.mean(N_error_toon)
        N_errors[mi, 1] = np.mean(N_error_lumo)
        N_errors[mi, 2] = np.mean(N_error_wu)

    file_path = shapeResultFile("ShapeEstimation", "MaterialShadingError", file_ext=".npy")
    np.save(file_path, C_errors)

    file_path = shapeResultFile("ShapeEstimation", "MaterialNormalError", file_ext=".npy")
    np.save(file_path, N_errors)

    file_path = shapeResultFile("ShapeEstimation", "MaterialIlluminationError", file_ext=".npy")
    np.save(file_path, I_errors)
Exemplo n.º 3
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        C0_32F = to32F(rgb(C0_8U))
        I_32F = luminance(C0_32F)

        N0_32F, A_8U = loadNormal(
            self.characterResultFile("N0_d.png",
                                     data_name="BaseDetailSepration"))
        Nd_32F, A_8U = loadNormal(
            self.characterResultFile("N_d_smooth.png",
                                     data_name="BaseDetailSepration"))
        Nb_32F, A_8U = loadNormal(
            self.characterResultFile("N_b_smooth.png",
                                     data_name="BaseDetailSepration"))

        W_32F = np.array(Nb_32F[:, :, 2])
        W_32F = W_32F
        W_32F[W_32F < 0.95] = 0.0

        L = lightEstimation(I_32F, N0_32F, A_8U)
        # L = lightEstimationByVoting(I_32F, N0_32F, A_8U)

        L_txt = 0.01 * np.int32(100 * L)

        L_img = lightSphere(L)

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05,
                            right=0.95,
                            top=0.9,
                            hspace=0.12,
                            wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 1
        num_cols = 4
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(C0_8U, r'$C$')
        plot_grid.showImage(normalToColor(N0_32F, A_8U), r'$N$')
        plot_grid.showImage(setAlpha(C0_32F, W_32F), r'$Nd_z$')
        plot_grid.showImage(
            L_img, r'$L: [%s, %s, %s]$' % (L_txt[0], L_txt[1], L_txt[2]))

        showMaximize()
    def _runImp(self):
        N0_file = shapeResultFile(result_name="InitialNormal", data_name=self._data_name)
        N0_data = loadNormal(N0_file)

        N0_32F, A_8U = N0_data

        Ng_data = loadNormal(self._data_file)
        Ng_32F, A_8U = Ng_data

        for colormap_file in colorMapFiles():
            self._runColorMap(colormap_file, Ng_32F, N0_32F, A_8U)
Exemplo n.º 5
0
    def _runImp(self):
        N0_file = shapeResultFile(result_name="InitialNormal",
                                  data_name=self._data_name)
        N0_data = loadNormal(N0_file)

        N0_32F, A_8U = N0_data

        Ng_data = loadNormal(self._data_file)
        Ng_32F, A_8U = Ng_data

        for colormap_file in colorMapFiles():
            self._runColorMap(colormap_file, Ng_32F, N0_32F, A_8U)
Exemplo n.º 6
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        #N0_32F = cv2.resize(N0_32F, (64, 64))
        #A_8U = cv2.resize(A_8U, N0_32F.shape[:2])

        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        # C0_32F = ToonShader().diffuseShading(L, N0_32F)
        C0_32F = LambertShader().diffuseShading(L, N0_32F)

        sfs_method = Wu08SFS(L, C0_32F, A_8U)
        sfs_method.run()
        N_32F = sfs_method.normal()

        saveNormal(self.resultFile(self._data_file_name, result_name="Wu08"), N_32F, A_8U)

        C_error = sfs_method.shadingError()
        I_32F = sfs_method.brightness()
        I_32F = gray2rgb(I_32F)
        C_32F = sfs_method.shading()

        N0_32F = trim(N0_32F, A_8U)
        C0_32F = trim(C0_32F, A_8U)
        C_32F = trim(C_32F, A_8U)
        N_32F = trim(N_32F, A_8U)
        C_error = trim(C_error, A_8U)
        I_32F = trim(I_32F, A_8U)
        A_32F = trim(A_32F, A_8U)
        A_8U = trim(A_8U, A_8U)

        h, w = N_32F.shape[:2]
        N_error = angleErros(N_32F.reshape(-1, 3), N0_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < np.max(A_8U)] = 0.0

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 2
        num_cols = 3
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(normalToColor(N0_32F, A_8U), r'Ground Truth Normal: $N_g$')
        plot_grid.showImage(normalToColor(N_32F, A_8U), r'Estimated Normal: $N$')
        plot_grid.showColorMap(N_error, r'Angle Error: $N_g, N$', v_min=0, v_max=30.0)

        plot_grid.showImage(setAlpha(C0_32F, A_32F), r'Shading: $C$')
        plot_grid.showImage(setAlpha(C_32F, A_32F), r'Estimated Shading: $C$')
        plot_grid.showColorMap(C_error, r'Shading Error: $C_g, C$', v_min=0, v_max=0.1)

        showMaximize()
def shapeList():

    num_rows = 1
    num_cols = len(shapeNames())

    w = 20
    h = w * num_rows / num_cols

    cmap_id = 10
    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.04, hspace=0.15, wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    for shape_name in shapeNames():
        Ng_data = shapeFile(shape_name)
        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "")

    file_path = shapeResultFile("ShapeEstimation", "ShapeList")
    fig.savefig(file_path, transparent=True)
def materialList():
    shape_name = "ThreeBox"
    num_rows = 1
    num_cols = len(colorMapFiles())

    w = 24
    h = w * num_rows / num_cols

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.04, hspace=0.1, wspace=0.05)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    for colormap_file in colorMapFilesSortedReflectanceError():
        M_32F = loadColorMap(colormap_file)


        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "")

    file_path = shapeResultFile("ShapeEstimation", "MaterialList")
    fig.savefig(file_path, transparent=True)
Exemplo n.º 9
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        # N0_32F = cv2.resize(N0_32F, (64, 64))
        # A_8U = cv2.resize(A_8U, N0_32F.shape[:2])

        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        # C0_32F = ToonShader().diffuseShading(L, N0_32F)
        C0_32F = LambertShader().diffuseShading(L, N0_32F)

        sfs_method = Wu08SFS(L, C0_32F, A_8U)
        sfs_method.run()
        N_32F = sfs_method.normal()

        saveNormal(self.resultFile(self._data_file_name, result_name="Wu08"), N_32F, A_8U)

        C_error = sfs_method.shadingError()
        I_32F = sfs_method.brightness()
        I_32F = gray2rgb(I_32F)
        C_32F = sfs_method.shading()

        N0_32F = trim(N0_32F, A_8U)
        C0_32F = trim(C0_32F, A_8U)
        C_32F = trim(C_32F, A_8U)
        N_32F = trim(N_32F, A_8U)
        C_error = trim(C_error, A_8U)
        I_32F = trim(I_32F, A_8U)
        A_32F = trim(A_32F, A_8U)
        A_8U = trim(A_8U, A_8U)

        h, w = N_32F.shape[:2]
        N_error = angleErros(N_32F.reshape(-1, 3), N0_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < np.max(A_8U)] = 0.0

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 2
        num_cols = 3
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(normalToColor(N0_32F, A_8U), r"Ground Truth Normal: $N_g$")
        plot_grid.showImage(normalToColor(N_32F, A_8U), r"Estimated Normal: $N$")
        plot_grid.showColorMap(N_error, r"Angle Error: $N_g, N$", v_min=0, v_max=30.0)

        plot_grid.showImage(setAlpha(C0_32F, A_32F), r"Shading: $C$")
        plot_grid.showImage(setAlpha(C_32F, A_32F), r"Estimated Shading: $C$")
        plot_grid.showColorMap(C_error, r"Shading Error: $C_g, C$", v_min=0, v_max=0.1)

        showMaximize()
def materialList():
    shape_name = "ThreeBox"
    num_rows = 1
    num_cols = len(colorMapFiles())

    w = 24
    h = w * num_rows / num_cols

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.96,
                        bottom=0.04,
                        hspace=0.1,
                        wspace=0.05)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    for colormap_file in colorMapFilesSortedReflectanceError():
        M_32F = loadColorMap(colormap_file)

        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "")

    file_path = shapeResultFile("ShapeEstimation", "MaterialList")
    fig.savefig(file_path, transparent=True)
def computeLambertShadingError():
    shape_names = shapeNames()

    L = normalizeVector(np.array([-0.2, 0.3, 0.6]))

    C_errors = np.zeros(len(shape_names))
    N_errors = np.zeros(len(shape_names))

    for si, shape_name in enumerate(shape_names):
        Ng_data = shapeFile(shape_name)

        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        N0_file = shapeResultFile(result_name="InitialNormal",
                                  data_name=shape_name)
        N0_data = loadNormal(N0_file)
        N0_32F, A_8U = N0_data

        C0_32F = LambertShader().diffuseShading(L, Ng_32F)

        sfs_method = ToonSFS(L, C0_32F, A_8U)
        sfs_method.setInitialNormal(N0_32F)
        sfs_method.setNumIterations(iterations=60)
        sfs_method.run()
        N_32F = sfs_method.normal()
        C_32F = sfs_method.shading()
        C_error = sfs_method.shadingError()
        C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0

        C_errors[si] = np.average(C_error[A_8U > 0.5 * np.max(A_8U)])

        h, w = A_8U.shape[:2]
        N_error = angleErros(N_32F.reshape(-1, 3),
                             Ng_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        N_errors[si] = np.average(N_error[A_8U > 0.5 * np.max(A_8U)])

    file_path = shapeResultFile("ShapeEstimation",
                                "ShadingError",
                                file_ext=".npy")
    np.save(file_path, C_errors)

    file_path = shapeResultFile("ShapeEstimation",
                                "NormalError",
                                file_ext=".npy")
    np.save(file_path, N_errors)
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        C0_32F = to32F(rgb(C0_8U))
        I_32F = luminance(C0_32F)

        N0_32F, A_8U = loadNormal(self.characterResultFile("N0_d.png", data_name="BaseDetailSepration"))
        Nd_32F, A_8U = loadNormal(self.characterResultFile("N_d_smooth.png", data_name="BaseDetailSepration"))
        Nb_32F, A_8U = loadNormal(self.characterResultFile("N_b_smooth.png", data_name="BaseDetailSepration"))

        W_32F = np.array(Nb_32F[:, :, 2])
        W_32F = W_32F
        W_32F[W_32F < 0.95] = 0.0

        L = lightEstimation(I_32F, N0_32F, A_8U)
        # L = lightEstimationByVoting(I_32F, N0_32F, A_8U)

        L_txt = 0.01 * np.int32(100 * L)

        L_img = lightSphere(L)

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 1
        num_cols = 4
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(C0_8U, r'$C$')
        plot_grid.showImage(normalToColor(N0_32F, A_8U), r'$N$')
        plot_grid.showImage(setAlpha(C0_32F, W_32F), r'$Nd_z$')
        plot_grid.showImage(L_img, r'$L: [%s, %s, %s]$' %(L_txt[0], L_txt[1], L_txt[2]))

        showMaximize()
Exemplo n.º 13
0
    def _runImp(self):
        # file_path = self._data_file
        file_path = self.resultFile(self._data_file_name, result_name="Wu08")
        normal_data = loadNormal(file_path)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        D_32F = depthFromNormal(N0_32F, A_8U)

        fig, axes = plt.subplots(figsize=(11, 5))

        font_size = 15

        fig.subplots_adjust(left=0.05,
                            right=0.95,
                            top=0.9,
                            hspace=0.05,
                            wspace=0.05)
        fig.suptitle("Depth From Normal", fontsize=font_size)

        plt.subplot(1, 4, 1)
        plt.title(r'Initial Normal: $N_0$')
        plt.imshow(normalToColor(N0_32F, A_8U))
        plt.axis('off')

        plt.subplot(1, 4, 2)
        plt.title(r'Estimated Depth: $D$')
        plt.gray()
        plt.imshow(D_32F)
        plt.axis('off')

        N_32F = depthToNormal(D_32F)
        plt.subplot(1, 4, 3)
        plt.title(r'Recovered Normal: $N$')
        plt.imshow(normalToColor(N_32F, A_8U))
        plt.axis('off')

        h, w = N_32F.shape[:2]
        N_diff = angleErros(N_32F.reshape(-1, 3),
                            N0_32F.reshape(-1, 3)).reshape(h, w)
        N_diff[A_8U < np.max(A_8U)] = 0.0
        plt.subplot(1, 4, 4)
        plt.title(r'Angle Error: $N, N_0$')
        plt.imshow(N_diff, cmap=plt.cm.jet, vmin=0, vmax=30.0)
        plt.axis('off')

        plt.colorbar()

        #out_file_path = self.resultFile(self._data_file_name)
        out_file_path = self.resultFile(self._data_file_name,
                                        result_name="Wu08Depth")
        plt.savefig(out_file_path)
def computeLambertShadingError():
    shape_names = shapeNames()

    L = normalizeVector(np.array([-0.2, 0.3, 0.6]))

    C_errors = np.zeros(len(shape_names))
    N_errors = np.zeros(len(shape_names))

    for si, shape_name in enumerate(shape_names):
        Ng_data = shapeFile(shape_name)

        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
        N0_data = loadNormal(N0_file)
        N0_32F, A_8U = N0_data

        C0_32F = LambertShader().diffuseShading(L, Ng_32F)

        sfs_method = ToonSFS(L, C0_32F, A_8U)
        sfs_method.setInitialNormal(N0_32F)
        sfs_method.setNumIterations(iterations=60)
        sfs_method.run()
        N_32F = sfs_method.normal()
        C_32F = sfs_method.shading()
        C_error = sfs_method.shadingError()
        C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0

        C_errors[si] = np.average(C_error[A_8U > 0.5 * np.max(A_8U)])

        h, w = A_8U.shape[:2]
        N_error = angleErros(N_32F.reshape(-1, 3), Ng_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        N_errors[si] = np.average(N_error[A_8U > 0.5 * np.max(A_8U)])

    file_path = shapeResultFile("ShapeEstimation", "ShadingError", file_ext=".npy")
    np.save(file_path, C_errors)

    file_path = shapeResultFile("ShapeEstimation", "NormalError", file_ext=".npy")
    np.save(file_path, N_errors)
Exemplo n.º 15
0
    def _runImp(self):
        # file_path = self._data_file
        file_path = self.resultFile(self._data_file_name, result_name="Wu08")
        normal_data = loadNormal(file_path)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        D_32F = depthFromNormal(N0_32F, A_8U)

        fig, axes = plt.subplots(figsize=(11, 5))

        font_size = 15

        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.05, wspace=0.05)
        fig.suptitle("Depth From Normal", fontsize=font_size)

        plt.subplot(1, 4, 1)
        plt.title(r'Initial Normal: $N_0$')
        plt.imshow(normalToColor(N0_32F, A_8U))
        plt.axis('off')

        plt.subplot(1, 4, 2)
        plt.title(r'Estimated Depth: $D$')
        plt.gray()
        plt.imshow(D_32F)
        plt.axis('off')

        N_32F = depthToNormal(D_32F)
        plt.subplot(1, 4, 3)
        plt.title(r'Recovered Normal: $N$')
        plt.imshow(normalToColor(N_32F, A_8U))
        plt.axis('off')

        h, w = N_32F.shape[:2]
        N_diff = angleErros(N_32F.reshape(-1, 3), N0_32F.reshape(-1, 3)).reshape(h, w)
        N_diff[A_8U < np.max(A_8U)] = 0.0
        plt.subplot(1, 4, 4)
        plt.title(r'Angle Error: $N, N_0$')
        plt.imshow(N_diff, cmap=plt.cm.jet, vmin=0, vmax=30.0)
        plt.axis('off')

        plt.colorbar()

        #out_file_path = self.resultFile(self._data_file_name)
        out_file_path = self.resultFile(self._data_file_name, result_name="Wu08Depth")
        plt.savefig(out_file_path)
Exemplo n.º 16
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N_32F, A_8U = normal_data

        N_32F = trim(N_32F, A_8U)
        A_8U = trim(A_8U, A_8U)
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        I_half = half_lambert.diffuse(N_32F, L)
        I_half = setAlpha(gray2rgb(I_half), A_32F)

        I_lambert = lambert.diffuse(N_32F, L)
        I_lambert = setAlpha(gray2rgb(I_lambert), A_32F)

        fig, axes = plt.subplots(figsize=(11, 5))

        font_size = 15

        fig.subplots_adjust(left=0.05,
                            right=0.95,
                            top=0.9,
                            hspace=0.05,
                            wspace=0.05)
        fig.suptitle("Depth From Normal", fontsize=font_size)

        plt.subplot(1, 4, 1)
        plt.title(r'Normal: $N$')
        plt.imshow(normalToColor(N_32F, A_8U))
        plt.axis('off')

        plt.subplot(1, 4, 2)
        plt.title(r'Half Lambert: $I_h$')
        plt.imshow(I_half)
        plt.axis('off')

        plt.subplot(1, 4, 3)
        plt.title(r'Lambert: $I_l$')
        plt.imshow(I_lambert)
        plt.axis('off')

        showMaximize()
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        # C0_32F = ToonShader().diffuseShading(L, N0_32F)
        C0_32F = LambertShader().diffuseShading(L, N0_32F)
        self._C0_32F = C0_32F

        self._loadImage()
Exemplo n.º 18
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        # C0_32F = ToonShader().diffuseShading(L, N0_32F)
        C0_32F = LambertShader().diffuseShading(L, N0_32F)
        self._C0_32F = C0_32F

        self._loadImage()
Exemplo n.º 19
0
def stylizedShadingFigure():
    target_shapes = ["Man", "Ogre", "Grog", "Vase"]
    target_shapes = [shapeFile(shape_name) for shape_name in target_shapes]

    target_colormaps = [1, 5, 10, 3]
    target_colormaps = [colorMapFile(cmap_id) for cmap_id in target_colormaps]

    fig, axes = plt.subplots(figsize=(12, 4))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.98,
                        bottom=0.02,
                        hspace=0.1,
                        wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    num_rows = 1
    num_cols = 4
    plot_grid = SubplotGrid(num_rows, num_cols)

    Ls = []
    Ls.append(normalizeVector(np.array([-0.5, 0.3, 0.7])))
    Ls.append(normalizeVector(np.array([0.2, -0.35, 0.4])))
    Ls.append(normalizeVector(np.array([-0.2, 0.6, 0.3])))
    Ls.append(normalizeVector(np.array([-0.2, 0.6, 0.3])))

    for shape_file, colormap_file, L in zip(target_shapes, target_colormaps,
                                            Ls):
        N_32F, A_8U = loadNormal(shape_file)
        M_32F = loadColorMap(colormap_file)
        C_32F = ColorMapShader(M_32F).diffuseShading(L, N_32F)
        #         C_32F = trim(C_32F, A_8U)
        #         A_8U = trim(A_8U, A_8U)
        C_32F = setAlpha(C_32F, to32F(A_8U))

        #         h, w = C_32F.shape[:2]
        #
        #         h_t = 512
        #         w_t = w * h_t / h
        #         C_32F = cv2.resize(C_32F, (w_t, h_t))
        plot_grid.showImage(C_32F, "", alpha_clip=True)

    file_path = os.path.join(shapeResultsDir(), "StylizedShading.png")
    fig.savefig(file_path, transparent=True)
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        C0_32F = LambertShader().diffuseShading(L, N0_32F)

        self._normal_constraint.clear()

        if os.path.exists(self.constraintFile()):
            self._normal_constraint.load(self.constraintFile())
        self._tool.setImage(setAlpha(C0_32F, A_32F))
Exemplo n.º 21
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        C0_32F = LambertShader().diffuseShading(L, N0_32F)

        self._normal_constraint.clear()

        if os.path.exists(self.constraintFile()):
            self._normal_constraint.load(self.constraintFile())
        self._tool.setImage(setAlpha(C0_32F, A_32F))
Exemplo n.º 22
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N_32F, A_8U = normal_data

        N_32F = trim(N_32F, A_8U)
        A_8U = trim(A_8U, A_8U)
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        I_half = half_lambert.diffuse(N_32F, L)
        I_half = setAlpha(gray2rgb(I_half), A_32F)

        I_lambert = lambert.diffuse(N_32F, L)
        I_lambert = setAlpha(gray2rgb(I_lambert), A_32F)

        fig, axes = plt.subplots(figsize=(11, 5))

        font_size = 15

        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.05, wspace=0.05)
        fig.suptitle("Depth From Normal", fontsize=font_size)

        plt.subplot(1, 4, 1)
        plt.title(r'Normal: $N$')
        plt.imshow(normalToColor(N_32F, A_8U))
        plt.axis('off')

        plt.subplot(1, 4, 2)
        plt.title(r'Half Lambert: $I_h$')
        plt.imshow(I_half)
        plt.axis('off')

        plt.subplot(1, 4, 3)
        plt.title(r'Lambert: $I_l$')
        plt.imshow(I_lambert)
        plt.axis('off')

        showMaximize()
Exemplo n.º 23
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.4, 0.7]))

        C0_32F = LambertShader().diffuseShading(L, N0_32F)
        I_32F = luminance(C0_32F)

        br_field = BrightnessField(I_32F, sigma=5.0)
        I_smooth_32F = br_field.smoothBrightness()
        dI = br_field.brightnessDifference()
        gx, gy = br_field.gradients()

        N_32F = br_field.field()

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05,
                            right=0.95,
                            top=0.9,
                            hspace=0.12,
                            wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 2
        num_cols = 4
        plot_grid = SubplotGrid(num_rows, num_cols)
        plot_grid.showImage(I_32F, r'$I$')
        plot_grid.showColorMap(dI, r'$dI$')
        plot_grid.showColorMap(gx, r'$gx$')
        plot_grid.showColorMap(gy, r'$gy$')

        plot_grid.showImage(normalToColor(N_32F, A_8U), r'$N$')
        plot_grid.showImage(N_32F[:, :, 2], r'$N_z$')

        showMaximize()
Exemplo n.º 24
0
def stylizedShadingFigure():
    target_shapes = ["Man", "Ogre", "Grog", "Vase"]
    target_shapes = [shapeFile(shape_name) for shape_name in target_shapes]

    target_colormaps = [1, 5, 10, 3]
    target_colormaps = [colorMapFile(cmap_id) for cmap_id in target_colormaps]

    fig, axes = plt.subplots(figsize=(12, 4))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.98, bottom=0.02, hspace=0.1, wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    num_rows = 1
    num_cols = 4
    plot_grid = SubplotGrid(num_rows, num_cols)

    Ls = []
    Ls.append(normalizeVector(np.array([-0.5, 0.3, 0.7])))
    Ls.append(normalizeVector(np.array([0.2, -0.35, 0.4])))
    Ls.append(normalizeVector(np.array([-0.2, 0.6, 0.3])))
    Ls.append(normalizeVector(np.array([-0.2, 0.6, 0.3])))


    for shape_file, colormap_file, L in zip(target_shapes, target_colormaps, Ls):
        N_32F, A_8U = loadNormal(shape_file)
        M_32F = loadColorMap(colormap_file)
        C_32F = ColorMapShader(M_32F).diffuseShading(L, N_32F)
#         C_32F = trim(C_32F, A_8U)
#         A_8U = trim(A_8U, A_8U)
        C_32F = setAlpha(C_32F, to32F(A_8U))

#         h, w = C_32F.shape[:2]
#
#         h_t = 512
#         w_t = w * h_t / h
#         C_32F = cv2.resize(C_32F, (w_t, h_t))
        plot_grid.showImage(C_32F, "", alpha_clip=True)

    file_path = os.path.join(shapeResultsDir(), "StylizedShading.png")
    fig.savefig(file_path, transparent=True)
Exemplo n.º 25
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        h, w = A_8U.shape[:2]
        A_c, b_c = amg_constraints.silhouetteConstraints(A_8U, is_flat=True)

        A_L = amg_constraints.laplacianMatrix((h, w), num_elements=3)
        A = A_c + A_L
        b = b_c

        N = amg_solver.solve(A, b).reshape(-1, 3)
        N = computeNz(N)
        N = normalizeVectors(N)
        N_32F = N.reshape(h, w, 3)

        file_path = self.resultFile(self._data_file_name)
        saveNormal(file_path, N_32F, A_8U)
Exemplo n.º 26
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        h, w = A_8U.shape[:2]
        A_c, b_c = amg_constraints.silhouetteConstraints(A_8U, is_flat=True)

        A_L = amg_constraints.laplacianMatrix((h, w), num_elements=3)
        A = A_c + A_L
        b = b_c

        N = amg_solver.solve(A, b).reshape(-1, 3)
        N = computeNz(N)
        N = normalizeVectors(N)
        N_32F = N.reshape(h, w, 3)

        file_path = self.resultFile(self._data_file_name)
        saveNormal(file_path, N_32F, A_8U)
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.4, 0.7]))

        C0_32F = LambertShader().diffuseShading(L, N0_32F)
        I_32F = luminance(C0_32F)

        br_field = BrightnessField(I_32F, sigma=5.0)
        I_smooth_32F = br_field.smoothBrightness()
        dI = br_field.brightnessDifference()
        gx, gy = br_field.gradients()

        N_32F = br_field.field()

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 2
        num_cols = 4
        plot_grid = SubplotGrid(num_rows, num_cols)
        plot_grid.showImage(I_32F, r'$I$')
        plot_grid.showColorMap(dI, r'$dI$')
        plot_grid.showColorMap(gx, r'$gx$')
        plot_grid.showColorMap(gy, r'$gy$')

        plot_grid.showImage(normalToColor(N_32F, A_8U), r'$N$')
        plot_grid.showImage(N_32F[:, :, 2], r'$N_z$')

        showMaximize()
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        #C0_32F = ToonShader().diffuseShading(L, N0_32F)
        C0_32F = LambertShader().diffuseShading(L, N0_32F)
        I0_32F = luminance(C0_32F)

        I0_low_32F = cv2.resize(I0_32F, (256, 256))
        A_low_8U = cv2.resize(A_8U, I0_low_32F.shape)

        D_32F = depthFromGradient(I0_low_32F, A_low_8U)
        D_32F = cv2.resize(D_32F, I0_32F.shape)
        N_32F = depthToNormal(D_32F)

        self._view.setRGBAD(setAlpha(C0_32F, A_32F), D_32F)
Exemplo n.º 29
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        #N0_32F = cv2.resize(N0_32F, (64, 64))
        #A_8U = cv2.resize(A_8U, N0_32F.shape[:2])

        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        C0_32F = ToonShader().diffuseShading(L, N0_32F)
        # C0_32F = LambertShader().diffuseShading(L, N0_32F)

        sfs_method = Wu08SFS(L, C0_32F, A_8U)
        sfs_method.run()
        N_32F = sfs_method.normal()

        saveNormal(self.resultFile(self._data_file_name, result_name="Wu08"), N_32F, A_8U)
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        #C0_32F = ToonShader().diffuseShading(L, N0_32F)
        C0_32F = LambertShader().diffuseShading(L, N0_32F)
        I0_32F = luminance(C0_32F)

        I0_low_32F = cv2.resize(I0_32F, (256, 256))
        A_low_8U = cv2.resize(A_8U, I0_low_32F.shape)

        D_32F = depthFromGradient(I0_low_32F, A_low_8U)
        D_32F = cv2.resize(D_32F, I0_32F.shape)
        N_32F = depthToNormal(D_32F)

        self._view.setRGBAD(setAlpha(C0_32F, A_32F), D_32F)
def shapeList():

    num_rows = 1
    num_cols = len(shapeNames())

    w = 20
    h = w * num_rows / num_cols

    cmap_id = 10
    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.96,
                        bottom=0.04,
                        hspace=0.15,
                        wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    for shape_name in shapeNames():
        Ng_data = shapeFile(shape_name)
        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "")

    file_path = shapeResultFile("ShapeEstimation", "ShapeList")
    fig.savefig(file_path, transparent=True)
Exemplo n.º 32
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data

        #N0_32F = cv2.resize(N0_32F, (64, 64))
        #A_8U = cv2.resize(A_8U, N0_32F.shape[:2])

        A_32F = to32F(A_8U)

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))

        C0_32F = ToonShader().diffuseShading(L, N0_32F)
        # C0_32F = LambertShader().diffuseShading(L, N0_32F)

        sfs_method = Wu08SFS(L, C0_32F, A_8U)
        sfs_method.run()
        N_32F = sfs_method.normal()

        saveNormal(self.resultFile(self._data_file_name, result_name="Wu08"),
                   N_32F, A_8U)
def methodComparisonFigure(shape_name="ThreeBox", cmap_id=10):
    num_methods = 3
    num_rows = 3
    num_cols = 2 * num_methods + 1

    w = 20
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.04, hspace=0.15, wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data
    A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)

    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)
    C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

    toon_sfs = ToonSFS(L, C0_32F, A_8U)
    toon_sfs.setInitialNormal(N0_32F)
    toon_sfs.setNumIterations(iterations=100)
    toon_sfs.setWeights(w_lap=0.1)
    toon_sfs.run()

    N_toon = toon_sfs.normal()
    C_toon = toon_sfs.shading()

    C_lumo, N_lumo = lumoSFS(C0_32F, L, N0_32F, A_8U)
    C_wu, N_wu = wuSFS(C0_32F, L, N0_32F, A_8U)

    C_error_toon, N_error_toon, I_error_toon = computeErrors(L, C0_32F, C_toon, Ng_32F, N_toon, A_8U)
    C_error_lumo, N_error_lumo, I_error_lumo = computeErrors(L, C0_32F, C_lumo, Ng_32F, N_lumo, A_8U)
    C_error_wu, N_error_wu, I_error_wu = computeErrors(L, C0_32F, C_wu, Ng_32F, N_wu, A_8U)

    plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "Ground-truth")

    title = ""
    plot_grid.showImage(setAlpha(C_lumo, to32F(A_8U)), "Lumo")
    plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_wu, to32F(A_8U)), "Lambert assumption")
    plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_toon, to32F(A_8U)), "Our result")
    plot_grid.showColorMap(C_error_toon, title, v_min=0, v_max=0.1, with_colorbar=True)

    plot_grid.showImage(normalToColor(Ng_32F, A_8U), title)

    plot_grid.showImage(normalToColor(N_lumo, A_8U), title)
    plot_grid.showColorMap(N_error_lumo, title, v_min=0, v_max=50.0, with_colorbar=True)
    plot_grid.showImage(normalToColor(N_wu, A_8U), title)
    plot_grid.showColorMap(N_error_wu, title, v_min=0, v_max=50.0, with_colorbar=True)
    plot_grid.showImage(normalToColor(N_toon, A_8U), title)
    plot_grid.showColorMap(N_error_toon, title, v_min=0, v_max=50.0, with_colorbar=True)

    plot_grid.showImage(computeIllumination(L, Ng_32F, A_8U), title)

    plot_grid.showImage(computeIllumination(L, N_lumo, A_8U), title)
    plot_grid.showColorMap(I_error_lumo, title, v_min=0, v_max=0.2, with_colorbar=True)
    plot_grid.showImage(computeIllumination(L, N_wu, A_8U), title)
    plot_grid.showColorMap(I_error_wu, title, v_min=0, v_max=0.2, with_colorbar=True)
    plot_grid.showImage(computeIllumination(L, N_toon, A_8U), title)
    plot_grid.showColorMap(I_error_toon, title, v_min=0, v_max=0.2, with_colorbar=True)

    # showMaximize()
    file_path = shapeResultFile("ShapeEstimation", "Comparison_%s_%s" %(shape_name, cmap_id), file_ext=".png")
    fig.savefig(file_path, transparent=True)
Exemplo n.º 34
0
def overviewFigure():
    cmap_id = 10
    colormap_file = colorMapFile(cmap_id)

    num_rows = 1
    num_cols = 5

    w = 10
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.02, hspace=0.05, wspace=0.05)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    L = normalizeVector(np.array([-0.4, 0.6, 0.6]))
    L_img = lightSphere(L)

    shape_name = "ThreeBox"

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data

    M_32F = loadColorMap(colormap_file)
    Cg_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

    borders=[0.6, 0.8, 0.92]
    colors = [np.array([0.2, 0.2, 0.4]),
              np.array([0.3, 0.3, 0.6]),
              np.array([0.4, 0.4, 0.8]),
              np.array([0.5, 0.5, 1.0])]
    #Cg_32F = ToonShader(borders, colors).diffuseShading(L, Ng_32F)
    #Cg_32F = cv2.GaussianBlur(Cg_32F, (0,0), 2.0)

    sfs_method = ToonSFS(L, Cg_32F, A_8U)
    sfs_method.setInitialNormal(N0_32F)
    sfs_method.setNumIterations(iterations=40)
    sfs_method.setWeights(w_lap=10.0)
    sfs_method.run()

    N_32F = sfs_method.normal()
    I_32F = np.float32(np.clip(LdotN(L, N_32F), 0.0, 1.0))
    I0_32F = np.float32(np.clip(LdotN(L, N0_32F), 0.0, 1.0))
    C_32F = sfs_method.shading()
    C0_32F = sfs_method.initialShading()

    M_32F = sfs_method.colorMap().mapImage()

    L1 = normalizeVector(np.array([0.0, 0.6, 0.6]))
    L1_img = lightSphere(L1)
    C1_32F = sfs_method.relighting(L1)

    L2 = normalizeVector(np.array([0.5, 0.8, 0.6]))
    L2_img = lightSphere(L2)
    C2_32F = sfs_method.relighting(L2)

    N_sil = silhouetteNormal(A_8U, sigma=7.0)
    N_sil[:, :, 2]  = N_sil[:, :, 2] ** 10.0
    N_sil = normalizeImage(N_sil)
    A_sil = 1.0 - N_sil[:, :, 2]
    A_sil = to8U(A_sil)
    N_xy = N_sil[:, :, 0] ** 2 + N_sil[:, :, 1] ** 2
    A_sil[N_xy < 0.1] = 0

    title = ""
    plot_grid.showImage(setAlpha(Cg_32F, to32F(A_8U)), title)
    plot_grid.showImage(normalToColor(N0_32F, A_8U), title)
    plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), title)
    plot_grid.showImage(normalToColor(N_32F, A_8U), title)

    plot_grid.showImage(setAlpha(C_32F, to32F(A_8U)), title)
    # plot_grid.showImage(normalToColor(Ng_32F, A_8U), title)

    #showMaximize()
    file_path = shapeResultFile("Overview", "Overview")
    fig.savefig(file_path, transparent=True)

    file_path = shapeResultFile("Overview", "Cg")
    saveRGBA(file_path, setAlpha(Cg_32F, to32F(A_8U)))

    file_path = shapeResultFile("Overview", "L")
    saveRGB(file_path, gray2rgb(to8U(L_img)))

    file_path = shapeResultFile("Overview", "L1")
    saveRGB(file_path, gray2rgb(to8U(L1_img)))

    file_path = shapeResultFile("Overview", "L2")
    saveRGB(file_path, gray2rgb(to8U(L2_img)))

    file_path = shapeResultFile("Overview", "N0")
    saveNormal(file_path, N0_32F, A_8U)

    file_path = shapeResultFile("Overview", "N_sil")
    saveNormal(file_path, N_sil, A_sil)

    file_path = shapeResultFile("Overview", "N")
    saveNormal(file_path, N_32F, A_8U)

    file_path = shapeResultFile("Overview", "C0")
    saveRGBA(file_path, setAlpha(C0_32F, to32F(A_8U)))

    file_path = shapeResultFile("Overview", "C")
    saveRGBA(file_path, setAlpha(C_32F, to32F(A_8U)))

    file_path = shapeResultFile("Overview", "C1")
    saveRGBA(file_path, setAlpha(C1_32F, to32F(A_8U)))

    file_path = shapeResultFile("Overview", "C2")
    saveRGBA(file_path, setAlpha(C2_32F, to32F(A_8U)))

    file_path = shapeResultFile("Overview", "I")
    saveRGBA(file_path, setAlpha(gray2rgb(I_32F), to32F(A_8U)))

    file_path = shapeResultFile("Overview", "I0")
    saveRGBA(file_path, setAlpha(gray2rgb(I0_32F), to32F(A_8U)))

    file_path = shapeResultFile("Overview", "M")
    saveRGB(file_path, M_32F)
def materialShapeVariationFigure():
    target_colormaps = [23, 3, 12]
    #target_colormaps = [3, 17]
    colormap_files = [colorMapFile(cmap_id) for cmap_id in target_colormaps]

    target_shapes = ["Blob1", "ThreeBox"]
    shape_names = target_shapes
    #shape_names = shapeNames()[4:5]

    num_shapes = len(shape_names)
    num_colormaps = len(colormap_files)
    num_rows = num_colormaps
    num_cols = 6

    w = 20
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.02, hspace=0.15, wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    shape_name = "Blob1"

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data

    for mi, colormap_file in enumerate(colormap_files):
        M_32F = loadColorMap(colormap_file)
        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        sfs_method = ToonSFS(L, C0_32F, A_8U)
        sfs_method.setInitialNormal(N0_32F)
        sfs_method.setNumIterations(iterations=40)
        sfs_method.setWeights(w_lap=5.0)
        sfs_method.run()

        N_32F = sfs_method.normal()
        C_32F = sfs_method.shading()
            #C_32F = cv2.bilateralFilter(C_32F, 0, 0.1, 3)
        C_error = sfs_method.shadingError()

        C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        C_error = trim(C_error, A_8U)

        h, w = A_8U.shape
        N_error = angleErros(N_32F.reshape(-1, 3), Ng_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        N_error = trim(N_error, A_8U)

        title = ""

        if mi == 0:
            title =  "Ground-truth"
        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), title)

        title = ""
        if mi == 0:
            title =  "Our result"
        plot_grid.showImage(setAlpha(C_32F, to32F(A_8U)), title)

        title = ""
        if mi == 0:
            title = "Error (shading)"
        plot_grid.showColorMap(C_error, title, v_min=0, v_max=0.1, with_colorbar=True)

        title = ""
        if mi == 0:
            title =  "Ground-truth"
        plot_grid.showImage(normalToColor(Ng_32F, A_8U), title)

        title = ""
        if mi == 0:
            title = "Our result"
        plot_grid.showImage(normalToColor(N_32F, A_8U), title)

        title = ""
        if mi == 0:
            title = "Error (shape)"

        plot_grid.showColorMap(N_error, title, v_min=0, v_max=50.0, with_colorbar=True)

    #showMaximize()
    file_path = shapeResultFile("ShapeEstimation", "MaterialShapeEvaluation", file_ext=".pdf")
    fig.savefig(file_path, transparent=False)
Exemplo n.º 36
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        A_32F = to32F(A_8U)

        C0_32F = to32F(rgb(C0_8U))
        I0_32F = luminance(C0_32F)

        initial_normals = ["N_lumo.png", "N0_d.png"]

        layer_name = os.path.splitext(os.path.basename(layer_file))[0]

        for initial_normal in initial_normals:
            N0_32F, AN_8U = loadNormal(self.characterResultFile(initial_normal, data_name="BaseDetailSepration"))
            N_32F, L, C_32F, M = self._runSFS(C0_32F, A_8U, N0_32F, AN_8U)

            L_img = lightSphere(L)

            M_img = M.mapImage()

            fig, axes = plt.subplots(figsize=(11, 5))
            font_size = 15
            fig.subplots_adjust(left=0.02, right=0.98, top=0.9, hspace=0.12, wspace=0.02)
            fig.suptitle(self.name(), fontsize=font_size)

            num_rows = 1
            num_cols = 4
            plot_grid = SubplotGrid(num_rows, num_cols)

            plot_grid.showImage(normalToColor(N0_32F, A_8U), r'Initial Normal: $N_0$')
            plot_grid.showImage(normalToColor(N_32F, A_8U), r'Estimated Normal: $N$')
            plot_grid.showImage(C0_8U, r'Shading: $C_0$')
            plot_grid.showImage(setAlpha(C_32F, A_32F), r'Recovered Shading: $C$')

            out_file_path = self.characterResultFile("ToonSFS" + initial_normal, layer_name=layer_name)
            plt.savefig(out_file_path)

            N_trim = trim(N_32F, A_8U)
            N0_trim = trim(N0_32F, A_8U)
            C0_trim = trim(C0_32F, A_8U)
            A_trim = trim(A_8U, A_8U)


            out_file_path = self.characterResultFile(initial_normal, layer_name=layer_name)
            saveNormal(out_file_path, N_trim, A_trim)

            images = self._relightingImages(N_trim, A_trim, M)

            initial_normal_name = os.path.splitext(initial_normal)[0]
            video_name = "Relighting" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name, layer_name=layer_name)
            saveVideo(out_file_path, images)

            images = self._relightingOffsetImages(L, C0_trim, N0_trim, A_trim, M)
            video_name = "RelightingOffset" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name, layer_name=layer_name)
            saveVideo(out_file_path, images)
def methodComparisonFigure(shape_name="ThreeBox", cmap_id=10):
    num_methods = 3
    num_rows = 3
    num_cols = 2 * num_methods + 1

    w = 20
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.96,
                        bottom=0.04,
                        hspace=0.15,
                        wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal",
                              data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data
    A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)

    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)
    C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

    toon_sfs = ToonSFS(L, C0_32F, A_8U)
    toon_sfs.setInitialNormal(N0_32F)
    toon_sfs.setNumIterations(iterations=100)
    toon_sfs.setWeights(w_lap=0.1)
    toon_sfs.run()

    N_toon = toon_sfs.normal()
    C_toon = toon_sfs.shading()

    C_lumo, N_lumo = lumoSFS(C0_32F, L, N0_32F, A_8U)
    C_wu, N_wu = wuSFS(C0_32F, L, N0_32F, A_8U)

    C_error_toon, N_error_toon, I_error_toon = computeErrors(
        L, C0_32F, C_toon, Ng_32F, N_toon, A_8U)
    C_error_lumo, N_error_lumo, I_error_lumo = computeErrors(
        L, C0_32F, C_lumo, Ng_32F, N_lumo, A_8U)
    C_error_wu, N_error_wu, I_error_wu = computeErrors(L, C0_32F, C_wu, Ng_32F,
                                                       N_wu, A_8U)

    plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "Ground-truth")

    title = ""
    plot_grid.showImage(setAlpha(C_lumo, to32F(A_8U)), "Lumo")
    plot_grid.showColorMap(C_error_lumo,
                           title,
                           v_min=0,
                           v_max=0.1,
                           with_colorbar=True)
    plot_grid.showImage(setAlpha(C_wu, to32F(A_8U)), "Lambert assumption")
    plot_grid.showColorMap(C_error_wu,
                           title,
                           v_min=0,
                           v_max=0.1,
                           with_colorbar=True)
    plot_grid.showImage(setAlpha(C_toon, to32F(A_8U)), "Our result")
    plot_grid.showColorMap(C_error_toon,
                           title,
                           v_min=0,
                           v_max=0.1,
                           with_colorbar=True)

    plot_grid.showImage(normalToColor(Ng_32F, A_8U), title)

    plot_grid.showImage(normalToColor(N_lumo, A_8U), title)
    plot_grid.showColorMap(N_error_lumo,
                           title,
                           v_min=0,
                           v_max=50.0,
                           with_colorbar=True)
    plot_grid.showImage(normalToColor(N_wu, A_8U), title)
    plot_grid.showColorMap(N_error_wu,
                           title,
                           v_min=0,
                           v_max=50.0,
                           with_colorbar=True)
    plot_grid.showImage(normalToColor(N_toon, A_8U), title)
    plot_grid.showColorMap(N_error_toon,
                           title,
                           v_min=0,
                           v_max=50.0,
                           with_colorbar=True)

    plot_grid.showImage(computeIllumination(L, Ng_32F, A_8U), title)

    plot_grid.showImage(computeIllumination(L, N_lumo, A_8U), title)
    plot_grid.showColorMap(I_error_lumo,
                           title,
                           v_min=0,
                           v_max=0.2,
                           with_colorbar=True)
    plot_grid.showImage(computeIllumination(L, N_wu, A_8U), title)
    plot_grid.showColorMap(I_error_wu,
                           title,
                           v_min=0,
                           v_max=0.2,
                           with_colorbar=True)
    plot_grid.showImage(computeIllumination(L, N_toon, A_8U), title)
    plot_grid.showColorMap(I_error_toon,
                           title,
                           v_min=0,
                           v_max=0.2,
                           with_colorbar=True)

    # showMaximize()
    file_path = shapeResultFile("ShapeEstimation",
                                "Comparison_%s_%s" % (shape_name, cmap_id),
                                file_ext=".png")
    fig.savefig(file_path, transparent=True)
def lightEstimationFigure():
    target_colormaps = [23, 0, 6, 22, 4, 12]
    target_shapes = ["Raptor", "Man", "Blob1", "OctaFlower", "Pulley", "Cone"]
    colormap_files = [colorMapFile(cmap_id) for cmap_id in target_colormaps]
    shape_names = target_shapes

    num_rows = len(shape_names) + 1
    num_cols = len(colormap_files) + 1

    w = 10
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.98, bottom=0.02, hspace=0.05, wspace=0.05)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    Lg = normalizeVector(np.array([-0.2, 0.3, 0.6]))
    Lg_img = lightSphere(Lg)

    plot_grid.showImage(Lg_img, "")

    Ms = []

    for colormap_file in colormap_files:
        M_32F = loadColorMap(colormap_file)
        Cs_32F = colorMapSphere(Lg, M_32F)

        plot_grid.showImage(Cs_32F, "")

        Ms.append(M_32F)

    L_errors = np.zeros((num_rows, num_cols))

    for si, shape_name in enumerate(shape_names):
        Ng_data = shapeFile(shape_name)

        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
        N0_data = loadNormal(N0_file)
        N0_32F, A_8U = N0_data

        plot_grid.showImage(normalToColor(Ng_32F, A_8U), "")

        for mi, M_32F in enumerate(Ms):
            C0_32F = ColorMapShader(M_32F).diffuseShading(Lg, Ng_32F)
            I_32F = luminance(C0_32F)

            L = lightEstimation(I_32F, N0_32F, A_8U)
            L_errors[si, mi] = angleError(Lg, L)

            L_img = lightSphereColorMap(L, v=L_errors[si, mi], v_min=0, v_max=40)

            plot_grid.showImage(L_img, "")

    L_error_min, L_error_max = np.min(L_errors), np.max(L_errors)

    file_path = shapeResultFile("LightEstimation", "LightEstimationError")
    fig.savefig(file_path, transparent=True)
def shapeErrorTable():
    cmap_id = 10
    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    num_shapes = len(shapeNames())
    C_errors = np.zeros((num_shapes, 3))
    N_errors = np.zeros((num_shapes, 3))
    I_errors = np.zeros((num_shapes, 3))

    for si, shape_name in enumerate(shapeNames()):
        Ng_data = shapeFile(shape_name)
        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        N0_file = shapeResultFile(result_name="InitialNormal",
                                  data_name=shape_name)
        N0_data = loadNormal(N0_file)
        N0_32F, A_8U = N0_data
        A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)
        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        toon_sfs = ToonSFS(L, C0_32F, A_8U)
        toon_sfs.setInitialNormal(N0_32F)
        toon_sfs.setNumIterations(iterations=20)
        toon_sfs.setWeights(w_lap=9.0)
        toon_sfs.run()

        N_toon = toon_sfs.normal()
        C_toon = toon_sfs.shading()

        C_lumo, N_lumo = lumoSFS(C0_32F, L, N0_32F, A_8U)
        C_wu, N_wu = wuSFS(C0_32F, L, N0_32F, A_8U)

        C_error_toon, N_error_toon, I_error_toon = computeErrors(
            L, C0_32F, C_toon, Ng_32F, N_toon, A_8U)
        C_error_lumo, N_error_lumo, I_error_lumo = computeErrors(
            L, C0_32F, C_lumo, Ng_32F, N_lumo, A_8U)
        C_error_wu, N_error_wu, I_error_wu = computeErrors(
            L, C0_32F, C_wu, Ng_32F, N_wu, A_8U)

        C_errors[si, 0] = np.mean(C_error_toon)
        C_errors[si, 1] = np.mean(C_error_lumo)
        C_errors[si, 2] = np.mean(C_error_wu)

        I_errors[si, 0] = np.mean(I_error_toon)
        I_errors[si, 1] = np.mean(I_error_lumo)
        I_errors[si, 2] = np.mean(I_error_wu)

        N_errors[si, 0] = np.mean(N_error_toon)
        N_errors[si, 1] = np.mean(N_error_lumo)
        N_errors[si, 2] = np.mean(N_error_wu)

    file_path = shapeResultFile("ShapeEstimation",
                                "ShapeShadingError",
                                file_ext=".npy")
    np.save(file_path, C_errors)

    file_path = shapeResultFile("ShapeEstimation",
                                "ShapeNormalError",
                                file_ext=".npy")
    np.save(file_path, N_errors)

    file_path = shapeResultFile("ShapeEstimation",
                                "ShapeIlluminationErrorShape",
                                file_ext=".npy")
    np.save(file_path, I_errors)

    showShapeErrorTable()
def relightingFigure(shape_name="Vase", cmap_id=3):
    num_methods = 3
    num_lights = 2
    num_rows = num_lights + 1
    num_cols = num_methods + 2

    w = 15
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.04, hspace=0.15, wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    Lg = normalizeVector(np.array([-0.2, 0.3, 0.5]))
    Lg_img = lightSphere(Lg)

    L1 = normalizeVector(np.array([0.0, 0.7, 0.6]))
    L2 = normalizeVector(np.array([0.3, 0.5, 0.6]))


    # Ls = [normalizeVector(Lg * (1.0 - t) + t * L1) for t in np.linspace(0.0, 1.0, num_lights) ]
    Ls = [L1, L2]

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data
    A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)

    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)
    C0_32F = ColorMapShader(M_32F).diffuseShading(Lg, Ng_32F)

    toon_sfs = ToonSFS(Lg, C0_32F, A_8U)
    toon_sfs.setInitialNormal(N0_32F)
    toon_sfs.setNumIterations(iterations=50)
    toon_sfs.setWeights(w_lap=0.2)
    toon_sfs.run()

    N_toon = toon_sfs.normal()
    C_toon = toon_sfs.shading()

    C_lumo, N_lumo = lumoSFS(C0_32F, Lg, N0_32F, A_8U)
    C_wu, N_wu = wuSFS(C0_32F, Lg, N0_32F, A_8U)

    M_lumo = estimatedReflectance(C0_32F, Lg, N_lumo, A_8U)
    M_wu = estimatedReflectance(C0_32F, Lg, N_wu, A_8U)

    plot_grid.showImage(Lg_img, "Light direction")
    plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "Ground-truth")

    title = ""
    plot_grid.showImage(setAlpha(C_lumo, to32F(A_8U)), "Lumo")
    #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_wu, to32F(A_8U)), "Lambert assumption")
    #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_toon, to32F(A_8U)), "Our result")
    #plot_grid.showColorMap(C_error_toon, title, v_min=0, v_max=0.1, with_colorbar=True)

    for L in Ls:
        C1 = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        C1_lumo = M_lumo.shading(LdotN(L, N_lumo).flatten()).reshape(C0_32F.shape)
        C1_wu = M_wu.shading(LdotN(L, N_wu).flatten()).reshape(C0_32F.shape)
        C1_toon = toon_sfs.relighting(L)

        plot_grid.showImage(lightSphere(L), "")

        plot_grid.showImage(setAlpha(C1, to32F(A_8U)), "")

        title = ""
        plot_grid.showImage(setAlpha(C1_lumo, to32F(A_8U)), "")
    #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_wu, to32F(A_8U)), "")
    #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_toon, to32F(A_8U)), "")

    # showMaximize()
    file_path = shapeResultFile("Relighting", "RelightingComparison", file_ext=".png")
    fig.savefig(file_path, transparent=True)
def relightingVideo(shape_name="Ogre", cmap_id=3):
    num_methods = 3
    num_rows = 1
    num_cols = num_methods + 2

    num_lights = 120

    w = 10
    h = 5

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.96,
                        bottom=0.04,
                        hspace=0.15,
                        wspace=0.1)
    fig.suptitle("Shading Analysis", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    Lg = normalizeVector(np.array([-0.2, 0.3, 0.5]))
    Lg_img = lightSphere(Lg)

    L1 = normalizeVector(np.array([0.5, 0.5, 0.6]))

    Ls = [
        normalizeVector(Lg * (1.0 - t) + t * L1)
        for t in np.linspace(0.0, 1.0, num_lights)
    ]
    # Ls = [normalizeVector(Lg + 1.0 * np.cos(t) * np.array([1, 0, 0]) + 1.0 * np.sin(t) * np.array([0, 1, 0])) for t in np.linspace(0.0, 1.0, num_lights) ]

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal",
                              data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data
    A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)

    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)
    C0_32F = ColorMapShader(M_32F).diffuseShading(Lg, Ng_32F)

    toon_sfs = ToonSFS(Lg, C0_32F, A_8U)
    toon_sfs.setInitialNormal(N0_32F)
    toon_sfs.setNumIterations(iterations=100)
    toon_sfs.setWeights(w_lap=0.2)
    toon_sfs.run()

    N_toon = toon_sfs.normal()
    C_toon = toon_sfs.shading()

    C_lumo, N_lumo = lumoSFS(C0_32F, Lg, N0_32F, A_8U)
    C_wu, N_wu = wuSFS(C0_32F, Lg, N0_32F, A_8U)

    M_lumo = estimatedReflectance(C0_32F, Lg, N_lumo, A_8U)
    M_wu = estimatedReflectance(C0_32F, Lg, N_wu, A_8U)

    plot_grid.showImage(Lg_img, "Light direction")
    plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "Ground-truth")

    title = ""
    plot_grid.showImage(setAlpha(C_lumo, to32F(A_8U)), "Lumo")
    #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_wu, to32F(A_8U)), "Lambert assumption")
    #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_toon, to32F(A_8U)), "Our result")
    #plot_grid.showColorMap(C_error_toon, title, v_min=0, v_max=0.1, with_colorbar=True)

    images = []

    for i in xrange(48):
        images.append(figure2numpy(fig))

    for li, L in enumerate(Ls):
        print li
        fig.clear()
        fig.suptitle("Relighting", fontsize=font_size)
        plot_grid.setPlot(1, 1)

        C1 = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        C1_lumo = M_lumo.shading(LdotN(L,
                                       N_lumo).flatten()).reshape(C0_32F.shape)
        C1_wu = M_wu.shading(LdotN(L, N_wu).flatten()).reshape(C0_32F.shape)
        C1_toon = toon_sfs.relighting(L)

        plot_grid.showImage(lightSphere(L), "Light direction")

        plot_grid.showImage(setAlpha(C1, to32F(A_8U)), "Ground-truth")

        title = ""
        plot_grid.showImage(setAlpha(C1_lumo, to32F(A_8U)), "Lumo")
        #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_wu, to32F(A_8U)), "Lambert assumption")
        #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_toon, to32F(A_8U)), "Our result")

        images.append(figure2numpy(fig))

    file_path = shapeResultFile("Relighting",
                                "Relighting_%s_%s" % (shape_name, cmap_id),
                                file_ext=".wmv")
    saveVideo(file_path, images)
def relightingFigure(shape_name="Vase", cmap_id=3):
    num_methods = 3
    num_lights = 2
    num_rows = num_lights + 1
    num_cols = num_methods + 2

    w = 15
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.96,
                        bottom=0.04,
                        hspace=0.15,
                        wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    Lg = normalizeVector(np.array([-0.2, 0.3, 0.5]))
    Lg_img = lightSphere(Lg)

    L1 = normalizeVector(np.array([0.0, 0.7, 0.6]))
    L2 = normalizeVector(np.array([0.3, 0.5, 0.6]))

    # Ls = [normalizeVector(Lg * (1.0 - t) + t * L1) for t in np.linspace(0.0, 1.0, num_lights) ]
    Ls = [L1, L2]

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal",
                              data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data
    A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)

    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)
    C0_32F = ColorMapShader(M_32F).diffuseShading(Lg, Ng_32F)

    toon_sfs = ToonSFS(Lg, C0_32F, A_8U)
    toon_sfs.setInitialNormal(N0_32F)
    toon_sfs.setNumIterations(iterations=50)
    toon_sfs.setWeights(w_lap=0.2)
    toon_sfs.run()

    N_toon = toon_sfs.normal()
    C_toon = toon_sfs.shading()

    C_lumo, N_lumo = lumoSFS(C0_32F, Lg, N0_32F, A_8U)
    C_wu, N_wu = wuSFS(C0_32F, Lg, N0_32F, A_8U)

    M_lumo = estimatedReflectance(C0_32F, Lg, N_lumo, A_8U)
    M_wu = estimatedReflectance(C0_32F, Lg, N_wu, A_8U)

    plot_grid.showImage(Lg_img, "Light direction")
    plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "Ground-truth")

    title = ""
    plot_grid.showImage(setAlpha(C_lumo, to32F(A_8U)), "Lumo")
    #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_wu, to32F(A_8U)), "Lambert assumption")
    #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_toon, to32F(A_8U)), "Our result")
    #plot_grid.showColorMap(C_error_toon, title, v_min=0, v_max=0.1, with_colorbar=True)

    for L in Ls:
        C1 = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        C1_lumo = M_lumo.shading(LdotN(L,
                                       N_lumo).flatten()).reshape(C0_32F.shape)
        C1_wu = M_wu.shading(LdotN(L, N_wu).flatten()).reshape(C0_32F.shape)
        C1_toon = toon_sfs.relighting(L)

        plot_grid.showImage(lightSphere(L), "")

        plot_grid.showImage(setAlpha(C1, to32F(A_8U)), "")

        title = ""
        plot_grid.showImage(setAlpha(C1_lumo, to32F(A_8U)), "")
        #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_wu, to32F(A_8U)), "")
        #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_toon, to32F(A_8U)), "")

    # showMaximize()
    file_path = shapeResultFile("Relighting",
                                "RelightingComparison",
                                file_ext=".png")
    fig.savefig(file_path, transparent=True)
def materialShapeVariationFigure():
    target_colormaps = [23, 3, 12]
    #target_colormaps = [3, 17]
    colormap_files = [colorMapFile(cmap_id) for cmap_id in target_colormaps]

    target_shapes = ["Blob1", "ThreeBox"]
    shape_names = target_shapes
    #shape_names = shapeNames()[4:5]

    num_shapes = len(shape_names)
    num_colormaps = len(colormap_files)
    num_rows = num_colormaps
    num_cols = 6

    w = 20
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.96,
                        bottom=0.02,
                        hspace=0.15,
                        wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    L = normalizeVector(np.array([-0.4, 0.5, 0.6]))

    shape_name = "Blob1"

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal",
                              data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data

    for mi, colormap_file in enumerate(colormap_files):
        M_32F = loadColorMap(colormap_file)
        C0_32F = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        sfs_method = ToonSFS(L, C0_32F, A_8U)
        sfs_method.setInitialNormal(N0_32F)
        sfs_method.setNumIterations(iterations=40)
        sfs_method.setWeights(w_lap=5.0)
        sfs_method.run()

        N_32F = sfs_method.normal()
        C_32F = sfs_method.shading()
        #C_32F = cv2.bilateralFilter(C_32F, 0, 0.1, 3)
        C_error = sfs_method.shadingError()

        C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        C_error = trim(C_error, A_8U)

        h, w = A_8U.shape
        N_error = angleErros(N_32F.reshape(-1, 3),
                             Ng_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        N_error = trim(N_error, A_8U)

        title = ""

        if mi == 0:
            title = "Ground-truth"
        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), title)

        title = ""
        if mi == 0:
            title = "Our result"
        plot_grid.showImage(setAlpha(C_32F, to32F(A_8U)), title)

        title = ""
        if mi == 0:
            title = "Error (shading)"
        plot_grid.showColorMap(C_error,
                               title,
                               v_min=0,
                               v_max=0.1,
                               with_colorbar=True)

        title = ""
        if mi == 0:
            title = "Ground-truth"
        plot_grid.showImage(normalToColor(Ng_32F, A_8U), title)

        title = ""
        if mi == 0:
            title = "Our result"
        plot_grid.showImage(normalToColor(N_32F, A_8U), title)

        title = ""
        if mi == 0:
            title = "Error (shape)"

        plot_grid.showColorMap(N_error,
                               title,
                               v_min=0,
                               v_max=50.0,
                               with_colorbar=True)

    #showMaximize()
    file_path = shapeResultFile("ShapeEstimation",
                                "MaterialShapeEvaluation",
                                file_ext=".pdf")
    fig.savefig(file_path, transparent=False)
def LambertShadingFigure():
    target_shapes = [
        "Sphere", "Cone", "Blob1", "Man", "Cone", "OctaFlower", "Pulley",
        "Grog", "Lucy", "Raptor"
    ]
    target_shapes = ["Blob1", "Pulley", "Lucy"]
    shape_names = target_shapes

    num_rows = len(shape_names)
    num_cols = 6

    w = 20
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02,
                        right=0.98,
                        top=0.96,
                        bottom=0.02,
                        hspace=0.15,
                        wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    L = normalizeVector(np.array([-0.2, 0.3, 0.6]))

    for si, shape_name in enumerate(shape_names):
        Ng_data = shapeFile(shape_name)

        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        N0_file = shapeResultFile(result_name="InitialNormal",
                                  data_name=shape_name)
        N0_data = loadNormal(N0_file)
        N0_32F, A_8U = N0_data

        C0_32F = LambertShader().diffuseShading(L, Ng_32F)

        sfs_method = ToonSFS(L, C0_32F, A_8U)
        sfs_method.setInitialNormal(N0_32F)
        sfs_method.setNumIterations(iterations=70)
        sfs_method.setWeights(w_lap=1.0)
        sfs_method.run()
        N_32F = sfs_method.normal()
        C_32F = sfs_method.shading()
        C_error = sfs_method.shadingError()
        C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        C_error = trim(C_error, A_8U)

        h, w = A_8U.shape
        N_error = angleErros(N_32F.reshape(-1, 3),
                             Ng_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        N_error = trim(N_error, A_8U)

        title = ""
        if si == 0:
            title = "Ground-truth"
        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), title)

        title = ""
        if si == 0:
            title = "Our result"
        plot_grid.showImage(setAlpha(C_32F, to32F(A_8U)), title)

        title = ""
        if si == 0:
            title = "Error (shading)"
        plot_grid.showColorMap(C_error,
                               title,
                               v_min=0,
                               v_max=0.1,
                               with_colorbar=True)

        title = ""
        if si == 0:
            title = "Ground-truth"
        plot_grid.showImage(normalToColor(Ng_32F, A_8U), title)

        title = ""
        if si == 0:
            title = "Our result"
        plot_grid.showImage(normalToColor(N_32F, A_8U), title)

        title = ""
        if si == 0:
            title = "Error (shape)"

        plot_grid.showColorMap(N_error,
                               title,
                               v_min=0,
                               v_max=30.0,
                               with_colorbar=True)

    file_path = shapeResultFile("ShapeEstimation",
                                "LambertEstimationError",
                                file_ext=".pdf")
    fig.savefig(file_path, transparent=True)
def relightingVideo(shape_name="Ogre", cmap_id=3):
    num_methods = 3
    num_rows = 1
    num_cols = num_methods + 2

    num_lights = 120

    w = 10
    h = 5

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.04, hspace=0.15, wspace=0.1)
    fig.suptitle("Shading Analysis", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    Lg = normalizeVector(np.array([-0.2, 0.3, 0.5]))
    Lg_img = lightSphere(Lg)

    L1 = normalizeVector(np.array([0.5, 0.5, 0.6]))

    Ls = [normalizeVector(Lg * (1.0 - t) + t * L1) for t in np.linspace(0.0, 1.0, num_lights) ]
    # Ls = [normalizeVector(Lg + 1.0 * np.cos(t) * np.array([1, 0, 0]) + 1.0 * np.sin(t) * np.array([0, 1, 0])) for t in np.linspace(0.0, 1.0, num_lights) ]

    Ng_data = shapeFile(shape_name)
    Ng_data = loadNormal(Ng_data)
    Ng_32F, A_8U = Ng_data

    N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
    N0_data = loadNormal(N0_file)
    N0_32F, A_8U = N0_data
    A_8U = cv2.bilateralFilter(A_8U, 0, 5, 2)

    colormap_file = colorMapFile(cmap_id)
    M_32F = loadColorMap(colormap_file)
    C0_32F = ColorMapShader(M_32F).diffuseShading(Lg, Ng_32F)

    toon_sfs = ToonSFS(Lg, C0_32F, A_8U)
    toon_sfs.setInitialNormal(N0_32F)
    toon_sfs.setNumIterations(iterations=100)
    toon_sfs.setWeights(w_lap=0.2)
    toon_sfs.run()

    N_toon = toon_sfs.normal()
    C_toon = toon_sfs.shading()

    C_lumo, N_lumo = lumoSFS(C0_32F, Lg, N0_32F, A_8U)
    C_wu, N_wu = wuSFS(C0_32F, Lg, N0_32F, A_8U)

    M_lumo = estimatedReflectance(C0_32F, Lg, N_lumo, A_8U)
    M_wu = estimatedReflectance(C0_32F, Lg, N_wu, A_8U)

    plot_grid.showImage(Lg_img, "Light direction")
    plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), "Ground-truth")

    title = ""
    plot_grid.showImage(setAlpha(C_lumo, to32F(A_8U)), "Lumo")
    #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_wu, to32F(A_8U)), "Lambert assumption")
    #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
    plot_grid.showImage(setAlpha(C_toon, to32F(A_8U)), "Our result")
    #plot_grid.showColorMap(C_error_toon, title, v_min=0, v_max=0.1, with_colorbar=True)

    images = []

    for i in xrange(48):
        images.append(figure2numpy(fig))

    for li, L in enumerate(Ls):
        print li
        fig.clear()
        fig.suptitle("Relighting", fontsize=font_size)
        plot_grid.setPlot(1, 1)

        C1 = ColorMapShader(M_32F).diffuseShading(L, Ng_32F)

        C1_lumo = M_lumo.shading(LdotN(L, N_lumo).flatten()).reshape(C0_32F.shape)
        C1_wu = M_wu.shading(LdotN(L, N_wu).flatten()).reshape(C0_32F.shape)
        C1_toon = toon_sfs.relighting(L)

        plot_grid.showImage(lightSphere(L), "Light direction")

        plot_grid.showImage(setAlpha(C1, to32F(A_8U)), "Ground-truth")

        title = ""
        plot_grid.showImage(setAlpha(C1_lumo, to32F(A_8U)), "Lumo")
    #plot_grid.showColorMap(C_error_lumo, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_wu, to32F(A_8U)), "Lambert assumption")
    #plot_grid.showColorMap(C_error_wu, title, v_min=0, v_max=0.1, with_colorbar=True)
        plot_grid.showImage(setAlpha(C1_toon, to32F(A_8U)), "Our result")

        images.append(figure2numpy(fig))

    file_path = shapeResultFile("Relighting", "Relighting_%s_%s" %(shape_name, cmap_id), file_ext=".wmv")
    saveVideo(file_path, images)
Exemplo n.º 46
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)
        N0_32F = trim(N0_32F, A_8U)
        A_32F = trim(A_32F, A_8U)
        A_8U = trim(A_8U, A_8U)

        L = normalizeVector(np.array([0.5, -0.2, 0.7]))
        C0_32F = ToonShader().diffuseShading(L, N0_32F)
        I0_32F = luminance(C0_32F)

        I_min, I_max = np.min(I0_32F), np.max(I0_32F)
        I_scale = (I0_32F - I_min) / (I_max - I_min)
        I_L = cv2.Laplacian(cv2.GaussianBlur(I_scale, (0, 0), 31.0), cv2.CV_32F, ksize=1)

        I_L_avg = np.average(np.abs(I_L))

        Ix = cv2.Sobel(I0_32F, cv2.CV_64F, 1, 0, ksize=1)
        Ix = cv2.GaussianBlur(Ix, (0, 0), 3.0)
        Ixx = cv2.Sobel(Ix, cv2.CV_64F, 1, 0, ksize=1)
        Ixx = cv2.GaussianBlur(Ixx, (0, 0), 5.0)
        Iy = -cv2.Sobel(I0_32F, cv2.CV_64F, 0, 1, ksize=1)
        Iy = cv2.GaussianBlur(Iy, (0, 0), 3.0)
        Iyy = -cv2.Sobel(Iy, cv2.CV_64F, 0, 1, ksize=1)
        Iyy = cv2.GaussianBlur(Iyy, (0, 0), 5.0)

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 2
        num_cols = 5
        plot_grid = SubplotGrid(num_rows, num_cols)

        Nx = cv2.Sobel(N0_32F[:, :, 0], cv2.CV_64F, 1, 0, ksize=1)
        Nx = cv2.GaussianBlur(Nx, (0, 0), 3.0)
        Nxx = cv2.Sobel(Nx, cv2.CV_64F, 1, 0, ksize=1)
        Nxx = cv2.GaussianBlur(Nxx, (0, 0), 5.0)
        Ny = -cv2.Sobel(N0_32F[:, :, 1], cv2.CV_64F, 0, 1, ksize=1)
        Ny = cv2.GaussianBlur(Ny, (0, 0), 3.0)
        Nyy = -cv2.Sobel(Ny, cv2.CV_64F, 0, 1, ksize=1)
        Nyy = cv2.GaussianBlur(Nyy, (0, 0), 5.0)
        Nz_L = cv2.Laplacian(cv2.GaussianBlur(N0_32F[:, :, 2], (0, 0), 5.0), cv2.CV_32F, ksize=5)

        Nz_L_avg = np.average(np.abs(Nz_L))

        Nz_L *= 1.0 / Nz_L_avg

        I_L *= 1.0 / I_L_avg

        print I_L_avg, Nz_L_avg

        Nz_L = np.clip(Nz_L, -5.0, 5.0)
        I_L = np.clip(I_L, -5.0, 5.0)

        plot_grid.showColorMap(N0_32F[:, :, 0], r'$N_{x}$', v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(N0_32F[:, :, 1], r'$N_{y}$', v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(Nx, r'$N_{xx}$', v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(Ny, r'$N_{yy}$', v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(Nz_L, r'$Nz_L$')
        #plot_grid.showColorMap(Nx + Ny, r'$N_{xx} + N_{yy}$', v_min=-0.01, v_max=0.01)

#         Ixx[Ixx>0] = 1.0
#         Ixx[Ixx<0] = -1.0
#         Iyy[Iyy>0] = 1.0
#         Iyy[Iyy<0] = -1.0
        plot_grid.showColorMap(-Ix, r'$I_{x}$', v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(-Iy, r'$I_{y}$', v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(-Ixx, r'$I_{xx}$', v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(-Iyy, r'$I_{yy}$', v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(I_L, r'$I_L$')
        #plot_grid.showColorMap(-Ixx - Iyy, r'$I_{xx} + I_{yy}$', v_min=-0.01, v_max=0.01)
        #plot_grid.showColorMap(Iy, r'$I_{y}$')

        showMaximize()
def LambertShadingFigure():
    target_shapes = ["Sphere", "Cone", "Blob1", "Man", "Cone", "OctaFlower", "Pulley", "Grog", "Lucy", "Raptor"]
    target_shapes = ["Blob1", "Pulley", "Lucy"]
    shape_names = target_shapes

    num_rows = len(shape_names)
    num_cols = 6

    w = 20
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.96, bottom=0.02, hspace=0.15, wspace=0.1)
    fig.suptitle("", fontsize=font_size)

    plot_grid = SubplotGrid(num_rows, num_cols)

    L = normalizeVector(np.array([-0.2, 0.3, 0.6]))

    for si, shape_name in enumerate(shape_names):
        Ng_data = shapeFile(shape_name)

        Ng_data = loadNormal(Ng_data)
        Ng_32F, A_8U = Ng_data

        N0_file = shapeResultFile(result_name="InitialNormal", data_name=shape_name)
        N0_data = loadNormal(N0_file)
        N0_32F, A_8U = N0_data

        C0_32F = LambertShader().diffuseShading(L, Ng_32F)

        sfs_method = ToonSFS(L, C0_32F, A_8U)
        sfs_method.setInitialNormal(N0_32F)
        sfs_method.setNumIterations(iterations=70)
        sfs_method.setWeights(w_lap=1.0)
        sfs_method.run()
        N_32F = sfs_method.normal()
        C_32F = sfs_method.shading()
        C_error = sfs_method.shadingError()
        C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        C_error = trim(C_error, A_8U)

        h, w = A_8U.shape
        N_error = angleErros(N_32F.reshape(-1, 3), Ng_32F.reshape(-1, 3)).reshape(h, w)
        N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
        N_error = trim(N_error, A_8U)

        title = ""
        if si == 0:
            title =  "Ground-truth"
        plot_grid.showImage(setAlpha(C0_32F, to32F(A_8U)), title)

        title = ""
        if si == 0:
            title =  "Our result"
        plot_grid.showImage(setAlpha(C_32F, to32F(A_8U)), title)

        title = ""
        if si == 0:
            title = "Error (shading)"
        plot_grid.showColorMap(C_error, title, v_min=0, v_max=0.1, with_colorbar=True)

        title = ""
        if si == 0:
            title =  "Ground-truth"
        plot_grid.showImage(normalToColor(Ng_32F, A_8U), title)

        title = ""
        if si == 0:
            title = "Our result"
        plot_grid.showImage(normalToColor(N_32F, A_8U), title)

        title = ""
        if si == 0:
            title = "Error (shape)"

        plot_grid.showColorMap(N_error, title, v_min=0, v_max=30.0, with_colorbar=True)

    file_path = shapeResultFile("ShapeEstimation", "LambertEstimationError", file_ext=".pdf")
    fig.savefig(file_path, transparent=True)
Exemplo n.º 48
0
    def _runImp(self):
        normal_data = loadNormal(self._data_file)

        if normal_data is None:
            return

        N0_32F, A_8U = normal_data
        A_32F = to32F(A_8U)
        N0_32F = trim(N0_32F, A_8U)
        A_32F = trim(A_32F, A_8U)
        A_8U = trim(A_8U, A_8U)

        L = normalizeVector(np.array([0.5, -0.2, 0.7]))
        C0_32F = ToonShader().diffuseShading(L, N0_32F)
        I0_32F = luminance(C0_32F)

        I_min, I_max = np.min(I0_32F), np.max(I0_32F)
        I_scale = (I0_32F - I_min) / (I_max - I_min)
        I_L = cv2.Laplacian(cv2.GaussianBlur(I_scale, (0, 0), 31.0), cv2.CV_32F, ksize=1)

        I_L_avg = np.average(np.abs(I_L))

        Ix = cv2.Sobel(I0_32F, cv2.CV_64F, 1, 0, ksize=1)
        Ix = cv2.GaussianBlur(Ix, (0, 0), 3.0)
        Ixx = cv2.Sobel(Ix, cv2.CV_64F, 1, 0, ksize=1)
        Ixx = cv2.GaussianBlur(Ixx, (0, 0), 5.0)
        Iy = -cv2.Sobel(I0_32F, cv2.CV_64F, 0, 1, ksize=1)
        Iy = cv2.GaussianBlur(Iy, (0, 0), 3.0)
        Iyy = -cv2.Sobel(Iy, cv2.CV_64F, 0, 1, ksize=1)
        Iyy = cv2.GaussianBlur(Iyy, (0, 0), 5.0)

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 2
        num_cols = 5
        plot_grid = SubplotGrid(num_rows, num_cols)

        Nx = cv2.Sobel(N0_32F[:, :, 0], cv2.CV_64F, 1, 0, ksize=1)
        Nx = cv2.GaussianBlur(Nx, (0, 0), 3.0)
        Nxx = cv2.Sobel(Nx, cv2.CV_64F, 1, 0, ksize=1)
        Nxx = cv2.GaussianBlur(Nxx, (0, 0), 5.0)
        Ny = -cv2.Sobel(N0_32F[:, :, 1], cv2.CV_64F, 0, 1, ksize=1)
        Ny = cv2.GaussianBlur(Ny, (0, 0), 3.0)
        Nyy = -cv2.Sobel(Ny, cv2.CV_64F, 0, 1, ksize=1)
        Nyy = cv2.GaussianBlur(Nyy, (0, 0), 5.0)
        Nz_L = cv2.Laplacian(cv2.GaussianBlur(N0_32F[:, :, 2], (0, 0), 5.0), cv2.CV_32F, ksize=5)

        Nz_L_avg = np.average(np.abs(Nz_L))

        Nz_L *= 1.0 / Nz_L_avg

        I_L *= 1.0 / I_L_avg

        print I_L_avg, Nz_L_avg

        Nz_L = np.clip(Nz_L, -5.0, 5.0)
        I_L = np.clip(I_L, -5.0, 5.0)

        plot_grid.showColorMap(N0_32F[:, :, 0], r"$N_{x}$", v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(N0_32F[:, :, 1], r"$N_{y}$", v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(Nx, r"$N_{xx}$", v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(Ny, r"$N_{yy}$", v_min=-0.01, v_max=0.01)
        plot_grid.showColorMap(Nz_L, r"$Nz_L$")
        # plot_grid.showColorMap(Nx + Ny, r'$N_{xx} + N_{yy}$', v_min=-0.01, v_max=0.01)

        #         Ixx[Ixx>0] = 1.0
        #         Ixx[Ixx<0] = -1.0
        #         Iyy[Iyy>0] = 1.0
        #         Iyy[Iyy<0] = -1.0
        plot_grid.showColorMap(-Ix, r"$I_{x}$", v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(-Iy, r"$I_{y}$", v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(-Ixx, r"$I_{xx}$", v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(-Iyy, r"$I_{yy}$", v_min=-0.001, v_max=0.001)
        plot_grid.showColorMap(I_L, r"$I_L$")
        # plot_grid.showColorMap(-Ixx - Iyy, r'$I_{xx} + I_{yy}$', v_min=-0.01, v_max=0.01)
        # plot_grid.showColorMap(Iy, r'$I_{y}$')

        showMaximize()
Exemplo n.º 49
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        A_32F = to32F(A_8U)

        C0_32F = to32F(rgb(C0_8U))
        I0_32F = luminance(C0_32F)

        initial_normals = ["N_lumo.png", "N0_d.png"]

        layer_name = os.path.splitext(os.path.basename(layer_file))[0]

        for initial_normal in initial_normals:
            N0_32F, AN_8U = loadNormal(
                self.characterResultFile(initial_normal,
                                         data_name="BaseDetailSepration"))
            N_32F, L, C_32F, M = self._runSFS(C0_32F, A_8U, N0_32F, AN_8U)

            L_img = lightSphere(L)

            M_img = M.mapImage()

            fig, axes = plt.subplots(figsize=(11, 5))
            font_size = 15
            fig.subplots_adjust(left=0.02,
                                right=0.98,
                                top=0.9,
                                hspace=0.12,
                                wspace=0.02)
            fig.suptitle(self.name(), fontsize=font_size)

            num_rows = 1
            num_cols = 4
            plot_grid = SubplotGrid(num_rows, num_cols)

            plot_grid.showImage(normalToColor(N0_32F, A_8U),
                                r'Initial Normal: $N_0$')
            plot_grid.showImage(normalToColor(N_32F, A_8U),
                                r'Estimated Normal: $N$')
            plot_grid.showImage(C0_8U, r'Shading: $C_0$')
            plot_grid.showImage(setAlpha(C_32F, A_32F),
                                r'Recovered Shading: $C$')

            out_file_path = self.characterResultFile("ToonSFS" +
                                                     initial_normal,
                                                     layer_name=layer_name)
            plt.savefig(out_file_path)

            N_trim = trim(N_32F, A_8U)
            N0_trim = trim(N0_32F, A_8U)
            C0_trim = trim(C0_32F, A_8U)
            A_trim = trim(A_8U, A_8U)

            out_file_path = self.characterResultFile(initial_normal,
                                                     layer_name=layer_name)
            saveNormal(out_file_path, N_trim, A_trim)

            images = self._relightingImages(N_trim, A_trim, M)

            initial_normal_name = os.path.splitext(initial_normal)[0]
            video_name = "Relighting" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name,
                                                     layer_name=layer_name)
            saveVideo(out_file_path, images)

            images = self._relightingOffsetImages(L, C0_trim, N0_trim, A_trim,
                                                  M)
            video_name = "RelightingOffset" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name,
                                                     layer_name=layer_name)
            saveVideo(out_file_path, images)