def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) dtype = img.dtype # uint8 x = np.arange(0, 256, dtype=np.int16) lut_hue = ((x * r[0]) % 180).astype(dtype) lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) lut_val = np.clip(x * r[2], 0, 255).astype(dtype) img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
def adjust_gamma(image, gamma=1.0): invGamma = 1.0 / gamma table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8") return cv2.LUT(image, table)
def gm(img, gamma=1.0): table = [] for i in range(256): table.append(((i / 255)**gamma) * 255) table = np.array(table).astype('uint8') img_dark_new = cv2.LUT(img, table) return img_dark_new
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") return cv2.LUT(image, table)
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) dst = cv.LUT(img, gamma_table) # cv.imshow("1",dst) # cv.waitKey(0) # cv.destroyAllWindows() return dst
def correct_brightness(img, threshold=0.05, min_white=150, max_black=50, _debug=True): blurred = cv2.medianBlur(img, 3) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) # adaptive histogram equalization lab[:, :, 0] = cv2.createCLAHE(clipLimit=0.7, tileGridSize=(8, 8)).apply(lab[:, :, 0]) # calculate white and black point hist = calc_hist(lab[:, :, 0]) cutoff = np.max(hist) * threshold white = np.max(np.arange(256)[hist > cutoff]) + 10 white = max(white, min_white) black = np.min(np.arange(256)[hist > cutoff]) / 3 black = min(black, max_black) # adjust lightness val_black = np.arange(0, 256) - black val_white = val_black / (white - black) * 255 table = np.clip(val_white, 0, 255).astype(np.uint8) lab[:, :, 0] = cv2.LUT(lab[:, :, 0], table) # increase saturation sat = 127 * 0.15 # percent val_sat = (np.arange(0, 256) - sat) / (255 - 2 * sat) * 255 table = np.clip(val_sat, 0, 255).astype(np.uint8) lab[:, :, 1] = cv2.LUT(lab[:, :, 1], table) lab[:, :, 2] = cv2.LUT(lab[:, :, 2], table) output = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) if _debug: import matplotlib.pyplot as plt plt.figure() plt.plot(hist) plt.plot(calc_hist(cv2.cvtColor(output, cv2.COLOR_BGR2LAB)[:, :, 0])) show_images([img, output], show=False) return output
def c_hsv_np(img_np, h_gain=0.5, s_gain=0.5, v_gain=0.5): ''' 随机色域 is_safe :param img_np: :param h_gain: :param s_gain: :param v_gain: :return: ''' r = np.random.uniform(-1, 1, 3) * [h_gain, s_gain, v_gain] + 1 # 0.5~1.5之间 hue, sat, val = cv2.split(cv2.cvtColor(img_np, cv2.COLOR_BGR2HSV)) dtype = img_np.dtype # uint8 x = np.arange(0, 256, dtype=np.int16) lut_hue = ((x * r[0]) % 180).astype(dtype) lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) lut_val = np.clip(x * r[2], 0, 255).astype(dtype) img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img_np) # no return needed
def adjust_gamma(images, gamma=1.0): assert (len(images.shape) == 4) assert (images.shape[3] == 1) invGamma = 1.0 / gamma table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8") new_images = np.empty(images.shape) for i in range(images.shape[0]): new_images[i, :, :, 0] = cv2.LUT(np.array(images[i, :, :, 0], dtype = np.uint8), table) return new_images
def gamma(img: np.ndarray, gamma_value=1.0): """ use a lookup table to calculate new gamma value of pixels Args: img: image as numpy array gamma_value: factor to darken or lighten image > 1.0 lighten, <1.0 darken, =1.0 no changes Returns: image with adjusted gamma values """ inverted = 1.0 / gamma_value table = np.array([((i / 255.0)**inverted) * 255 for i in np.arange(0, 256)]).astype("uint8") return cv2.LUT(img, table)
def adjust_gamma(self, image, gamma=1.0): """ Image gamma correction Parameters ---------- image: ndim np.array image to be transformed by gamma correction gamma: float Returns ------- corrected image as np.array """ # 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)
#3. Escreva um algoritmo que leia a imagem abaixo, e então clareie somente as regiões mais escuras dela: from cv2 import cv2 import numpy as np img = cv2.imread("img1.jpg") gamma = 0.6 lookUpTable = np.empty((1,256), np.uint8) for i in range(256): lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255) res = cv2.LUT(img, lookUpTable) cv2.imshow("img original", img) cv2.imshow("res", res) cv2.waitKey(0)
from cv2 import cv2 as cv import numpy as np img = cv.imread("Practica/Input/lake.jpg", 1) #Ecualización Conjunta gris = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #conversion gris hist = cv.calcHist(images = [gris], channels = [0], mask = None, histSize = [256], ranges = [0, 256]) hist *= 255.0/cv.norm(hist, cv.NORM_L1) # MAT CV_8UC1 lut = np.zeros((1, 256), dtype = "uint8") acum = 0.0 for i in range (0,256): lut[0][i] = acum acum += hist[i] res = cv.LUT(img,lut) cv.imshow("Entrada", img) cv.imshow("Ecualizada", res) cv.waitKey(0)
def gamma_transform(img, gamma): gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(size)] gamma_table = np.round(np.array(gamma_table)).astype(np.uint8) return cv2.LUT(img, gamma_table)
def gamma(image, gamma=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 data_aug(self, data_dict): ### image data augmentation ### new_img = data_dict['Image'] angle_aug = random.random() * self.max_rot * 2 - self.max_rot scale_aug = random.random() * (self.max_scale - self.min_scale) + \ self.min_scale shift_aug_x = random.random() * \ (self.max_shift * self.posmap_size) * 2 \ - (self.max_shift * self.posmap_size) shift_aug_y = random.random() * \ (self.max_shift * self.posmap_size) * 2 \ - (self.max_shift * self.posmap_size) tform = tf.SimilarityTransform(scale=scale_aug, rotation=np.deg2rad(angle_aug), translation=(shift_aug_x, shift_aug_y)) shift_y, shift_x = np.array(new_img.shape[:2]) / 2. tf_shift = tf.SimilarityTransform(translation=[-shift_x, -shift_y]) tf_shift_inv = tf.SimilarityTransform(translation=[shift_x, shift_y]) new_img = tf.warp(new_img, (tf_shift + (tform + tf_shift_inv)).inverse) # fill blank border_value = np.mean(new_img[ :3, :], axis=(0, 1)) * 0.25 + \ np.mean(new_img[-3:, :], axis=(0, 1)) * 0.25 + \ np.mean(new_img[:, -3:], axis=(0, 1)) * 0.25 + \ np.mean(new_img[:, :3], axis=(0, 1)) * 0.25 mask = np.sum(new_img.reshape(-1, 3), axis=1) == 0 mask = mask[:, np.newaxis] mask = np.concatenate((mask, mask, mask), axis=1) border_value = np.repeat(border_value[np.newaxis, :], mask.shape[0], axis=0) border_value *= mask new_img = (new_img.reshape(-1, 3) + border_value) new_img = new_img.reshape(self.img_size, self.img_size, 3) new_img = (new_img * 255.).astype('uint8') # gamma correlation gamma = random.random() * (1.8 - 1.0) + 1.0 invGamma = 1.0 / gamma table = np.array([((i / 255.0)**invGamma) * 255 for i in np.arange(0, 256)]).astype(np.uint8) new_img = cv2.LUT(new_img, table) # noise noise_aug = random.random() * self.max_noise_var new_img = (skimage.util.random_noise( new_img, mode="gaussian", var=noise_aug) * 255).astype(np.uint8) # blur blur_aug = random.randint(self.min_blur_resize, self.img_size) new_img = cv2.resize(cv2.resize(new_img, (blur_aug, blur_aug)), (self.img_size, self.img_size)) # gray if random.random() < 0.2: new_img = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY) new_img = np.stack((new_img, ) * 3, axis=-1) data_dict['Image'] = new_img # aug posmap posmap = data_dict['Posmap'] vertices = np.reshape(posmap, [-1, 3]).T z = vertices[2, :].copy() / tform.params[0, 0] vertices[2, :] = 1 vertices = np.dot((tf_shift + (tform + tf_shift_inv)).params, vertices) vertices = np.vstack((vertices[:2, :], z)) posmap = np.reshape(vertices.T, [self.posmap_size, self.posmap_size, 3]) data_dict['Posmap'] = posmap return data_dict