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 _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 _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 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)