def DoM(img, sigma): ksize = 2 * int(sigma / 2) + 1 img_8U = to8U(img) M = cv2.medianBlur(img_8U, ksize=ksize) D = to32F(img_8U) - to32F(M) return D
def bilateralSmoothing(image): sigma_xy = 0.1 xy = positionFeatures(image) / sigma_xy Lab = LabFeatures(image) foreground = foreGroundFeatures(image) Labxy = np.concatenate((Lab, xy), axis=1)[foreground, :] sigma_L = 1.0 Labxy[:, 0] = Labxy[:, 0] / sigma_L Labxy_sparse = shuffle(Labxy, random_state=0)[:1000] Lab_smooth = np.array(Lab) smooth = 0.0 L_rbf = Rbf(Labxy_sparse[:, 0], Labxy_sparse[:, 1], Labxy_sparse[:, 2], Labxy_sparse[:, 3], Labxy_sparse[:, 4], sigma_L * Labxy_sparse[:, 0], function='linear', smooth=smooth) a_rbf = Rbf(Labxy_sparse[:, 0], Labxy_sparse[:, 1], Labxy_sparse[:, 2], Labxy_sparse[:, 3], Labxy_sparse[:, 4], Labxy_sparse[:, 1], function='linear', smooth=smooth) b_rbf = Rbf(Labxy_sparse[:, 0], Labxy_sparse[:, 1], Labxy_sparse[:, 2], Labxy_sparse[:, 3], Labxy_sparse[:, 4], Labxy_sparse[:, 2], function='linear', smooth=smooth) #Lab_smooth[:, 0] = L_rbf(Labxy[:, 0], Labxy[:, 1], Labxy[:, 2], Labxy[:, 3], Labxy[:, 4]) Lab_smooth[foreground, 0] = L_rbf(*(Labxy.T)) Lab_smooth[foreground, 1] = a_rbf(*(Labxy.T)) Lab_smooth[foreground, 2] = b_rbf(*(Labxy.T)) h, w = image.shape[:2] Lab_smooth = Lab_smooth.reshape((h, w, 3)) rgb_smooth = Lab2rgb(np.float32(Lab_smooth)) rgb_smooth_8U = to8U(rgb_smooth) rgb_smooth_8U = setAlpha(rgb_smooth_8U, alpha(image)) return rgb_smooth_8U
def computeHistogram(M): M_8U = to8U(M.reshape(1, -1, 3)) hist = cv2.calcHist([M_8U], [0, 1, 2], None, [32, 32, 32], [0, 256, 0, 256, 0, 256]) hist = cv2.normalize(hist).flatten() return hist.flatten()
def baseDetailSeparationMedian(I_32F, ksize=5): ksize = 2 * (ksize / 2) + 1 B = to32F(cv2.medianBlur(to8U(I_32F), ksize)) D = I_32F - B return B, D
def normalToColor(N_32F, A_8U=None): C_32F = 0.5 * N_32F + 0.5 C_8U = to8U(C_32F) if A_8U is not None: C_8U = setAlpha(C_8U, A_8U) return C_8U
def depthImage(self): D_min = np.min(self._depth) D_max = np.max(self._depth) D_32F = (self._depth - D_min) / (D_max - D_min) D_8U = to8U(D_32F) D_8U = gray2rgb(D_8U) A_8U = alpha(self._image) D_8U = setAlpha(D_8U, A_8U) return D_8U
def saveVideo(file_path, images, fps=30, size=None): if size is None: h, w = images[0].shape[:2] size = (w, h) print size fourcc = cv2.cv.CV_FOURCC('W', 'M', 'V', '2') writer = cv2.VideoWriter(file_path, fourcc, fps, size, True) for image in images: bgr = rgb2bgr(to8U(rgb(image))) writer.write(bgr) writer.release()
def keyPressEvent(self, e): if e.key() == Qt.Key_0: self._view.render(self._image) if e.key() == Qt.Key_1: if self._N_32F is None: self._interpolateNormal() A_8U = None if self._image.shape[2] == 4: A_8U = to8U(alpha(self._image)) self._view.render(normalToColor(self._N_32F, A_8U)) if e.key() == Qt.Key_2: self._interpolateNormal() if self._N_32F is None: self._interpolateNormal() A_8U = None if self._image.shape[2] == 4: A_8U = to8U(alpha(self._image)) self._view.render(normalToColor(self._N_32F, A_8U)) if e.key() == Qt.Key_Delete: self._normal_constraints.clear() self._view.update()
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 ndarrayToQImage(img): C_8U = to8U(img) if len(C_8U.shape) == 2: C_8U = gray2rgb(C_8U) if C_8U.shape[2] == 2: C_8U = rg_to_rgb(C_8U) if C_8U.shape[2] == 3: return rgb_to_Qrgb(C_8U) if C_8U.shape[2] == 4: return rgba_to_Qargb(C_8U) return QImage()
def _strokeEdited(self, stroke_sets): print "Graph Cut Command" if len(stroke_sets.strokeSets()) < 2: return fg_stroke_set, bg_stroke_set = stroke_sets.strokeSets()[:2] image = self._scene.image() image_rgb = to8U(rgb(image)) mask = np.zeros(image.shape[:2], dtype=np.uint8) mask.fill(cv2.GC_PR_BGD) for stroke_set, color in zip( [fg_stroke_set, bg_stroke_set], [int(cv2.GC_FGD), int(cv2.GC_BGD)]): for stroke in stroke_set.strokes(): if stroke.empty(): continue points = stroke.points() points = np.int32(points) brush_size = int(stroke.brushSize()) print color cv2.polylines(mask, [points], 0, color, brush_size) bgdModel = np.zeros((1, 65), np.float64) fgdModel = np.zeros((1, 65), np.float64) cv2.grabCut(image_rgb, mask, None, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK) fg_mask = np.zeros(image.shape[:2], dtype=np.uint8) fg_mask[(mask == int(cv2.GC_FGD)) | (mask == int(cv2.GC_PR_FGD))] = 255 bg_mask = np.zeros(image.shape[:2], dtype=np.uint8) bg_mask[(mask == int(cv2.GC_BGD)) | (mask == int(cv2.GC_PR_BGD))] = 255 layer_set = self._scene.layerSet() layer_set.clear() layer_set.addLayer( Layer(name="BG", color=(0.0, 0.0, 1.0, 0.4), mask=bg_mask)) layer_set.addLayer( Layer(name="FG", color=(1.0, 0.0, 0.0, 0.4), mask=fg_mask))
def sparseHistogramSampling(image, num_color_bin=24, num_xy_bins=64): h, w = image.shape[:2] Lab = LabFeatures(image) xy = positionFeatures(image) Labxy = np.concatenate((Lab, xy), axis=1) num_bins = [ num_color_bin, num_color_bin, num_color_bin, num_xy_bins, num_xy_bins ] num_color_bins = num_bins[:] num_color_bins.append(3) hist_bins = np.zeros(num_bins, dtype=np.float32) color_bins = np.zeros(num_color_bins, dtype=np.float32) f_min = np.min(Labxy, axis=0) f_max = np.max(Labxy, axis=0) num_bins = np.array(num_bins) f_ids = (num_bins - 1) * (Labxy - f_min) / (f_max - f_min) f_ids = np.int32(f_ids) hist_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]] += 1 color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]] += Lab[:, :] hist_positive = hist_bins > 0.0 print np.count_nonzero(hist_positive) for ci in xrange(3): color_bins[hist_positive, ci] /= hist_bins[hist_positive] map_Lab = color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]].reshape(h, w, -1) map_rgb = to8U(Lab2rgb(map_Lab)) print image.shape print map_rgb.shape map_image = setAlpha(map_rgb, alpha(image)) return map_image
def _strokeEdited(self, stroke_sets): print "Graph Cut Command" if len(stroke_sets.strokeSets()) < 2: return fg_stroke_set, bg_stroke_set = stroke_sets.strokeSets()[:2] image = self._scene.image() image_rgb = to8U(rgb(image)) mask = np.zeros(image.shape[:2], dtype=np.uint8) mask.fill(cv2.GC_PR_BGD) for stroke_set, color in zip([fg_stroke_set, bg_stroke_set], [int(cv2.GC_FGD), int(cv2.GC_BGD)]): for stroke in stroke_set.strokes(): if stroke.empty(): continue points = stroke.points() points = np.int32(points) brush_size = int(stroke.brushSize()) print color cv2.polylines(mask, [points], 0, color, brush_size) bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) cv2.grabCut(image_rgb, mask, None, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK) fg_mask = np.zeros(image.shape[:2], dtype=np.uint8) fg_mask[(mask==int(cv2.GC_FGD)) | (mask==int(cv2.GC_PR_FGD))] = 255 bg_mask = np.zeros(image.shape[:2], dtype=np.uint8) bg_mask[(mask==int(cv2.GC_BGD)) | (mask==int(cv2.GC_PR_BGD))] = 255 layer_set = self._scene.layerSet() layer_set.clear() layer_set.addLayer(Layer(name="BG", color=(0.0, 0.0, 1.0, 0.4), mask=bg_mask)) layer_set.addLayer(Layer(name="FG", color=(1.0, 0.0, 0.0, 0.4), mask=fg_mask))
def sparseHistogramSampling(image, num_color_bin=24, num_xy_bins=64): h, w = image.shape[:2] Lab = LabFeatures(image) xy = positionFeatures(image) Labxy = np.concatenate((Lab, xy), axis=1) num_bins = [num_color_bin, num_color_bin, num_color_bin, num_xy_bins, num_xy_bins] num_color_bins = num_bins[:] num_color_bins.append(3) hist_bins = np.zeros(num_bins, dtype=np.float32) color_bins = np.zeros(num_color_bins, dtype=np.float32) f_min = np.min(Labxy, axis=0) f_max = np.max(Labxy, axis=0) num_bins = np.array(num_bins) f_ids = (num_bins - 1) * (Labxy - f_min) / (f_max - f_min) f_ids = np.int32(f_ids) hist_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]] += 1 color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]] += Lab[:, :] hist_positive = hist_bins > 0.0 print np.count_nonzero(hist_positive) for ci in xrange(3): color_bins[hist_positive, ci] /= hist_bins[hist_positive] map_Lab = color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]].reshape(h, w, -1) map_rgb = to8U(Lab2rgb(map_Lab)) print image.shape print map_rgb.shape map_image = setAlpha(map_rgb, alpha(image)) return map_image
def _interpolateNormal(self): if self._normal_constraints.empty(): return if self._image is None: return ps = np.int32(self._normal_constraints.positions()) ns = self._normal_constraints.normals() h, w = self._image.shape[:2] N0_32F = np.zeros((h, w, 3)) N0_32F[ps[:, 1], ps[:, 0]] = ns W_32F = np.zeros((h, w)) W_32F[ps[:, 1], ps[:, 0]] = 1.0 A_8U = None if self._image.shape[2] == 4: A_8U = to8U(alpha(self._image)) self._N_32F = self._interpolateNormalImage(N0_32F, W_32F, A_8U) self._projectConstraints()
def slicSampling(image, num_segments=4000, sigma=5): h, w = image.shape[:2] C_32F = to32F(rgb(image)) segments = slic(C_32F, n_segments=num_segments, sigma=sigma) print segments.shape print np.max(segments) num_centers = np.max(segments) + 1 hist_centers = np.zeros((num_centers), dtype=np.float32) centers = np.zeros((num_centers, 3), dtype=np.float32) hist_centers[segments[:, :]] += 1.0 centers[segments[:, :], :] += C_32F[:, :, :3] hist_positive = hist_centers > 0.0 print np.count_nonzero(hist_positive) for ci in xrange(3): centers[hist_positive, ci] /= hist_centers[hist_positive] map_rgb = to8U(centers[segments[:, :], :].reshape(h, w, -1)) #map_rgb = to8U(mark_boundaries(C_32F, segments)) map_image = setAlpha(map_rgb, alpha(image)) return map_image
def slicSampling(image, num_segments=4000, sigma=5): h, w = image.shape[:2] C_32F = to32F(rgb(image)) segments = slic(C_32F, n_segments=num_segments, sigma=sigma) print segments.shape print np.max(segments) num_centers = np.max(segments) + 1 hist_centers = np.zeros((num_centers), dtype=np.float32) centers = np.zeros((num_centers, 3), dtype=np.float32) hist_centers[segments[:, :]] += 1.0 centers[segments[:, :], :] += C_32F[:, :, :3] hist_positive = hist_centers > 0.0 print np.count_nonzero(hist_positive) for ci in xrange(3): centers[hist_positive, ci] /= hist_centers[hist_positive] map_rgb = to8U(centers[segments[:, :], :].reshape(h, w, -1)) # map_rgb = to8U(mark_boundaries(C_32F, segments)) map_image = setAlpha(map_rgb, alpha(image)) return map_image
def computeIllumination(L, N_32F, A_8U): I_32F = np.clip(LdotN(L, N_32F), 0.0, 1.0) I_32F[A_8U < 0.5 * np.max(A_8U)] = 0.0 I_32F = setAlpha(gray2rgb(to8U(I_32F)), A_8U) I_32F = trim(I_32F, A_8U) return I_32F
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 __init__(self, image): self._image = to8U(image) self._geometry = Mesh() self._initGeometry() self._texture_id = None