def test_sun_rgbd():
from pybot.vision.image_utils import to_color
from pybot.vision.imshow_utils import imshow_cv
from pybot.utils.io_utils import write_video
from pybot.vision.color_utils import colormap
directory = '/media/HD1/data/SUNRGBD/'
dataset = SUNRGBDDataset(directory)
colors = cv2.imread('data/sun3d/sun.png').astype(np.uint8)
for (rgb, depth, label) in dataset.segmentationdb(None):
cout = np.dstack([label, label, label])
colored = cv2.LUT(cout, colors)
cdepth = colormap(depth / 64000.0)
for j in range(5):
write_video('xtion.avi', np.hstack([rgb, cdepth, colored]))
# for f in dataset.iteritems(every_k_frames=5):
# # vis = rgbd_data_uw.annotate(f)
# imshow_cv('frame', f.img, text='Image')
# imshow_cv('depth', (f.depth / 16).astype(np.uint8), text='Depth')
# imshow_cv('instance', (f.instance).astype(np.uint8), text='Instance')
# imshow_cv('label', (f.label).astype(np.uint8), text='Label')
# cv2.waitKey(100)
return dataset
python类LUT的实例源码
def adjust_gamma(image, gamma=1.0):
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(image, table)
#Random noize effect
def adjust_gamma(self, image, gamma=1.0):
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(image, table)
def applyColorMap(gray, cmap='flame'):
'''
like cv2.applyColorMap(im_gray, cv2.COLORMAP_*) but with different color maps
'''
# TODO:implement more cmaps
if cmap != 'flame':
raise NotImplemented
# TODO: make better
mx = 256 # if gray.dtype==np.uint8 else 65535
lut = np.empty(shape=(256, 3))
cmap = (
# taken from pyqtgraph GradientEditorItem
(0, (0, 0, 0)),
(0.2, (7, 0, 220)),
(0.5, (236, 0, 134)),
(0.8, (246, 246, 0)),
(1.0, (255, 255, 255))
)
# build lookup table:
lastval, lastcol = cmap[0]
for step, col in cmap[1:]:
val = int(step * mx)
for i in range(3):
lut[lastval:val, i] = np.linspace(
lastcol[i], col[i], val - lastval)
lastcol = col
lastval = val
s0, s1 = gray.shape
out = np.empty(shape=(s0, s1, 3), dtype=np.uint8)
for i in range(3):
out[..., i] = cv2.LUT(gray, lut[:, i])
return out
def adjust_gamma(img, gamma=1.0):
assert (img.shape[0] == 1)
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) *
255 for i in np.arange(0, 256)]).astype("uint8")
new_img = cv2.LUT(np.array(img, dtype=np.uint8), table)
return new_img
def adjust_gamma(image, gamma=1.0):
"""
http://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/
"""
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(image, table)
image_augmentation.py 文件源码
项目:dlcv_for_beginners
作者: frombeijingwithlove
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def gamma_transform(img, gamma):
gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
return cv2.LUT(img, gamma_table)
def adjust_gamma(image, gamma=1.0):
"""Adjust gamma value of image
Keyword arguments:
imgs_0 -- first image of image pair (with length of bath size)
gamma -- gamma value (default = 1.0)
"""
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(image, table)
def adjust_gamma(imgs, gamma=1.0):
assert (len(imgs.shape)==4) #4D arrays
assert (imgs.shape[1]==1) #check the channel is 1
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
new_imgs = np.empty(imgs.shape)
for i in range(imgs.shape[0]):
new_imgs[i,0] = cv2.LUT(np.array(imgs[i,0], dtype = np.uint8), table)
return new_imgs
def adjust_gamma(imgs, gamma=1.0):
assert (len(imgs.shape)==4) #4D arrays
assert (imgs.shape[1]==1) #check the channel is 1
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
new_imgs = np.empty(imgs.shape)
for i in range(imgs.shape[0]):
new_imgs[i,0] = cv2.LUT(np.array(imgs[i,0], dtype = np.uint8), table)
return new_imgs
def adjust_gamma(imgs, gamma=1.0):
assert (len(imgs.shape)==4) #4D arrays
assert (imgs.shape[1]==1) #check the channel is 1
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
new_imgs = np.empty(imgs.shape)
for i in range(imgs.shape[0]):
new_imgs[i,0] = cv2.LUT(np.array(imgs[i,0], dtype = np.uint8), table)
return new_imgs
def adjust_gamma(imgs, gamma=1.0):
assert (len(imgs.shape)==4) #4D arrays
assert (imgs.shape[1]==1) #check the channel is 1
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
new_imgs = np.empty(imgs.shape)
for i in range(imgs.shape[0]):
new_imgs[i,0] = cv2.LUT(np.array(imgs[i,0], dtype = np.uint8), table)
return new_imgs
def adjust_gamma(imgs, gamma=1.0):
assert (len(imgs.shape)==4) #4D arrays
assert (imgs.shape[1]==1) #check the channel is 1
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
new_imgs = np.empty(imgs.shape)
for i in range(imgs.shape[0]):
new_imgs[i,0] = cv2.LUT(np.array(imgs[i,0], dtype = np.uint8), table)
return new_imgs
def equalize_hist(image):
hist, bins = np.histogram(image.flatten(), 256, [0, 256])
cdf = hist.cumsum() # ???????
cdf_m = np.ma.masked_equal(cdf, 0) # ???????0?
cdf_m = (cdf_m - cdf_m.min()) * 255 / (cdf_m.max() - cdf_m.min()) # ????????lut[i] = int(255.0 *p[i])??
cdf = np.ma.filled(cdf_m, 0).astype('uint8') # ???????????0
return cv2.LUT(image, cdf)
def _augment(self, img, gamma):
lut = ((np.arange(256, dtype='float32') / 255) ** (1. / (1. + gamma)) * 255).astype('uint8')
img = np.clip(img, 0, 255).astype('uint8')
img = cv2.LUT(img, lut).astype('float32')
return img
def render(self, img_rgb):
# warming filter: increase red, decrease blue
c_r, c_g, c_b = cv2.split(img_rgb)
c_r = cv2.LUT(c_r, self.incr_ch_lut).astype(np.uint8)
c_b = cv2.LUT(c_b, self.decr_ch_lut).astype(np.uint8)
img_rgb = cv2.merge((c_r, c_g, c_b))
# increase color saturation
c_h, c_s, c_v = cv2.split(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV))
c_s = cv2.LUT(c_s, self.incr_ch_lut).astype(np.uint8)
return cv2.cvtColor(cv2.merge((c_h, c_s, c_v)), cv2.COLOR_HSV2RGB)
def render(self, img_rgb):
# cooling filter: increase blue, decrease red
c_r, c_g, c_b = cv2.split(img_rgb)
c_r = cv2.LUT(c_r, self.decr_ch_lut).astype(np.uint8)
c_b = cv2.LUT(c_b, self.incr_ch_lut).astype(np.uint8)
img_rgb = cv2.merge((c_r, c_g, c_b))
# decrease color saturation
c_h, c_s, c_v = cv2.split(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV))
c_s = cv2.LUT(c_s, self.decr_ch_lut).astype(np.uint8)
return cv2.cvtColor(cv2.merge((c_h, c_s, c_v)), cv2.COLOR_HSV2RGB)
def random_brightness(image, alpha=2.0):
gamma = np.random.rand() * alpha
gf = [[255 * pow(i/255, 1/gamma)] for i in range(256)]
table = np.reshape(gf, (256, -1))
image = cv2.LUT(image, table)
return image