def computeInitialNormal(self): if self._N_lumo.shape != self._N_b_smooth.shape: return self._N0_b_32F = normalizeImage( bumpMapping(np.array(self._N_lumo), self._N_b_smooth)) self._N0_d_32F = normalizeImage( bumpMapping(np.array(self._N_lumo), self._N_d_smooth))
def _computeInitialDetailNormal(self): bump_scale = self._parameters["bumpScale"].value() self._N_b[:, :, :2] *= bump_scale self._N_b = normalizeImage(self._N_b, th=1.0) self._N_d[:, :, :2] *= bump_scale self._N_d = normalizeImage(self._N_d, th=1.0) self._view.render(normalToColor(self._N_b))
def computeGradientNormals(D_32F, sigma=1.0): h, w = D_32F.shape gx = cv2.Sobel(D_32F, cv2.CV_64F, 1, 0, ksize=1) gx = cv2.GaussianBlur(gx, (0, 0), sigma) gy = cv2.Sobel(D_32F, cv2.CV_64F, 0, 1, ksize=1) gy = cv2.GaussianBlur(gy, (0, 0), sigma) g_max = max(np.max(gx), np.max(gy)) g_scale = 100.0 / g_max T_32F = np.zeros((h, w, 3), dtype=np.float32) T_32F[:, :, 0] = 1.0 T_32F[:, :, 2] = g_scale * gx B_32F = np.zeros((h, w, 3), dtype=np.float32) B_32F[:, :, 1] = 1.0 B_32F[:, :, 2] = -g_scale * gy T_flat = T_32F.reshape(-1, 3) B_flat = B_32F.reshape(-1, 3) N_flat = np.cross(T_flat, B_flat) N_32F = N_flat.reshape(h, w, 3) N_32F = normalizeImage(N_32F) return N_32F
def depthToNormal(D_32F): h, w = D_32F.shape gx = cv2.Sobel(D_32F, cv2.CV_64F, 1, 0, ksize=1) gy = cv2.Sobel(D_32F, cv2.CV_64F, 0, 1, ksize=1) N_32F = np.zeros((h, w, 3), dtype=np.float32) N_32F[:, :, 0] = -gx N_32F[:, :, 1] = gy N_32F[:, :, 2] = 2 N_32F = normalizeImage(N_32F) return N_32F
def preProcess(N0_32F, A_8U): foreground = A_8U > 0.5 * np.max(A_8U) background = A_8U == 0 N_32F = np.array(N0_32F) N_32F[background, :] = np.array([0.0, 0.0, 1.0]) sigma = 5.0 for i in xrange(5): N_32F = cv2.GaussianBlur(N_32F, (0, 0), sigma) N_32F[foreground, :] = N0_32F[foreground, :] #N_32F[background, :] = np.array([0.0, 0.0, 1.0]) #N_32F[background, :] = np.array([0.0, 0.0, 1.0]) N_32F = normalizeImage(N_32F) return N_32F
def bumpNormal(D_32F, scale=1.0, sigma=1.0): gx = cv2.Sobel(D_32F, cv2.CV_64F, 1, 0, ksize=1) gx = cv2.GaussianBlur(gx, (0, 0), sigma) gy = -cv2.Sobel(D_32F, cv2.CV_64F, 0, 1, ksize=1) gy = cv2.GaussianBlur(gy, (0, 0), sigma) h, w = D_32F.shape[:2] N_32F = np.zeros((h, w, 3), dtype=np.float32) N_32F[:, :, 0] = -scale * gx N_32F[:, :, 1] = -scale * gy N_32F[:, :, 2] = 1.0 N_32F = normalizeImage(N_32F, th=1.0) return N_32F
def _interpolateNormalAMG(self, N0_32F, W_32F, A_8U): h, w = N0_32F.shape[:2] A_c, b_c = normalConstraints(W_32F, N0_32F) A_8U = None if self._image.shape[2] == 4: A_8U = to8U(alpha(self._image)) A_sil, b_sil = silhouetteConstraints(A_8U) A_L = laplacianMatrix((h, w)) A = 10.0 * A_c + A_L + A_sil b = 10.0 * b_c + b_sil N_32F = amg_solver.solve(A, b).reshape(h, w, 3) N_32F = normalizeImage(N_32F) return N_32F
def normalSphere(h=256, w=256): N_32F = np.zeros((h, w, 3)) A_32F = np.zeros((h, w)) for y in xrange(h): N_32F[y, :, 0] = np.linspace(-1.05, 1.05, w) for x in xrange(w): N_32F[:, x, 1] = np.linspace(1.05, -1.05, w) r_xy = N_32F[:, :, 0]**2 + N_32F[:, :, 1]**2 N_32F[r_xy < 1.0, 2] = np.sqrt(1.0 - r_xy[r_xy < 1.0]) N_32F[r_xy > 1.0, 2] = 0.0 N_32F = normalizeImage(N_32F) A_32F[r_xy < 1.0] = 1.0 - r_xy[r_xy < 1.0]**100 A_32F = cv2.bilateralFilter(np.float32(A_32F), 0, 0.1, 2) return N_32F, A_32F
def _computeLumoNormal(self): A_8U = self._A_8U if A_8U is None: return h, w = A_8U.shape[:2] A_c, b_c = amg_constraints.silhouetteConstraints(A_8U) A_L = amg_constraints.laplacianMatrix((h, w)) A = 3.0 * A_c + A_L b = 3.0 * b_c N_32F = amg_solver.solve(A, b).reshape(h, w, 3) N_32F = computeNz(N_32F.reshape(-1, 3)).reshape(h, w, 3) N_32F = normalizeImage(N_32F) self._N_lumo = np.array(N_32F)
def _computeDetailNormal(self, N0_32F): h, w = N0_32F.shape[:2] W_32F = np.zeros((h, w)) # sigma_d = 2.0 * np.max(N0_32F[:, :, 2]) # W_32F = 1.0 - np.exp( - (N0_32F[:, :, 2] ** 2) / (sigma_d ** 2)) W_32F = 1.0 - N0_32F[:, :, 2] W_32F *= 1.0 / np.max(W_32F) W_32F = W_32F ** 1.5 A_c, b_c = amg_constraints.normalConstraints(W_32F, N0_32F) A_L = amg_constraints.laplacianMatrix((h, w)) lambda_d = 2.0 A = A_c + lambda_d * A_L b = b_c N_32F = amg_solver.solve(A, b).reshape(h, w, 3) N_32F = computeNz(N_32F.reshape(-1, 3)).reshape(h, w, 3) N_32F = normalizeImage(N_32F) return N_32F
def _computeDetailNormal(self, N0_32F): h, w = N0_32F.shape[:2] W_32F = np.zeros((h, w)) # sigma_d = 2.0 * np.max(N0_32F[:, :, 2]) # W_32F = 1.0 - np.exp( - (N0_32F[:, :, 2] ** 2) / (sigma_d ** 2)) W_32F = 1.0 - N0_32F[:, :, 2] W_32F *= 1.0 / np.max(W_32F) W_32F = W_32F**1.5 A_c, b_c = amg_constraints.normalConstraints(W_32F, N0_32F) A_L = amg_constraints.laplacianMatrix((h, w)) lambda_d = 2.0 A = A_c + lambda_d * A_L b = b_c N_32F = amg_solver.solve(A, b).reshape(h, w, 3) N_32F = computeNz(N_32F.reshape(-1, 3)).reshape(h, w, 3) N_32F = normalizeImage(N_32F) return N_32F
def bilateralNormalSmoothing(image, normal): sigma_xy = 1.0 xy = positionFeatures(image) / sigma_xy Lab = LabFeatures(image) foreground = foreGroundFeatures(image) N = normal[:, :, :3].reshape(-1, 3) LabxyN = np.concatenate((Lab, xy, N), axis=1)[foreground, :] sigma_L = 1.0 LabxyN[:, 0] = LabxyN[:, 0] / sigma_L LabxyN_sparse = shuffle(LabxyN, random_state=0)[:100] N_smooth = np.array(N) smooth = 10.0 f_x = np.vstack((LabxyN_sparse[:, :5].T, LabxyN_sparse[:, 5])) f_y = np.vstack((LabxyN_sparse[:, :5].T, LabxyN_sparse[:, 6])) f_z = np.vstack((LabxyN_sparse[:, :5].T, LabxyN_sparse[:, 7])) Nx_rbf = Rbf(*(f_x), function='linear', smooth=smooth) Ny_rbf = Rbf(*(f_y), function='linear', smooth=smooth) Nz_rbf = Rbf(*(f_z), function='linear', smooth=smooth) Labxy = LabxyN[:, :5] #Lab_smooth[:, 0] = L_rbf(Labxy[:, 0], Labxy[:, 1], Labxy[:, 2], Labxy[:, 3], Labxy[:, 4]) N_smooth[foreground, 0] = Nx_rbf(*(Labxy.T)) N_smooth[foreground, 1] = Ny_rbf(*(Labxy.T)) N_smooth[foreground, 2] = Nz_rbf(*(Labxy.T)) h, w = image.shape[:2] N_smooth = N_smooth.reshape((h, w, 3)) N_smooth = normalizeImage(N_smooth) return N_smooth
def func(N): N = normalizeImage(N, th) return N
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 computeInitialNormal(self): if self._N_lumo.shape != self._N_b_smooth.shape: return self._N0_b_32F = normalizeImage(bumpMapping(np.array(self._N_lumo), self._N_b_smooth)) self._N0_d_32F = normalizeImage(bumpMapping(np.array(self._N_lumo), self._N_d_smooth))