def _runColorMap(self, colormap_file, Ng_32F, N0_32F, A_8U): M_32F = loadColorMap(colormap_file) L0 = normalizeVector(np.array([-0.2, 0.3, 0.6])) L0_img = lightSphere(L0) L0_txt = 0.01 * np.int32(100 * L0) C0_32F = ColorMapShader(M_32F).diffuseShading(L0, Ng_32F) I_32F = luminance(C0_32F) L = lightEstimation(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(setAlpha(C0_32F, A_8U), r'Input image: $\mathbf{c}$', font_size=font_size) plot_grid.showImage(normalToColor(N0_32F, A_8U), r'Initial normal: $\mathbf{N}_0$') plot_grid.showImage(L0_img, r'Ground trugh light: $L_g = (%s, %s, %s)$' %(L0_txt[0], L0_txt[1], L0_txt[2])) plot_grid.showImage(L_img, r'Estimated light: $L = (%s, %s, %s)$' %(L_txt[0], L_txt[1], L_txt[2])) showMaximize()
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 _runSFS(self, C0_32F, A_8U, N0_32F, AN_8U): I0_32F = luminance(C0_32F) L = lightEstimation(I0_32F, N0_32F, A_8U) LdN = LdotN(L, N0_32F) layer_area = A_8U > 0.5 * np.max(A_8U) Cs = C0_32F[layer_area].reshape(-1, 3) Is = LdN[layer_area].flatten() M = ColorMapEstimation(Cs, Is) I_const = M.illumination(Cs) I_32F = np.array(I0_32F) I_32F[layer_area] = I_const I_32F[A_8U < 0.5 * np.max(A_8U)] = np.min(I_const) toon_sfs = ToonSFS(L, C0_32F, A_8U) toon_sfs.setInitialNormal(N0_32F) toon_sfs.run() N_32F = toon_sfs.normal() LdN_recovered = LdotN(L, N_32F) LdN_recovered[A_8U < 0.5 * np.max(A_8U)] = np.min(LdN_recovered) Is = LdN_recovered[layer_area].flatten() Cs_recovered = M.shading(Is) C_32F = np.array(C0_32F) C_32F[layer_area, :] = Cs_recovered return N_32F, L, C_32F, M
def _computeBaseDetalSeparation(self): sigma_space, sigma_range = self._parameters["sigmaSpace"], self._parameters["sigmaRange"] C0_32F = self._C0_32F I_32F = luminance(C0_32F) B_b, D_b = baseDetailSeparationBilateral(I_32F, sigma_space=sigma_space.value(), sigma_range=sigma_range.value()) self._D = D_b self._N_b = bumpNormal(B_b, scale=1.0, sigma=1.0) self._N_d = bumpNormal(D_b, scale=1.0, sigma=1.0) self._view.render(D_b)
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) Lab_32F = rgb2Lab(C0_32F) th_specular = 0.2 th_contour = 0.02 th_material = 0.1 E_32F = DoG(I_32F, sigma=2.0) contour = th_contour * np.min(E_32F) - E_32F contour *= 1.0 / np.max(contour) contour = np.clip(contour, 0.0, 1.0) specular = E_32F - th_specular * np.max(E_32F) specular *= 1.0 / np.max(specular) specular = np.clip(specular, 0.0, 1.0) material = rgb(C0_8U) # edge_mask = np.zeros(I_32F.shape, dtype=np.uint8) # edge_mask[contour > 0.0] = 1.0 # material = cv2.inpaint(material, edge_mask, 3, cv2.INPAINT_TELEA) for i in xrange(1): material = cv2.medianBlur(material, ksize=7) # material = th_material * np.max(np.abs(E_32F)) - np.abs(E_32F) # material *= 1.0 / np.max(material) # material = np.clip(material, 0.0, 1.0) # material[material > 0.0] = 1.0 E_32F[E_32F < 0.0] = 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 = 1 num_cols = 4 plot_grid = SubplotGrid(num_rows, num_cols) plot_grid.showImage(C0_8U, r'$C$') #plot_grid.showImage(setAlpha(C0_32F, material), r'$Material$') plot_grid.showImage(setAlpha(material, A_8U), r'$Material$') plot_grid.showImage(setAlpha(C0_32F, contour), r'$Contour$') plot_grid.showImage(setAlpha(C0_32F, specular), r'$Specular$') showMaximize()
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 __init__(self, C_32F, I_32F=None): self._C_32F = C_32F I0_32F = luminance(C_32F) self._I0_32F = I0_32F if I_32F is None: I_32F = np.array(I0_32F) self._I_32F = I_32F self._compute()
def _computeBaseDetalSeparation(self): sigma_space, sigma_range = self._parameters[ "sigmaSpace"], self._parameters["sigmaRange"] C0_32F = self._C0_32F I_32F = luminance(C0_32F) B_b, D_b = baseDetailSeparationBilateral( I_32F, sigma_space=sigma_space.value(), sigma_range=sigma_range.value()) self._D = D_b self._N_b = bumpNormal(B_b, scale=1.0, sigma=1.0) self._N_d = bumpNormal(D_b, scale=1.0, sigma=1.0) self._view.render(D_b)
def errorTable(): colormap_files = colorMapFiles() num_colormap_files = len(colormap_files) M_errors = np.zeros((num_colormap_files)) for mi, colormap_file in enumerate(colormap_files): M_32F = loadColorMap(colormap_file) C_32F = M_32F.reshape(1, len(M_32F), 3) I_32F = np.linspace(0.0, 1.0, len(M_32F)) I_32F = I_32F.reshape(C_32F.shape[:2]) reflectance = LambertReflectanceEstimation(C_32F, I_32F) Ml = reflectance.shading(I_32F)[0, :, :] I0_32F = luminance(C_32F) IL_32F = luminance(Ml.reshape(1, -1, 3)) I_min, I_max = np.min(I0_32F), np.max(I0_32F) M_error = normVectors(M_32F - Ml) #M_errors[mi] = np.mean(M_error) / (I_max - I_min) # M_errors[mi] = computeGradientDistance(M_32F, Ml) / (I_max - I_min) #M_errors[mi] = np.linalg.norm(I0_32F - IL_32F) / (I_max - I_min) M_errors[mi] = np.mean(M_error) / (I_max - I_min) # M_errors[mi] = np.linalg.norm(M_32F - Ml) / (I_max - I_min) # M_errors[mi] = compareHist(M_32F, Ml) file_path = shapeResultFile("ReflectanceEstimation", "ReflectanceError", file_ext=".npy") np.save(file_path, M_errors) plt.plot(M_errors) plt.show()
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): 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 _compute(self): C0_8U = self._image C0_32F = to32F(rgb(C0_8U)) Lab_32F = rgb2Lab(C0_32F) I_32F = luminance(C0_32F) h, w = I_32F.shape[:2] edge_mask = np.zeros((h, w), dtype=np.uint8) #E_32F = DoG(Lab_32F, sigma=7.0) # E_32F[E_32F > 0.0] = 0.0 # E_32F = - E_32F # E_32F /= np.max(E_32F) # th_contour = 0.05 # for ci in xrange(3): # edge_area = E_32F[:, :, ci] > th_contour # edge_mask[edge_area] = 255 E_32F = DoG(I_32F, sigma=3.0) h, w = E_32F.shape[:2] E_norm = -np.array(E_32F) E_norm[E_norm < 0.0] = 0.0 th_contour = 0.1 edge_area = E_norm > th_contour * np.max(E_norm) edge_mask[edge_area] = 255 self._edge_mask = edge_mask labels = np.array(edge_mask) mask = np.ones((h + 2, w + 2), dtype=np.uint8) mask[1:-1, 1:-1] = self._edge_mask for label in xrange(1, 3): regionSeeds = np.where(self._edge_mask == 0) if len(regionSeeds[0]) == 0: break p = (regionSeeds[1][0], regionSeeds[0][0]) cv2.floodFill(labels, mask, p, label) self._edge_mask[labels == label] = label self._labels = labels
def _compute(self): C0_8U = self._image C0_32F = to32F(rgb(C0_8U)) Lab_32F = rgb2Lab(C0_32F) I_32F = luminance(C0_32F) h, w = I_32F.shape[:2] edge_mask = np.zeros((h, w), dtype=np.uint8) # E_32F = DoG(Lab_32F, sigma=7.0) # E_32F[E_32F > 0.0] = 0.0 # E_32F = - E_32F # E_32F /= np.max(E_32F) # th_contour = 0.05 # for ci in xrange(3): # edge_area = E_32F[:, :, ci] > th_contour # edge_mask[edge_area] = 255 E_32F = DoG(I_32F, sigma=3.0) h, w = E_32F.shape[:2] E_norm = -np.array(E_32F) E_norm[E_norm < 0.0] = 0.0 th_contour = 0.1 edge_area = E_norm > th_contour * np.max(E_norm) edge_mask[edge_area] = 255 self._edge_mask = edge_mask labels = np.array(edge_mask) mask = np.ones((h + 2, w + 2), dtype=np.uint8) mask[1:-1, 1:-1] = self._edge_mask for label in xrange(1, 3): regionSeeds = np.where(self._edge_mask == 0) if len(regionSeeds[0]) == 0: break p = (regionSeeds[1][0], regionSeeds[0][0]) cv2.floodFill(labels, mask, p, label) self._edge_mask[labels == label] = label self._labels = labels
def _runColorMap(self, colormap_file, Ng_32F, N0_32F, A_8U): M_32F = loadColorMap(colormap_file) L0 = normalizeVector(np.array([-0.2, 0.3, 0.6])) L0_img = lightSphere(L0) L0_txt = 0.01 * np.int32(100 * L0) C0_32F = ColorMapShader(M_32F).diffuseShading(L0, Ng_32F) I_32F = luminance(C0_32F) L = lightEstimation(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(setAlpha(C0_32F, A_8U), r'Input image: $\mathbf{c}$', font_size=font_size) plot_grid.showImage(normalToColor(N0_32F, A_8U), r'Initial normal: $\mathbf{N}_0$') plot_grid.showImage( L0_img, r'Ground trugh light: $L_g = (%s, %s, %s)$' % (L0_txt[0], L0_txt[1], L0_txt[2])) plot_grid.showImage( L_img, r'Estimated light: $L = (%s, %s, %s)$' % (L_txt[0], L_txt[1], L_txt[2])) showMaximize()
def _interpolateNormalImage(self, N0_32F, W_32F, A_8U): constraints = [] constraints.append(image_constraints.laplacianConstraints(w_c=0.1)) constraints.append(image_constraints.normalConstraints(W_32F, N0_32F, w_c=3.0)) L = normalizeVector(np.array([-0.2, 0.3, 0.7])) I_32F = luminance(to32F(rgb(self._image))) I_min, I_max = np.min(I_32F), np.max(I_32F) I_32F = (I_32F - I_min) / (I_max - I_min) # constraints.append(image_constraints.brightnessConstraints(L, I_32F, w_c=0.5)) constraints.append(image_constraints.silhouetteConstraints(A_8U, w_c=0.8)) solver_iter = image_solver.solveIterator(constraints, [postNormalize(th=0.0)]) N_32F = np.array(N0_32F) N_32F = image_solver.solveMG(N_32F, solver_iter, iterations=10) N_32F = image_constraints.NxyToNz(N_32F) return N_32F
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 _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 _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 _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 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 _computeBrightness(self, C_32F): return luminance(np.float32(C_32F))
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 _loadImage(self): C0_32F = self._C0_32F I_32F = luminance(C0_32F) self._view.render(I_32F) return