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