我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用skimage.color.rgb2lab()。
def average(self, aligned = True): ''' averaging procedure, this function saves the newly calculated average''' if aligned: dataset = self.algimgs else: dataset = self.imgs s = MyRGBImg(np.zeros(dataset[0].data.shape)) s = color.rgb2lab(s.data) for i, picture in enumerate(dataset): print("Averaging image: " , i) # convert both to lab im = color.rgb2lab(picture.data) #perform operations s += im s = s / float(len(dataset)) self.avg = MyRGBImg(color.lab2rgb(s))
def __getitem__(self, index): path, target = self.imgs[index] img = self.loader(path) if self.transform is not None: img_original = self.transform(img) img_original = np.asarray(img_original) img_lab = rgb2lab(img_original) img_lab = (img_lab + 128) / 255 img_ab = img_lab[:, :, 1:3] img_ab = torch.from_numpy(img_ab.transpose((2, 0, 1))) img_original = rgb2gray(img_original) img_original = torch.from_numpy(img_original) if self.target_transform is not None: target = self.target_transform(target) return (img_original, img_ab), target
def extract_colour_histogram(image, labels, n_bins=8, use_lab=False): ih, iw, _ = image.shape n_labels = labels.max()+1 _range = np.array([[0, 256], [0, 256], [0, 256]], dtype='float') # for rgb histograms if use_lab: image = rgb2lab(image) _range[:] = [[0,100],[-500*25/29, 500*25/29], [-200*25/29, 200*25/29]] hist = np.zeros((n_labels, n_bins**3)) mask = np.zeros((ih, iw), dtype='bool') for i in range(n_labels): mask[:] = labels == i yy, xx = mask.nonzero() pixels = image[yy, xx, :] hist[i, :] = np.histogramdd(sample=pixels, bins=n_bins, range=_range)[0].flat return hist
def ransac_guess_color(colors, n_iter=50, std=2): colors = rgb2lab(colors) colors = colors.reshape(-1, 3) masked = colors[:, 0] < 0.1 colors = colors[~masked] assert len(colors) > 0, "Must have at least one color" best_mu = np.array([0, 0, 0]) best_n = 0 for k in range(n_iter): subset = colors[np.random.choice(np.arange(len(colors)), 1)] mu = subset.mean(0) #inliers = (((colors - mu) ** 2 / std) < 1).all(1) inliers = ((np.sqrt(np.sum((colors - mu)**2, axis=1)) / std) < 1) mu = colors[inliers].mean(0) n = len(colors[inliers]) if n > best_n: best_n = n best_mu = mu #import ipdb; ipdb.set_trace() best_mu = np.squeeze(lab2rgb(np.array([[best_mu]]))) return best_mu
def applyTexture(x, y, texture = texture_input): text = imread(texture_input) height,width = text.shape[:2] xmin, ymin = amin(x),amin(y) xmax, ymax = amax(x),amax(y) scale = max(((xmax - xmin + 2)/height),((ymax - ymin + 2)/width)) text = imresize(text, scale) # print text.shape[:2] # print xmax - xmin +2, ymax - ymin+2 X = (x-xmin).astype(int) Y = (y-ymin).astype(int) val1 = color.rgb2lab((text[X, Y]/255.).reshape(len(X), 1, 3)).reshape(len(X), 3) val2 = color.rgb2lab((im[x, y]/255.).reshape(len(x), 1, 3)).reshape(len(x), 3) L, A, B = mean(val2[:,0]), mean(val2[:,1]), mean(val2[:,2]) val2[:, 0] = np.clip(val2[:, 0] - L + val1[:,0], 0, 100) val2[:, 1] = np.clip(val2[:, 1] - A + val1[:,1], -127, 128) val2[:, 2] = np.clip(val2[:, 2] - B + val1[:,2], -127, 128) im[x,y] = color.lab2rgb(val2.reshape(len(x), 1, 3)).reshape(len(x), 3)*255 # points = np.loadtxt('nailpoint_5')
def __call__(self, image, test): image_data = numpy.asarray(image, dtype=numpy.float32)[:, :, :3] rgb_image_data = image_data.transpose(2, 0, 1) lab_image_data = rgb2lab(image_data / 255).transpose(2, 0, 1).astype(numpy.float32) luminous_image_data = lab_image_data[0].astype(numpy.uint8) try: th = threshold_otsu(luminous_image_data) except: import traceback print(traceback.format_exc()) th = 0 linedrawing = (luminous_image_data > th).astype(numpy.float32) linedrawing = numpy.expand_dims(linedrawing, axis=0) return lab_image_data, linedrawing, rgb_image_data
def _draw_process(self, small_input_image, big_input_image): lab = rgb2lab(numpy.array(small_input_image)) lab[:, :, 0] /= 100 small_image = self.drawer.draw( input_images_array=lab.astype(numpy.float32).transpose(2, 0, 1)[numpy.newaxis], rgb_images_array=numpy.array(self.reference_image, dtype=numpy.float32).transpose(2, 0, 1)[numpy.newaxis], )[0] small_image = small_image.convert('RGB') if self.drawer_sr is not None: drawn_panel_image = self._superresolution_process(small_image, big_input_image) else: drawn_panel_image = small_image return drawn_panel_image
def Get_Batch_Chrominance(): ''''Convert every image in the batch to LAB Colorspace and normalize each value of it between [0,1] Return: AbColores_values array [batch_size,2224,224,2] 0-> A value, 1-> B value color ''' global AbColores_values global ColorImages_Batch AbColores_values = np.empty((Batch_size,224,224,2),"float32") for indx in range(Batch_size): lab = color.rgb2lab(ColorImages_Batch[indx]) Min_valueA = np.amin(lab[:,:,1]) Max_valueA = np.amax(lab[:,:,1]) Min_valueB = np.amin(lab[:,:,2]) Max_valueB = np.amax(lab[:,:,2]) AbColores_values[indx,:,:,0] = Normalize(lab[:,:,1],-128,127) AbColores_values[indx,:,:,1] = Normalize(lab[:,:,2],-128,127)
def Get_Batch_Chrominance(ColorImages_Batch, Batch_Size): ''''Convert every image in the batch to LAB Colorspace and normalize each value of it between [0,1] Return: AbColores_values array [batch_size,2224,224,2] 0-> A value, 1-> B value color ''' AbColores_values = np.empty((Batch_Size, MainHeight, MainWidth, 2),"float32") for indx in range(Batch_Size): lab = color.rgb2lab(ColorImages_Batch[indx]) AbColores_values[indx,:,:,0] = Normalize(lab[:,:,1],NormalizationRange[0],NormalizationRange[1]) AbColores_values[indx,:,:,1] = Normalize(lab[:,:,2],NormalizationRange[0],NormalizationRange[1]) return AbColores_values #----------------------------------------------------------------------------------------------
def myrgb2lab(I: np.ndarray, row_num: int, col_num: int): """ change rgb to lab format :param I: rgb format image :return: L: L channel, range from 0 to 255, dtype: uint8, shape: (row_num * col_num,) a: a channel, range from 0 to 255, dtype: uint8, shape: (row_num * col_num,) b: b channel, range from 0 to 255, dtype: uint8, shape: (row_num * col_num,) """ lab_img = color.rgb2lab(I).transpose([2, 1, 0]) L = lab_img[0].copy().reshape([row_num * col_num]) a = lab_img[1].copy().reshape([row_num * col_num]) b = lab_img[2].copy().reshape([row_num * col_num]) L /= (100 / 255) # L is [0, 100], change it to [0, 255] L += 0.5 a += 128 + 0.5 # A is [-128, 127], change it to [0, 255] b += 128 + 0.5 # B is [-128, 127], change it to [0, 255] return L.astype(np.uint8), a.astype(np.uint8), b.astype(np.uint8)
def _global(self, img): h, w = img.shape[:2] mask = np.zeros((h, w), dtype=np.bool) max_distance = self._settings['max_distance'] if self._settings['use_lab']: img = skc.rgb2lab(img) # Compute euclidean distance of each corner against all other pixels. corners = [(0, 0), (-1, 0), (0, -1), (-1, -1)] for color in (img[i, j] for i, j in corners): norm = np.sqrt(np.sum(np.square(img - color), 2)) # Add to the mask pixels close to one of the corners. mask |= norm < max_distance return mask
def _transform(self, filename): try: image = misc.imread(filename) if len(image.shape) < 3: # make sure images are of shape(h,w,3) image = np.array([image for i in range(3)]) if self.image_options.get("resize", False) and self.image_options["resize"]: resize_size = int(self.image_options["resize_size"]) resize_image = misc.imresize(image, [resize_size, resize_size]) else: resize_image = image if self.image_options.get("color", False): option = self.image_options['color'] if option == "LAB": resize_image = color.rgb2lab(resize_image) elif option == "HSV": resize_image = color.rgb2hsv(resize_image) except: print ("Error reading file: %s of shape %s" % (filename, str(image.shape))) raise return np.array(resize_image)
def quantize(cls, raster, n_colors, **kwargs): lab_raster = color.rgb2lab(raster) lab_quantized_raster = super(RGBtoLABmixin, cls).quantize( lab_raster, n_colors, **kwargs) quantized_raster = (color.lab2rgb(lab_quantized_raster) * 255).astype( 'uint8') return quantized_raster
def applyNailPolish(x , y , r = Rg, g = Gg, b = Bg): val = color.rgb2lab((im[x, y]/255.).reshape(len(x), 1, 3)).reshape(len(x), 3) L, A, B = mean(val[:,0]), mean(val[:,1]), mean(val[:,2]) L1, A1, B1 = color.rgb2lab(np.array((r/255., g/255., b/255.)).reshape(1, 1, 3)).reshape(3,) ll, aa, bb = L1 - L, A1 - A, B1 - B val[:, 0] = np.clip(val[:, 0] + ll, 0, 100) val[:, 1] = np.clip(val[:, 1] + aa, -127, 128) val[:, 2] = np.clip(val[:, 2] + bb, -127, 128) im[x, y] = color.lab2rgb(val.reshape(len(x), 1, 3)).reshape(len(x), 3)*255
def rgb_to_lab(x): """Converts RGB image to the lab colorspace [0; 100] [-127; 128] [-128; 127].""" return rgb2lab(x)
def __init__(self, image, pred, params): """ pred: [C x H x W] """ #assert pred.shape[1:3] == prediction_shape prediction_shape = pred.shape[1:3] self.prediction_shape = prediction_shape self.npixels = np.prod(prediction_shape[0:2]) self.nlabels = pred.shape[0] self.params = params # Convert from [C x H x W] to [(H*W) x C] unary_probs = pred.reshape(self.nlabels, self.npixels).transpose() # remove 'other' class #unary_probs[:, config.NAME_TO_LABEL['other']] = 0.0 self.unary_costs = -np.log(np.clip(unary_probs + params['unary_prob_padding'], 1e-20, 1e20)) self.unary_costs = np.copy(self.unary_costs, order='C').astype(np.float32) if image.shape[0:2] == prediction_shape: self.im_lab = rgb2lab(image) else: self.im_lab = rgb2lab(transform.resize(image, prediction_shape[0:2])) # scale features to have have dynamic range ~10-20ish self.scaled_positions = ( np.indices(prediction_shape[0:2]).astype(np.float32) * 10.0 / float(np.min(prediction_shape[0:2])) ) self.bilateral_features = np.zeros((self.npixels, 5), dtype=np.float32) self.bilateral_features[:, 0] = self.scaled_positions[0].ravel() self.bilateral_features[:, 1] = self.scaled_positions[1].ravel() self.bilateral_features[:, 2] = self.im_lab[:, :, 0].ravel() / 10.0 self.bilateral_features[:, 3] = self.im_lab[:, :, 1].ravel() / 10.0 self.bilateral_features[:, 4] = self.im_lab[:, :, 2].ravel() / 10.0
def __init__(self, image, pred, params): """ pred: [C x H x W] """ #assert pred.shape[1:3] == prediction_shape prediction_shape = pred.shape[1:3] self.prediction_shape = prediction_shape self.npixels = np.prod(prediction_shape[0:2]) self.nlabels = pred.shape[0] self.params = params # Convert from [C x H x W] to [(H*W) x C], remove 'other' class unary_probs = pred.reshape(self.nlabels, self.npixels).transpose() unary_probs[:, config.NAME_TO_LABEL['other']] = 0.0 self.unary_costs = -np.log(np.clip(unary_probs + params['unary_prob_padding'], 1e-20, 1e20)) self.unary_costs = np.copy(self.unary_costs, order='C').astype(np.float32) if image.shape[0:2] == prediction_shape: self.im_lab = rgb2lab(image) else: self.im_lab = rgb2lab(transform.resize(image, prediction_shape[0:2])) # scale features to have have dynamic range ~10-20ish self.scaled_positions = ( np.indices(prediction_shape[0:2]).astype(np.float32) * 10.0 / float(np.min(prediction_shape[0:2])) ) self.bilateral_features = np.zeros((self.npixels, 5), dtype=np.float32) self.bilateral_features[:, 0] = self.scaled_positions[0].ravel() self.bilateral_features[:, 1] = self.scaled_positions[1].ravel() self.bilateral_features[:, 2] = self.im_lab[:, :, 0].ravel() / 10.0 self.bilateral_features[:, 3] = self.im_lab[:, :, 1].ravel() / 10.0 self.bilateral_features[:, 4] = self.im_lab[:, :, 2].ravel() / 10.0 #position_features = np.zeros((npixels, 2), dtype=np.float32) #position_features[:, 0] = scaled_positions[0].ravel() * (params['position_theta_xy'] / mindim) #position_features[:, 1] = scaled_positions[1].ravel() * (params['position_theta_xy'] / mindim)
def __call__(self, image, test): # type: (Image.Image, any) -> any image = numpy.asarray(image, dtype=self._dtype)[:, :, :3] / 255 # rgb image = rgb2lab(image).astype(self._dtype).transpose(2, 0, 1) if self._normalize: image /= 50 image[0] -= 1 return image
def _calc_rgb2lab_min_max(): """ :return: ([L_min, a_min, b_min], [L_max, a_max, b_max]) """ num_space = 16 size_image = num_space * num_space * num_space values_pixel = numpy.linspace(0, 1, num_space) image_array = [[r, g, b] for r in values_pixel for g in values_pixel for b in values_pixel] image_array = numpy.vstack(image_array).reshape((1, size_image, 3)) image_array = rgb2lab(image_array) # illuminant='D65' return image_array.min(axis=1).squeeze(), image_array.max(axis=1).squeeze()
def get_example(self, i): # type: (any) -> typing.Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray] rgb_image_data, gray_image_data, _ = self.base[i] dtype = rgb_image_data.dtype image_data = rgb_image_data.transpose(1, 2, 0) / 255 lab_image_data = rgb2lab(image_data).transpose(2, 0, 1).astype(dtype) luminous_image_data = numpy.expand_dims(lab_image_data[0], axis=0) return lab_image_data, luminous_image_data, rgb_image_data
def image_a_b_gen(batch_size): for batch in datagen.flow(Xtrain, batch_size=batch_size): if batch == None: break lab_batch = rgb2lab(batch) X_batch = lab_batch[:,:,:,0] Y_batch = lab_batch[:,:,:,1:] yield (X_batch.reshape(X_batch.shape+(1,)), Y_batch) # Train model
def apply_nail_polish(x, y, r=Rg, g=Gg, b=Bg): val = color.rgb2lab((im[x, y] / 255.).reshape(len(x), 1, 3)).reshape(len(x), 3) L, A, B = mean(val[:, 0]), mean(val[:, 1]), mean(val[:, 2]) L1, A1, B1 = color.rgb2lab(np.array((r / 255., g / 255., b / 255.)).reshape(1, 1, 3)).reshape(3, ) ll, aa, bb = L1 - L, A1 - A, B1 - B val[:, 0] = np.clip(val[:, 0] + ll, 0, 100) val[:, 1] = np.clip(val[:, 1] + aa, -127, 128) val[:, 2] = np.clip(val[:, 2] + bb, -127, 128) im[x, y] = color.lab2rgb(val.reshape(len(x), 1, 3)).reshape(len(x), 3) * 255
def apply_texture(x, y): xmin, ymin = amin(x), amin(y) X = (x - xmin).astype(int) Y = (y - ymin).astype(int) val1 = color.rgb2lab((text[X, Y] / 255.).reshape(len(X), 1, 3)).reshape(len(X), 3) val2 = color.rgb2lab((im[x, y] / 255.).reshape(len(x), 1, 3)).reshape(len(x), 3) L, A, B = mean(val2[:, 0]), mean(val2[:, 1]), mean(val2[:, 2]) val2[:, 0] = np.clip(val2[:, 0] - L + val1[:, 0], 0, 100) val2[:, 1] = np.clip(val2[:, 1] - A + val1[:, 1], -127, 128) val2[:, 2] = np.clip(val2[:, 2] - B + val1[:, 2], -127, 128) im[x, y] = color.lab2rgb(val2.reshape(len(x), 1, 3)).reshape(len(x), 3) * 255
def apply_blush_color(r=Rg, g=Gg, b=Bg): global im val = color.rgb2lab((im / 255.)).reshape(width * height, 3) L, A, B = mean(val[:, 0]), mean(val[:, 1]), mean(val[:, 2]) L1, A1, B1 = color.rgb2lab(np.array((r / 255., g / 255., b / 255.)).reshape(1, 1, 3)).reshape(3, ) ll, aa, bb = (L1 - L) * intensity, (A1 - A) * intensity, (B1 - B) * intensity val[:, 0] = np.clip(val[:, 0] + ll, 0, 100) val[:, 1] = np.clip(val[:, 1] + aa, -127, 128) val[:, 2] = np.clip(val[:, 2] + bb, -127, 128) im = color.lab2rgb(val.reshape(height, width, 3)) * 255
def load_demo_image(): im = color.rgb2lab(Image.open('../object_recognition/img/Patern_test.jpg')) / 100.0 return im[..., 0]
def trans(self, img): rst = color.rgb2lab(img) print('============', rst.min(), rst.max()) rst+=100; rst*=(255/200.0) return (rst).astype(np.uint8)
def preprocess(image, use_rgb=False, use_hsv=False, norm=True): """ Preprocesses a RGB image before extracting super-regions from it. Improves the quality of the super-regions by transforming to the L*a*b colorspace and normalizing the image. Args: image: numpy (H, W) or (H, W, 3) array RGB image to be preprocessed. use_rgb: bool Wether to append RGB channels to the L*a*b channels. use_hsv: bool Wether to append HSV channels to the L*a*b channels. norm: Wether to standardize individual channels. Result: result: numpy (H * W, K) array Where K is 3, 6 or depending on `use_rgb` and `use_hsv`. channel specific normalization to enhance distances. """ if image.ndim == 2 or image.shape[2] == 1: data = (np.squeeze(image) - image.mean()) / image.std() return data assert image.shape[2] == 3, 'Error: invalid image format' result = color.rgb2lab(image).reshape(-1, 3) if use_rgb: result = np.column_stack(result, image.reshape(-1, 3)) if use_hsv: result = np.column_stack(result, color.rgb2hsv(data).reshape(-1, 3)) # Standardize channels and reshape in-place if norm: result = (result - result.mean(0)) / result.std(0) return result.astype(np.float32)
def __call__(self, image: numpy.ndarray, test): from scipy import stats def dilate_diff(image, range, iterations=1): dil = cv2.dilate(image, numpy.ones((range, range), numpy.float32), iterations=iterations) image = cv2.absdiff(image, dil) return image dtype = image.dtype rgb = (image.transpose(1, 2, 0) + 1) / 2 lab = rgb2lab(rgb) / 100 image = lab[:, :, 0] image = dilate_diff(image, 3).astype(numpy.float32) rand = 0.2 + (numpy.random.randn(1) / 20 if not test else 0) rand = 0.000001 if rand <= 0 else rand image = cv2.GaussianBlur(image, (5, 5), rand) rand = 0.4 + (numpy.random.randn(1) / 20 if not test else 0) rand = 0.000001 if rand <= 0 else rand image = cv2.GaussianBlur(image, (5, 5), rand) rand = numpy.random.randn(1) / 40 if not test else 0 image = numpy.power(image, 0.8 + rand) image = image.astype(dtype)[numpy.newaxis] return image
def _find_source_patch(self, target_pixel): target_patch = self._get_patch(target_pixel) height, width = self.working_image.shape[:2] patch_height, patch_width = self._patch_shape(target_patch) best_match = None best_match_difference = 0 lab_image = rgb2lab(self.working_image) for y in range(height - patch_height + 1): for x in range(width - patch_width + 1): source_patch = [ [y, y + patch_height-1], [x, x + patch_width-1] ] if self._patch_data(self.working_mask, source_patch) \ .sum() != 0: continue difference = self._calc_patch_difference( lab_image, target_patch, source_patch ) if best_match is None or difference < best_match_difference: best_match = source_patch best_match_difference = difference return best_match
def rgb2lch(rgb): """Convert RBG to LCH colorspace (via LAB) Input and output are in (bands, cols, rows) order """ # reshape for skimage (bands, cols, rows) -> (cols, rows, bands) srgb = np.swapaxes(rgb, 0, 2) # convert colorspace lch = lab2lch(rgb2lab(srgb)) # return in (bands, cols, rows) order return np.swapaxes(lch, 2, 0)
def color_hist(im, colBins): """ Get color histogram descriptors for RGB and LAB space. Input: im: (h,w,c): 0-255: np.uint8: RGB Output: descriptor: (colBins*6,) """ assert im.ndim == 3 and im.shape[2] == 3, "image should be rgb" arr = np.concatenate((im, color.rgb2lab(im)), axis=2).reshape((-1, 6)) desc = np.zeros((colBins * 6,), dtype=np.float) for i in range(3): desc[i * colBins:(i + 1) * colBins], _ = np.histogram( arr[:, i], bins=colBins, range=(0, 255)) desc[i * colBins:(i + 1) * colBins] /= np.sum( desc[i * colBins:(i + 1) * colBins]) + ( np.sum(desc[i * colBins:(i + 1) * colBins]) < 1e-4) i += 1 desc[i * colBins:(i + 1) * colBins], _ = np.histogram( arr[:, i], bins=colBins, range=(0, 100)) desc[i * colBins:(i + 1) * colBins] /= np.sum( desc[i * colBins:(i + 1) * colBins]) + ( np.sum(desc[i * colBins:(i + 1) * colBins]) < 1e-4) for i in range(4, 6): desc[i * colBins:(i + 1) * colBins], _ = np.histogram( arr[:, i], bins=colBins, range=(-128, 127)) desc[i * colBins:(i + 1) * colBins] /= np.sum( desc[i * colBins:(i + 1) * colBins]) + ( np.sum(desc[i * colBins:(i + 1) * colBins]) < 1e-4) return desc
def color_hist(im, colBins): """ Get color histogram descriptors for RGB and LAB space. Input: im: (h,w,c): 0-255: np.uint8 Output: descriptor: (colBins*6,) """ assert im.ndim == 3 and im.shape[2] == 3, "image should be rgb" arr = np.concatenate((im, color.rgb2lab(im)), axis=2).reshape((-1, 6)) desc = np.zeros((colBins * 6,), dtype=np.float) for i in range(3): desc[i * colBins:(i + 1) * colBins], _ = np.histogram( arr[:, i], bins=colBins, range=(0, 255)) desc[i * colBins:(i + 1) * colBins] /= np.sum( desc[i * colBins:(i + 1) * colBins]) + ( np.sum(desc[i * colBins:(i + 1) * colBins]) < 1e-4) i += 1 desc[i * colBins:(i + 1) * colBins], _ = np.histogram( arr[:, i], bins=colBins, range=(0, 100)) desc[i * colBins:(i + 1) * colBins] /= np.sum( desc[i * colBins:(i + 1) * colBins]) + ( np.sum(desc[i * colBins:(i + 1) * colBins]) < 1e-4) for i in range(4, 6): desc[i * colBins:(i + 1) * colBins], _ = np.histogram( arr[:, i], bins=colBins, range=(-128, 127)) desc[i * colBins:(i + 1) * colBins] /= np.sum( desc[i * colBins:(i + 1) * colBins]) + ( np.sum(desc[i * colBins:(i + 1) * colBins]) < 1e-4) return desc
def compute_saliency(img): """ Computes Boolean Map Saliency (BMS). """ img_lab = rgb2lab(img) img_lab -= img_lab.min() img_lab /= img_lab.max() thresholds = np.arange(0, 1, 1.0 / N_THRESHOLDS)[1:] # compute boolean maps bool_maps = [] for thresh in thresholds: img_lab_T = img_lab.transpose(2, 0, 1) img_thresh = (img_lab_T > thresh) bool_maps.extend(list(img_thresh)) # compute mean attention map attn_map = np.zeros(img_lab.shape[:2], dtype=np.float) for bool_map in bool_maps: attn_map += activate_boolean_map(bool_map) attn_map /= N_THRESHOLDS # gaussian smoothing attn_map = cv2.GaussianBlur(attn_map, (0, 0), 3) # perform normalization norm = np.sqrt((attn_map**2).sum()) attn_map /= norm attn_map /= attn_map.max() / 255 return attn_map.astype(np.uint8)
def save_zhang_feats(img_fns, ext='JPEG'): gpu_id = 0 caffe.set_mode_gpu() caffe.set_device(gpu_id) net = caffe.Net('third_party/colorization/models/colorization_deploy_v1.prototxt', \ 'third_party/colorization/models/colorization_release_v1.caffemodel', caffe.TEST) (H_in,W_in) = net.blobs['data_l'].data.shape[2:] # get input shape (H_out,W_out) = net.blobs['class8_ab'].data.shape[2:] # get output shape net.blobs['Trecip'].data[...] = 6/np.log(10) # 1/T, set annealing temperature feats_fns = [] for img_fn_i, img_fn in enumerate(img_fns): # load the original image img_rgb = caffe.io.load_image(img_fn) img_lab = color.rgb2lab(img_rgb) # convert image to lab color space img_l = img_lab[:,:,0] # pull out L channel (H_orig,W_orig) = img_rgb.shape[:2] # original image size # create grayscale version of image (just for displaying) img_lab_bw = img_lab.copy() img_lab_bw[:,:,1:] = 0 img_rgb_bw = color.lab2rgb(img_lab_bw) # resize image to network input size img_rs = caffe.io.resize_image(img_rgb,(H_in,W_in)) # resize image to network input size img_lab_rs = color.rgb2lab(img_rs) img_l_rs = img_lab_rs[:,:,0] net.blobs['data_l'].data[0,0,:,:] = img_l_rs-50 # subtract 50 for mean-centering net.forward() # run network npz_fn = img_fn.replace(ext, 'npz') np.savez_compressed(npz_fn, net.blobs['conv7_3'].data) feats_fns.append(npz_fn) return feats_fns
def tensor2image(image): """ convert a mean-0 tensor to float numpy image :param image: :return: image """ image = image.clone() image[0] = image[0] + 122.67891434 image[1] = image[1] + 116.66876762 image[2] = image[2] + 104.00698793 image = image.numpy() / 255.0 image = image.transpose((1, 2, 0)) image = img_as_ubyte(image) return image # def prior_map(img): # """ # get RFCN prior map # :param img: numpy array (H*W*C, RGB), [0, 1], float # :return: pmap # """ # # step 1 over segmentation into superpixels # sp = slic(img, n_segments=200, sigma=5) # sp_num = sp.max() + 1 # sp = sp.astype(float) # # # step 2 the mean lab color of the sps # mean_lab_color = np.zeros((sp_num, 3)) # lab_img = color.rgb2lab(img) # for c in range(3): # for i in range(sp_num): # mean_lab_color[i, c] = lab_img[sp == i, c].mean() # # # step 3, element uniqueness return pimg
def average_mean(self, aligned = True, debug = False, transition = True): ''' performs the mean of the images, aligned is True will use the aligned pictures while if false will use the original picture, for the transition, each averaging step is printed out ''' self.mylog.log("started the mean averaging procedure") sizedataset = len(self.imgs_names) if aligned: picture = self.get_alg_image(0) else: picture = self.get_image(0) # initialize sum variable s = MyRGBImg(np.zeros(picture.data.shape)) #s = color.rgb2lab(s.data) for i in range(sizedataset): if debug: self.mylog.log("Averaging image: " + str(i)) #load the picture if aligned: picture = self.get_alg_image(i) else: picture = self.get_image(i) # convert both to lab #im = color.rgb2lab(picture.data) im = picture.data #perform operations s += im # if the transition is true show what happens to each picture if transition: tr = s / float(i + 1) #avg = MyRGBImg(color.lab2rgb(tr)) avg = tr avg.save(join(self.subfolders["avg_transition"], "avg_tr_" + str(i) + ".png")) # calculate the average s = s / float(sizedataset) #self.avg = MyRGBImg(color.lab2rgb(s)) self.avg = s # small trick to align the image in the correct sense if they are # squared if self.avg.data.shape[0] == self.avg.data.shape[1]: self.avg.rotate(90) self.avg.flip_V()
def generate_features(self): # prepare variables img_lab = rgb2lab(self._img) segments = slic(img_lab, n_segments=500, compactness=30.0, convert2lab=False) max_segments = segments.max() + 1 # create x,y feather shape = self._img.shape a = shape[0] b = shape[1] x_axis = np.linspace(0, b - 1, num=b) y_axis = np.linspace(0, a - 1, num=a) x_coordinate = np.tile(x_axis, (a, 1,)) # ??X????? y_coordinate = np.tile(y_axis, (b, 1,)) # ??y????? y_coordinate = np.transpose(y_coordinate) coordinate_segments_mean = np.zeros((max_segments, 2)) # create lab feather img_l = img_lab[:, :, 0] img_a = img_lab[:, :, 1] img_b = img_lab[:, :, 2] img_segments_mean = np.zeros((max_segments, 3)) for i in xrange(max_segments): segments_i = segments == i coordinate_segments_mean[i, 0] = x_coordinate[segments_i].mean() coordinate_segments_mean[i, 1] = y_coordinate[segments_i].mean() img_segments_mean[i, 0] = img_l[segments_i].mean() img_segments_mean[i, 1] = img_a[segments_i].mean() img_segments_mean[i, 2] = img_b[segments_i].mean() # element distribution wc_ij = np.exp(-cdist(img_segments_mean, img_segments_mean) ** 2 / (2 * self._sigma_distribution ** 2)) wc_ij = wc_ij / wc_ij.sum(axis=1)[:, None] mu_i = np.dot(wc_ij, coordinate_segments_mean) distribution = np.dot(wc_ij, np.linalg.norm(coordinate_segments_mean - mu_i, axis=1) ** 2) distribution = normalize(distribution) distribution = np.array([distribution]).T # element uniqueness feature wp_ij = np.exp( -cdist(coordinate_segments_mean, coordinate_segments_mean) ** 2 / (2 * self._sigma_uniqueness ** 2)) wp_ij = wp_ij / wp_ij.sum(axis=1)[:, None] uniqueness = np.sum(cdist(img_segments_mean, img_segments_mean) ** 2 * wp_ij, axis=1) uniqueness = normalize(uniqueness) uniqueness = np.array([uniqueness]).T # save features and variables self.img_lab = img_lab self.segments = segments self.img_segments_mean = img_segments_mean self.coordinate_segments_mean = coordinate_segments_mean self.uniqueness = uniqueness self.distribution = distribution