我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用cv2.COLOR_BGR2LAB。
def enhance(image_path, clip_limit=3): image = cv2.imread(image_path) # convert image to LAB color model image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) # split the image into L, A, and B channels l_channel, a_channel, b_channel = cv2.split(image_lab) # apply CLAHE to lightness channel clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(8, 8)) cl = clahe.apply(l_channel) # merge the CLAHE enhanced L channel with the original A and B channel merged_channels = cv2.merge((cl, a_channel, b_channel)) # convert iamge from LAB color model back to RGB color model final_image = cv2.cvtColor(merged_channels, cv2.COLOR_LAB2BGR) return cv2_to_pil(final_image)
def get_data(image_id, a_size, m_size, p_size, sf): rgb_data = get_rgb_data(image_id) rgb_data = cv2.resize(rgb_data, (p_size*sf, p_size*sf), interpolation=cv2.INTER_LANCZOS4) # rgb_data = rgb_data.astype(np.float) / 2500. # print(np.max(rgb_data), np.mean(rgb_data)) # rgb_data[:, :, 0] = exposure.equalize_adapthist(rgb_data[:, :, 0], clip_limit=0.04) # rgb_data[:, :, 1] = exposure.equalize_adapthist(rgb_data[:, :, 1], clip_limit=0.04) # rgb_data[:, :, 2] = exposure.equalize_adapthist(rgb_data[:, :, 2], clip_limit=0.04) A_data = get_spectral_data(image_id, a_size*sf, a_size*sf, bands=['A']) M_data = get_spectral_data(image_id, m_size*sf, m_size*sf, bands=['M']) P_data = get_spectral_data(image_id, p_size*sf, p_size*sf, bands=['P']) # lab_data = cv2.cvtColor(rgb_data, cv2.COLOR_BGR2LAB) P_data = np.concatenate([rgb_data, P_data], axis=2) return A_data, M_data, P_data
def showImageLab(image_file): image_bgr = cv2.imread(image_file) image_Lab = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2LAB) L = image_Lab[:, :, 0] a = image_Lab[:, :, 1] b = image_Lab[:, :, 2] plt.subplot(1, 3, 1) plt.title('L') plt.gray() plt.imshow(L) plt.axis('off') plt.subplot(1, 3, 2) plt.title('a') plt.gray() plt.imshow(a) plt.axis('off') plt.subplot(1, 3, 3) plt.title('b') plt.gray() plt.imshow(b) plt.axis('off') plt.show()
def cross_sp_voting(image, segments, numSegments): hogfeats = [] rgb = [] lab = [] image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) for i in range(numSegments): temp_idx = np.where(segments == i) locations = zip(tuple(temp_idx[0]), tuple(idx[1])) hogfeats.append(compute_hog(image, locations)) rgb.append(image[temp_idx[0], temp_idx[1], :]) lab.append(image_lab[temp_idx[0], temp_idx[1], :])
def __getitem__(self, idx): color_ab = np.zeros((2, self.shape[0], self.shape[1]), dtype='f') weights = np.ones((2, self.shape[0], self.shape[1]), dtype='f') recon_const = np.zeros((1, self.shape[0], self.shape[1]), dtype='f') recon_const_outres = np.zeros((1, self.outshape[0], self.outshape[1]), dtype='f') greyfeats = np.zeros((512, 28, 28), dtype='f') img_large = cv2.imread(self.img_fns[idx]) if(self.shape is not None): img = cv2.resize(img_large, (self.shape[0], self.shape[1])) img_outres = cv2.resize(img_large, (self.outshape[0], self.outshape[1])) img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) img_lab_outres = cv2.cvtColor(img_outres, cv2.COLOR_BGR2LAB) img_lab = ((img_lab*2.)/255.)-1. img_lab_outres = ((img_lab_outres*2.)/255.)-1. recon_const[0, :, :] = img_lab[..., 0] recon_const_outres[0, :, :] = img_lab_outres[..., 0] color_ab[0, :, :] = img_lab[..., 1].reshape(1, self.shape[0], self.shape[1]) color_ab[1, :, :] = img_lab[..., 2].reshape(1, self.shape[0], self.shape[1]) if(self.lossweights is not None): weights = self.__getweights__(color_ab) featobj = np.load(self.feats_fns[idx]) greyfeats[:,:,:] = featobj['arr_0'] return color_ab, recon_const, weights, recon_const_outres, greyfeats
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 componentes(imagen): imagen = cv2.cvtColor(imagen, cv2.COLOR_BGR2LAB); imagen2 = imagen[:,:,1]; return imagen2;
def lab(img, op=None): return cv.cvtColor(img, cv.COLOR_BGR2LAB)
def train_next_batch(self, batch_size, nch): batch = np.zeros((batch_size, nch*np.prod(self.shape)), dtype='f') batch_lossweights = np.ones((batch_size, nch*np.prod(self.shape)), dtype='f') batch_recon_const = np.zeros((batch_size, np.prod(self.shape)), dtype='f') batch_recon_const_outres = np.zeros((batch_size, np.prod(self.outshape)), dtype='f') if(self.train_batch_head + batch_size >= len(self.train_img_fns)): self.train_shuff_ids = np.random.permutation(len(self.train_img_fns)) self.train_batch_head = 0 for i_n, i in enumerate(range(self.train_batch_head, self.train_batch_head+batch_size)): currid = self.train_shuff_ids[i] img_large = cv2.imread(self.train_img_fns[currid]) if(self.shape is not None): img = cv2.resize(img_large, (self.shape[0], self.shape[1])) img_outres = cv2.resize(img_large, (self.outshape[0], self.outshape[1])) img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) img_lab_outres = cv2.cvtColor(img_outres, cv2.COLOR_BGR2LAB) batch_recon_const[i_n, ...] = ((img_lab[..., 0].reshape(-1)*1.)-128.)/128. batch_recon_const_outres[i_n, ...] = ((img_lab_outres[..., 0].reshape(-1)*1.)-128.)/128. batch[i_n, ...] = np.concatenate((((img_lab[..., 1].reshape(-1)*1.)-128.)/128., ((img_lab[..., 2].reshape(-1)*1.)-128.)/128.), axis=0) if(self.lossweights is not None): batch_lossweights[i_n, ...] = self.__get_lossweights(batch[i_n, ...]) self.train_batch_head = self.train_batch_head + batch_size return batch, batch_recon_const, batch_lossweights, batch_recon_const_outres
def test_next_batch(self, batch_size, nch): batch = np.zeros((batch_size, nch*np.prod(self.shape)), dtype='f') batch_recon_const = np.zeros((batch_size, np.prod(self.shape)), dtype='f') batch_recon_const_outres = np.zeros((batch_size, np.prod(self.outshape)), dtype='f') batch_imgnames = [] if(self.test_batch_head + batch_size > len(self.test_img_fns)): self.test_batch_head = 0 for i_n, i in enumerate(range(self.test_batch_head, self.test_batch_head+batch_size)): if(i >= self.test_img_num): #Repeat first image to make up for incomplete last batch i = 0 currid = self.test_shuff_ids[i] img_large = cv2.imread(self.test_img_fns[currid]) batch_imgnames.append(self.test_img_fns[currid].split('/')[-1]) if(self.shape is not None): img = cv2.resize(img_large, (self.shape[1], self.shape[0])) img_outres = cv2.resize(img_large, (self.outshape[0], self.outshape[1])) img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) img_lab_outres = cv2.cvtColor(img_outres, cv2.COLOR_BGR2LAB) batch_recon_const[i_n, ...] = ((img_lab[..., 0].reshape(-1)*1.)-128.)/128. batch_recon_const_outres[i_n, ...] = ((img_lab_outres[..., 0].reshape(-1)*1.)-128.)/128. batch[i_n, ...] = np.concatenate((((img_lab[..., 1].reshape(-1)*1.)-128.)/128., ((img_lab[..., 2].reshape(-1)*1.)-128.)/128.), axis=0) self.test_batch_head = self.test_batch_head + batch_size return batch, batch_recon_const, batch_recon_const_outres, batch_imgnames
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))
def cvEdgeDetection(image_file): image_bgr = cv2.imread(image_file) image_Lab = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2LAB) L = image_Lab[:, :, 0] win_name = "CannyEdge" cv2.namedWindow(win_name, cv2.WINDOW_NORMAL) cv2.imshow(win_name, image_bgr) display_modes = ['Original', 'Gray', 'CannyEdge'] display_mode = display_modes[2] low_threshold_trackbar = CVTrackbar("LowThr", win_name, 0, 200) low_threshold_trackbar.setValue(20) upper_threshold_trackbar = CVTrackbar("UpperThr", win_name, 0, 300) upper_threshold_trackbar.setValue(40) while True: key = cv2.waitKey(30) & 0xFF # ????????? if key == ord('1'): display_mode = display_modes[0] cv2.imshow(win_name, image_bgr) elif key == ord('2'): display_mode = display_modes[1] cv2.imshow(win_name, L) elif key == ord('3'): display_mode = display_modes[2] elif key == ord('q'): break # Canny Edge?????????????? if display_mode == display_modes[2]: edge = cv2.Canny(L, low_threshold_trackbar.value(), upper_threshold_trackbar.value()) cv2.imshow(win_name, edge) cv2.destroyAllWindows() # Matplot + OpenCV?Trackbar?Canny Edge??????
def pltEdgeDetection(image_file): image_bgr = cv2.imread(image_file) image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) image_Lab = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2LAB) L = image_Lab[:, :, 0] win_name = "CannyEdge" cv2.namedWindow(win_name, cv2.WINDOW_NORMAL) low_threshold_trackbar = CVTrackbar("LowThr", win_name, 0, 200) low_threshold_trackbar.setValue(20) upper_threshold_trackbar = CVTrackbar("UpperThr", win_name, 0, 300) upper_threshold_trackbar.setValue(40) fig = plt.figure() fig.subplots_adjust( left=0.05, bottom=0.05, right=0.95, top=0.9, wspace=0.05, hspace=0.05) plt.subplot(1, 3, 1) plt.title('Original') plt.imshow(image_rgb) plt.axis('off') plt.subplot(1, 3, 2) plt.title('Gray') plt.gray() plt.imshow(L) plt.axis('off') plt.subplot(1, 3, 3) plt.title('CannyEdge') plt.gray() edge = cv2.Canny(L, low_threshold_trackbar.value(), upper_threshold_trackbar.value()) edge_plt = plt.imshow(edge) plt.axis('off') # Canny Edge?????????????? def updateValue(x): edge = cv2.Canny(L, low_threshold_trackbar.value(), upper_threshold_trackbar.value()) edge_plt.set_array(edge) fig.canvas.draw_idle() low_threshold_trackbar.setCallBack(updateValue) upper_threshold_trackbar.setCallBack(updateValue) plt.show() cv2.destroyAllWindows()