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 _compute(self): image = self._image img_32F = to32F(self._image) self._A_32F = alpha(img_32F) sigma_xy = 100.0 xy = positionFeatures(img_32F) / sigma_xy Lab = LabFeatures(img_32F) sigma_L = 1.0 Lab[:, 0] /= sigma_L foreground = foreGroundFeatures(img_32F) Labxy = np.concatenate((Lab, xy), axis=1) Labxy_samples = shuffle(Labxy[foreground, :], random_state=0)[:1000] kmeans = sklearn.cluster.KMeans(n_clusters=self._num_colors, random_state=0).fit(Labxy_samples) self._centers = kmeans.cluster_centers_ self._centers[:, 0] *= sigma_L labels = kmeans.predict(Labxy) self._labels = labels.reshape(image.shape[:2])
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 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 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