我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用skimage.color.rgb2hsv()。
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 get_hsv(self, plot=False): """ Extract HSV values for each image. Creates bins for the HSV vectors. Also finds the peaks in the HSV histograms Input: None Output: None """ self.hsv_image = color.rgb2hsv(self.image) self.hue_bins, self.avg_hue, self.hue_var = self.create_hist_vector(self.hsv_image, 0, 48, (0.0, 1)) self.sat_bins, self.avg_sat, self.sat_var = self.create_hist_vector(self.hsv_image, 1, 32, (0.0, 1)) self.val_bins, self.avg_val, self.val_var = self.create_hist_vector(self.hsv_image, 2, 32, (0.0, 1)) # get the peaks self.hue_peaks = self.get_peaks(self.hue_bins, 0.5, 5) self.val_peaks = self.get_peaks(self.val_bins, 0.4, 5) self.sat_peaks = self.get_peaks(self.sat_bins, 0.4, 5) if plot is True: viz.plot_hsv(self.hsv_image)
def test_hsv_value(): filtered = edges_hsv(COLOR_IMAGE) value = color.rgb2hsv(COLOR_IMAGE)[:, :, 2] assert_allclose(color.rgb2hsv(filtered)[:, :, 2], filters.sobel(value))
def test_hsv_value_with_filter_argument(): filtered = smooth_hsv(COLOR_IMAGE, SIGMA) value = color.rgb2hsv(COLOR_IMAGE)[:, :, 2] assert_allclose(color.rgb2hsv(filtered)[:, :, 2], smooth(value))
def test_hsv_value_with_non_float_output(): # Since `rgb2hsv` returns a float image and the result of the filtered # result is inserted into the HSV image, we want to make sure there isn't # a dtype mismatch. filtered = edges_hsv_uint(COLOR_IMAGE) filtered_value = color.rgb2hsv(filtered)[:, :, 2] value = color.rgb2hsv(COLOR_IMAGE)[:, :, 2] # Reduce tolerance because dtype conversion. assert_allclose(filtered_value, filters.sobel(value), rtol=1e-5, atol=1e-5)
def identify_color(pixel_color, hsv_color_ranges, debug=False): """ This method compares colors to red, green, blue and white using the HSV color model to be able to detect colors more or less reliable, even for various light situations from a photo parameters: triple of rgb values returns: string of color ('blue', 'green', 'red', 'white') or None """ hsv = rgb2hsv(pixel_color) couldbe = {} for color, color_range in hsv_color_ranges.items(): # for every hsv color range in hsv_color_ranges couldbe[color] = 0 for i in range(0,3): # for every channel if i is 0 and color_range[0][i] > color_range[1][i]: # if it is h and min and max are reversed # its red, so from 0 to max or min to 1 if (0. <= hsv[i] <= color_range[1][i]) or (color_range[0][i] <= hsv[i] <= 1.): couldbe[color] += 1 else: ## if hsv channel between hsv color range_min and range_max if color_range[0][i] <= hsv[i] <= color_range[1][i]: couldbe[color] +=1 # save all colors where score in couldbe is 3, so all channels have matched # should not happen, but this is good for debugging the hsv color ranges possible_colors = [] for color, weight in couldbe.items(): if weight == 3: # matches all three channels possible_colors.append(color) if len(possible_colors) == 0: if debug: print('COLOR: matches no color\t\t',pixel_color, ' (rgb)\t\t',hsv,'(hsv)') return None elif len(possible_colors) == 1: if debug: print('COLOR: should be', possible_colors[0], '\t\t',pixel_color, ' (rgb)') return possible_colors[0] elif len(possible_colors) > 1: print('COLOR: CONFLICT! matches multiple colors (',possible_colors,')\t\t',pixel_color,' (rgb)\t',hsv,'(hsv)') return None
def rgb2hsv(rgb): """ Converts an RGB pixel to HSV scikit-image rgb2hsv method just accepts image arrays, so we make an array with one pixel """ x = numpy.zeros((1,1,3)) x[0,0] = rgb return color.rgb2hsv(x)[0][0]
def to_hsv(picture): return color.rgb2hsv(picture)
def RGBAtoHSVA(image): rgb = image[:,:,:3] a = image[:,:,3:] / 255.0 hsv = color.rgb2hsv(rgb) hsva = np.concatenate((hsv,a),axis=2) return hsva
def add_hsv_noise(rgb, hue_offset, saturation_offset, value_offset, proba=0.5): mask = np.all(rgb != 0, axis=2) hsv = rgb2hsv(rgb) if random.uniform(0, 1) > proba: hsv[:, :, 0] = (hsv[:, :, 0] + random.uniform(-hue_offset, hue_offset)) % 1 if random.uniform(0, 1) > proba-0.1: hsv[:, :, 1] = (hsv[:, :, 1] + random.uniform(-saturation_offset, saturation_offset)) % 1 if random.uniform(0, 1) > proba-0.1: hsv[:, :, 2] = (hsv[:, :, 2] + random.uniform(-value_offset, value_offset)) % 1 rgb = hsv2rgb(hsv) * 255 return rgb.astype(np.uint8) * mask[:, :, np.newaxis]
def process(self, im): return color.rgb2hsv(im)
def trans(self, img): rst = color.rgb2hsv(img) rst *= 255 print('============', rst.min(), rst.max()) return rst.astype(np.uint8)
def get(self, img): t = self._settings['skin_type'] if t == 'general': img = rgb2hsv(img) elif t == 'none': return np.zeros(img.shape[:2], np.bool) else: raise NotImplementedError('Only general type is implemented') return self._range_mask(img)
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 filter_image(path, min_size=300, max_size=None, min_aspect=1 / 4, max_aspect=4): import numpy from PIL import Image from skimage.color import rgb2hsv try: assert os.path.getsize(path) <= 100 * 1000 * 1000, 'file size error' image = Image.open(path).convert('RGB') w, h = image.size if min_size is not None: assert w >= min_size and h >= min_size, 'min size error' if max_size is not None: assert w <= max_size or h <= max_size, 'max size error' aspect = w / h assert min_aspect is None or aspect >= min_aspect, 'min aspect error' assert max_aspect is None or aspect <= max_aspect, 'max aspect error' # check data process data = data_process(path, test=True) # remove low saturation rgb = numpy.array(image, numpy.float32) / 255 hsv = rgb2hsv(rgb) sigma = numpy.var(hsv[:, :, 1]) assert sigma > 0.02, 'saturation error' except Exception as e: print(path, e) return False return True
def get_overlayed_image(x, c, gray_factor_bg = 0.3): ''' For an image x and a relevance vector c, overlay the image with the relevance vector to visualise the influence of the image pixels. ''' imDim = x.shape[0] if np.ndim(c)==1: c = c.reshape((imDim,imDim)) if np.ndim(x)==2: # this happens with the MNIST Data x = 1-np.dstack((x, x, x))*gray_factor_bg # make it a bit grayish if np.ndim(x)==3: # this is what happens with cifar data x = color.rgb2gray(x) x = 1-(1-x)*0.5 x = np.dstack((x,x,x)) alpha = 0.8 # Construct a colour image to superimpose im = plt.imshow(c, cmap = cm.seismic, vmin=-np.max(np.abs(c)), vmax=np.max(np.abs(c)), interpolation='nearest') color_mask = im.to_rgba(c)[:,:,[0,1,2]] # Convert the input image and color mask to Hue Saturation Value (HSV) colorspace img_hsv = color.rgb2hsv(x) color_mask_hsv = color.rgb2hsv(color_mask) # Replace the hue and saturation of the original image # with that of the color mask img_hsv[..., 0] = color_mask_hsv[..., 0] img_hsv[..., 1] = color_mask_hsv[..., 1] * alpha img_masked = color.hsv2rgb(img_hsv) return img_masked
def TF_shift_hue(x, shift=0.0): assert len(x.shape) == 3 h, w, nc = x.shape hsv = rgb2hsv(x) hsv[:,:,0] += shift return hsv2rgb(hsv)
def _change_brightness(self, X): X = color.rgb2hsv(X.transpose(1, 2, 0)/255.0) diff = np.random.uniform(-1 * self._brightness, self._brightness) X[:,:,2] = np.clip((X[:,:,2] + diff), 0.0, 1.0) X = color.hsv2rgb(X).transpose(2, 0, 1) * 255.0 return X.astype(np.int32)
def main(): """Load image, collect pixels, cluster, create segment images, plot.""" # load image img_rgb = data.coffee() img_rgb = misc.imresize(img_rgb, (256, 256)) / 255.0 img = color.rgb2hsv(img_rgb) height, width, channels = img.shape print("Image shape is: ", img.shape) # collect pixels as tuples of (r, g, b, y, x) print("Collecting pixels...") pixels = [] for y in range(height): for x in range(width): pixel = img[y, x, ...] pixels.append([pixel[0], pixel[1], pixel[2], (y/height)*2.0, (x/width)*2.0]) pixels = np.array(pixels) print("Found %d pixels to cluster" % (len(pixels))) # cluster the pixels using mean shift print("Clustering...") bandwidth = estimate_bandwidth(pixels, quantile=0.05, n_samples=500) clusterer = MeanShift(bandwidth=bandwidth, bin_seeding=True) labels = clusterer.fit_predict(pixels) # process labels generated during clustering labels_unique = set(labels) labels_counts = [(lu, len([l for l in labels if l == lu])) for lu in labels_unique] labels_unique = sorted(list(labels_unique), key=lambda l: labels_counts[l], reverse=True) nb_clusters = len(labels_unique) print("Found %d clusters" % (nb_clusters)) print(labels.shape) print("Creating images of segments...") img_segments = [np.copy(img_rgb)*0.25 for label in labels_unique] for y in range(height): for x in range(width): pixel_idx = (y*width) + x label = labels[pixel_idx] img_segments[label][y, x, 0] = 1.0 print("Plotting...") images = [img_rgb] titles = ["Image"] for i in range(min(8, nb_clusters)): images.append(img_segments[i]) titles.append("Segment %d" % (i)) plot_images(images, titles)