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 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 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