def _strokeEdited(self, stroke_sets): image = self._scene.image() C_32F = to32F(rgb(image)) for stroke_set in stroke_sets.strokeSets(): for stroke in stroke_set.strokes(): if stroke.empty(): continue mask = np.zeros(image.shape[:2], dtype=np.uint8) points = stroke.points() points = np.int32(points) brush_size = int(stroke.brushSize()) cv2.polylines(mask, [points], 0, 255, brush_size) Cs = C_32F[mask > 0, :] Is = np.arange(len(Cs), dtype=np.float32) Is = np.array(Is) Cs = np.array(Cs) M = ColorMapEstimation(Cs, Is, num_samples=1000) M_img = M.mapImage(image_size=(256, 32)) def plotFunc(): plt.imshow(M_img) self._matplot_view.drawPlots(plotFunc) saveImage(self._currentColorMapFile(), M_img) self._newColorMapID()
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 estimatedReflectance(C0_32F, L, N_32F, A_8U): LdN = LdotN(L, N_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) return M
def _optimize(self): L = self._L N0_32F = self._N0_32F C0_32F = self._C0_32F LdN = LdotN(L, N0_32F) A_8U = self._A_8U 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) self._Cini_32F = M.shading(LdN.flatten()).reshape(self._C0_32F.shape) I_recovered = M.illumination(Cs) I_32F = np.zeros(LdN.shape) I_32F[:] = np.min(I_recovered) I_32F[layer_area] = I_recovered I_dog = DoG(I_32F, sigma=2.0) sigma = 2.0 I_32F = np.float32(cv2.GaussianBlur(I_32F, (0, 0), sigma)) I_lap = -cv2.Laplacian(np.float32(I_32F), cv2.CV_32F, ksize=1) I_lap = np.abs(I_lap) I_lap_median = np.median(I_lap[layer_area]) print "I_lap_mean", np.mean(I_lap[layer_area]) print "I_lap_median", np.median(I_lap[layer_area]) sigma = 0.05 epsilon = 0.0 * I_lap_median w_min = 0.05 w_max = 1.0 W_32F = w_min + (w_max - w_min) * (1.0 - np.exp(- (I_lap - epsilon) ** 2 / (sigma ** 2))) W_32F = w_min * np.ones(I_32F.shape[:2]) W_32F[I_lap < epsilon] = w_min W_32F[I_lap > epsilon] = w_max W_32F[:, :] = 1.0 constraints = [] #W_32F = np.ones(N0_32F.shape[:2]) #constraints.append(image_constraints.normalConstraints(W_32F, N0_32F, w_c=0.05)) w_lap = self._w_lap constraints.append(image_constraints.laplacianConstraints(w_c=w_lap)) constraints.append(image_constraints.brightnessConstraints(L, I_32F, w_c=1.0)) constraints.append(image_constraints.gradientConstraints(L, I_32F, w_c=0.2)) w_sil = 0.4 * w_lap constraints.append(image_constraints.silhouetteConstraints(A_8U, w_c=w_sil)) N_32F = np.array(N0_32F, dtype=np.float64) solver_iter = image_solver.solveIterator(constraints, [image_constraints.postNormalize(th=0.0)]) #[image_constraints.postComputeNz()]) N_32F = image_solver.solveMG(N_32F, solver_iter, iterations=self._iterations) self._N_32F = N_32F LdN = LdotN(L, N_32F) I_32F[layer_area] = LdN[layer_area] self._C_32F = M.shading(I_32F.flatten()).reshape(self._C0_32F.shape) self._M = M