我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用cv2.COLOR_RGB2LAB。
def lab_reduce_colors(colors, weights, threshold=30, return_rgb=True): """ Reduce colors by L*a*b* space. """ _colors = np.array([colors], dtype=np.uint8) # cv2.cvtColor only accept 2d array lab_colors = cv2.cvtColor(_colors, cv2.COLOR_RGB2LAB)[0].astype(np.double) inds = [] for i, color in enumerate(lab_colors): dist = np.linalg.norm(hsv_colors_xyz[inds] - color, axis=1) if not dist.size or dist.min() > threshold: inds.append(i) if return_rgb: ret = colors[inds] else: ret = lab_colors[inds] return ret
def Ra_space(img, Ra_ratio, a_threshold): ''' Extract the Ra features by converting RGB to LAB space. The higher is a value, the "redder" is the pixel. ''' imgLab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB); w = img.shape[0] h = img.shape[1] Ra = np.zeros((w*h, 2)) for i in range(w): for j in range(h): R = math.sqrt((w/2-i)*(w/2-i) + (h/2-j)*(h/2-j)) Ra[i*h+j, 0] = R Ra[i*h+j, 1] = min(imgLab[i][j][1], a_threshold) Ra[:,0] /= max(Ra[:,0]) Ra[:,0] *= Ra_ratio Ra[:,1] /= max(Ra[:,1]) return Ra
def color_reduction(image, n_colors, method='kmeans', palette=None): """Reduce the number of colors in image to n_colors using method""" method = method.lower() if method not in ('kmeans', 'linear', 'max', 'median', 'octree'): method = 'kmeans' if n_colors < 2: n_colors = 2 elif n_colors > 128: n_colors = 128 if method == 'kmeans': n_clusters = n_colors h, w = image.shape[:2] img = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) img = img.reshape((-1, 3)) # -1 -> img.shape[0] * img.shape[1] centers, labels = kmeans(img, n_clusters) if palette is not None: # palette comes in RGB centers = cv2.cvtColor(np.array([palette]), cv2.COLOR_RGB2LAB)[0] quant = centers[labels].reshape((h, w, 3)) output = cv2.cvtColor(quant, cv2.COLOR_LAB2BGR) else: img = PIL.Image.fromarray(image[:, :, ::-1], mode='RGB') quant = img.quantize(colors=n_colors, method=get_quantize_method(method)) if palette is not None: palette = np.array(palette, dtype=np.uint8) quant.putpalette(palette.flatten()) output = np.array(quant.convert('RGB'), dtype=np.uint8)[:, :, ::-1] return output
def __MR_readimg(self,img): if isinstance(img,str): # a image path img = cv2.imread(img,cv2.CV_LOAD_IMAGE_COLOR) # img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB).astype(float)/255 # img = cv2.cvtColor(img,cv2.COLOR_BGR2LAB).astype(float)/255 img = cv2.cvtColor(img,cv2.COLOR_RGB2LAB).astype(float)/255 # h = 100 # w = int(float(h)/float(img.shape[0])*float(img.shape[1])) return img #cv2.resize(img,(w,h))