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)
python类COLOR_LAB2BGR的实例源码
clahe.py 文件源码
项目:fully-convolutional-network-semantic-segmentation
作者: alecng94
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def __tiledoutput__(self, net_op, batch_size, num_cols=8, net_recon_const=None):
num_rows = np.int_(np.ceil((batch_size*1.)/num_cols))
out_img = np.zeros((num_rows*self.outshape[0], num_cols*self.outshape[1], 3), dtype='uint8')
img_lab = np.zeros((self.outshape[0], self.outshape[1], 3), dtype='uint8')
c = 0
r = 0
for i in range(batch_size):
if(i % num_cols == 0 and i > 0):
r = r + 1
c = 0
img_lab[..., 0] = self.__decodeimg__(net_recon_const[i, 0, :, :].reshape(\
self.outshape[0], self.outshape[1]))
img_lab[..., 1] = self.__decodeimg__(net_op[i, 0, :, :].reshape(\
self.shape[0], self.shape[1]))
img_lab[..., 2] = self.__decodeimg__(net_op[i, 1, :, :].reshape(\
self.shape[0], self.shape[1]))
img_rgb = cv2.cvtColor(img_lab, cv2.COLOR_LAB2BGR)
out_img[r*self.outshape[0]:(r+1)*self.outshape[0], \
c*self.outshape[1]:(c+1)*self.outshape[1], ...] = img_rgb
c = c+1
return out_img
def save_output(self, net_op, batch_size, num_cols=8, net_recon_const=None):
num_rows = np.int_(np.ceil((batch_size*1.)/num_cols))
out_img = np.zeros((num_rows*self.outshape[0], num_cols*self.outshape[1], 3), dtype='uint8')
img_lab = np.zeros((self.outshape[0], self.outshape[1], 3), dtype='uint8')
c = 0
r = 0
for i in range(batch_size):
if(i % num_cols == 0 and i > 0):
r = r + 1
c = 0
img_lab[..., 0] = self.__get_decoded_img(net_recon_const[i, ...].reshape(self.outshape[0], self.outshape[1]))
img_lab[..., 1] = self.__get_decoded_img(net_op[i, :np.prod(self.shape)].reshape(self.shape[0], self.shape[1]))
img_lab[..., 2] = self.__get_decoded_img(net_op[i, np.prod(self.shape):].reshape(self.shape[0], self.shape[1]))
img_rgb = cv2.cvtColor(img_lab, cv2.COLOR_LAB2BGR)
out_img[r*self.outshape[0]:(r+1)*self.outshape[0], c*self.outshape[1]:(c+1)*self.outshape[1], ...] = img_rgb
c = c+1
return out_img
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 visualisePCA(dstFolder):
meanIsomap = pickle.load(open(os.path.join(dstFolder, "meanIsomap.p"), "rb"))
pca = pickle.load(open(os.path.join(dstFolder, "pca.p"), "rb"))
for i in range(10):
for j in np.linspace(-1,1,3):
X = [0,]*10
X[i]=j*np.sqrt(pca.explained_variance_[i])
displayIm = getIsoMapFromPCA(pca, meanIsomap, X)
displayIm = cv2.cvtColor(displayIm[:, :, :3].astype(np.uint8), cv2.COLOR_LAB2BGR)
cv2.imshow("pca_{}".format(j), displayIm.astype(np.uint8))
# cv2.waitKey(-1)
isomaps = pickle.load(open(os.path.join(dstFolder, "0_isomaps_centered.p"), "rb"))
#play with variance
for i in range(isomaps.shape[0]):
isomap_just_color = isomaps[i, :, :, 1:3]
# flatten to data
X = isomap_just_color.reshape(1, -1)
isomap_pca_color = pca.transform(X)[0]
scale = 1
# for j in range(5):
new_isomap_pca_color = isomap_pca_color.copy()
# new_isomap_pca_color[j] += np.sqrt(pca.explained_variance_[j]) * scale
isomap_new_color = pca.inverse_transform(new_isomap_pca_color)
isomap_new_color = isomap_new_color.reshape(isomap_just_color.shape[0],isomap_just_color.shape[1],isomap_just_color.shape[2])
isomap_full = isomaps[i,:,:,:3].copy()
isomap_full[:,:,1:] = isomap_new_color
displayIm = isomaps[i, :, :, :3].copy() + meanIsomap[:, :, :3]
displayIm = cv2.cvtColor(displayIm[:, :, :3].astype(np.uint8), cv2.COLOR_LAB2BGR)
cv2.imshow("isomap_orig", displayIm.astype(np.uint8))
displayIm = isomap_full.copy() + meanIsomap[:, :, :3]
displayIm = cv2.cvtColor(displayIm[:, :, :3].astype(np.uint8), cv2.COLOR_LAB2BGR)
cv2.imshow("isomap_new", displayIm.astype(np.uint8))
cv2.waitKey(-1)
def save_divcolor(self, net_op, gt, epoch, itr_id, prefix, batch_size, imgname, num_cols=8, net_recon_const=None):
img_lab = np.zeros((self.outshape[0], self.outshape[1], 3), dtype='uint8')
img_lab_mat = np.zeros((self.shape[0], self.shape[1], 2), dtype='uint8')
if not os.path.exists('%s/%s' % (self.out_directory, imgname)):
os.makedirs('%s/%s' % (self.out_directory, imgname))
for i in range(batch_size):
img_lab[..., 0] = self.__get_decoded_img(net_recon_const[i, ...].reshape(self.outshape[0], self.outshape[1]))
img_lab[..., 1] = self.__get_decoded_img(net_op[i, :np.prod(self.shape)].reshape(self.shape[0], self.shape[1]))
img_lab[..., 2] = self.__get_decoded_img(net_op[i, np.prod(self.shape):].reshape(self.shape[0], self.shape[1]))
img_lab_mat[..., 0] = 128.*net_op[i, :np.prod(self.shape)].reshape(self.shape[0], self.shape[1])+128.
img_lab_mat[..., 1] = 128.*net_op[i, np.prod(self.shape):].reshape(self.shape[0], self.shape[1])+128.
img_rgb = cv2.cvtColor(img_lab, cv2.COLOR_LAB2BGR)
out_fn_pred = '%s/%s/%s_%03d.png' % (self.out_directory, imgname, prefix, i)
cv2.imwrite(out_fn_pred, img_rgb)
# out_fn_mat = '%s/%s/%s_%03d.mat' % (self.out_directory, imgname, prefix, i)
# np.save(out_fn_mat, img_lab_mat)
img_lab[..., 0] = self.__get_decoded_img(net_recon_const[i, ...].reshape(self.outshape[0], self.outshape[1]))
img_lab[..., 1] = self.__get_decoded_img(gt[0, :np.prod(self.shape)].reshape(self.shape[0], self.shape[1]))
img_lab[..., 2] = self.__get_decoded_img(gt[0, np.prod(self.shape):].reshape(self.shape[0], self.shape[1]))
img_lab_mat[..., 0] = 128.*gt[0, :np.prod(self.shape)].reshape(self.shape[0], self.shape[1])+128.
img_lab_mat[..., 1] = 128.*gt[0, np.prod(self.shape):].reshape(self.shape[0], self.shape[1])+128.
out_fn_pred = '%s/%s/gt.png' % (self.out_directory, imgname)
img_rgb = cv2.cvtColor(img_lab, cv2.COLOR_LAB2BGR)
cv2.imwrite(out_fn_pred, img_rgb)
# out_fn_mat = '%s/%s/gt.mat' % (self.out_directory, imgname)
# np.save(out_fn_mat, img_lab_mat)