def gaussian_blur(image, sigmax, sigmay=None, meanx=0, meany=0, theta=0, mask_type=None, **kwargs): """ Blurring the image with a Gaussian kernel. """ image, max_pixel = im2double_max(image) image_org = image.copy() image_mask = create_mask_image(image, mask_type, **kwargs) g_kernel = gaussian_kernel2(sigmax=sigmax, sigmay=sigmay, meanx=meanx, meany=meany, theta=theta) image_blur = cv2.filter2D(image, -1, g_kernel) output = image_org * image_mask + image_blur * (1 - image_mask) output *= max_pixel return output
def keep_blue_channel(image, amount): assert (amount >= 0.0), 'amount too low.' assert (amount <= 1.0), 'amount too high.' output, max_pixel = im2double_max(image) output[:, :, 0] *= amount output[:, :, 1] *= amount output *= max_pixel return output
def random_occlusion(image, object_instances=1, object_ratio=0.05): output, max_pixel = im2double_max(image) (rows, cols, chns) = output.shape extent = (round(rows * object_ratio), round(cols * object_ratio)) for i in range(object_instances): rand_row = random.randint(0 + extent[0], rows - extent[0]) rand_col = random.randint(0 + extent[1], cols - extent[1]) start = (rand_row, rand_col) # FIXME: if backend shape is different (rr, cc) = rectangle(start, extent=extent, shape=output.shape[0:2]) output[np.int64(rr), np.int64(cc), :] = 1 output *= max_pixel return output
def local_std(image, window_size=(5, 5)): """ Computing the local standard deviation of an image. """ image, max_pixel = im2double_max(image) npixels = window_size[0] * window_size[1] kernel = np.ones(window_size, np.float32) / npixels # TODO: consider different border treatment avg_image = cv2.filter2D(image, -1, kernel) std_image = cv2.filter2D((image - avg_image)**2, -1, kernel)**0.5 std_image *= max_pixel avg_image *= max_pixel return std_image, avg_image
def adjust_gamma(image, amount, pixel_variatoin=0, mask_type=None, **kwargs): amount = np.array(amount) image, max_pixel = im2double_max(image) image_org = image.copy() image_mask = create_mask_image(image, mask_type, **kwargs) min_gamma = amount - pixel_variatoin max_gamma = amount + pixel_variatoin gamma_mat = np.random.uniform(low=min_gamma, high=max_gamma, size=image.shape) image_gamma = image**gamma_mat output = image_org * image_mask + image_gamma * (1 - image_mask) output *= max_pixel return output
def poisson_noise(image, seed=None, clip=True, mask_type=None, eq_chns=False, **kwargs): image, max_pixel = im2double_max(image) image_org = image.copy() image_mask = create_mask_image(image, mask_type, **kwargs) image_noise = random_noise(image, mode='poisson', seed=seed, clip=clip, eq_chns=eq_chns) output = image_org * image_mask + image_noise * (1 - image_mask) output *= max_pixel return output
def adjust_illuminant(image, illuminant, pixel_variatoin=0, mask_type=None, **kwargs): image, max_pixel = im2double_max(image) image_org = image.copy() image_mask = create_mask_image(image, mask_type, **kwargs) for i in range(image.shape[2]): min_illuminant = illuminant[i] - pixel_variatoin max_illuminant = illuminant[i] + pixel_variatoin illuminant_i = np.random.uniform(low=min_illuminant, high=max_illuminant, size=image[:, :, i].shape) image[:, :, i] = image[:, :, i] * illuminant_i output = image_org * image_mask + image * (1 - image_mask) output *= max_pixel return output
def adjust_contrast(image, amount, pixel_variatoin=0, mask_type=None, **kwargs): """Return the image scaled to a certain contrast level in [0, 1]. parameters: - image: a numpy.ndarray - contrast_level: a scalar or array corresponding to each channel in range [0, 1]; with 1 -> full contrast """ amount = np.array(amount) assert np.all(amount >= 0.0), 'contrast_level too low.' assert np.all(amount <= 1.0), 'contrast_level too high.' is_uint8 = image.dtype == 'uint8' image, max_pixel = im2double_max(image) image_org = image.copy() image_mask = create_mask_image(image, mask_type, **kwargs) min_contrast = amount - pixel_variatoin max_contrast = amount + pixel_variatoin contrast_mat = np.random.uniform(low=min_contrast, high=max_contrast, size=image.shape) image_contrast = (1 - contrast_mat) / 2.0 + np.multiply( image, contrast_mat) output = image_org * image_mask + image_contrast * (1 - image_mask) output *= max_pixel if is_uint8: output = output.astype('uint8') return output
def s_p_noise(image, amount, salt_vs_pepper=0.5, seed=None, clip=True, mask_type=None, eq_chns=False, **kwargs): image, max_pixel = im2double_max(image) image_org = image.copy() image_mask = create_mask_image(image, mask_type, **kwargs) image_noise = random_noise(image, mode='s&p', seed=seed, clip=clip, salt_vs_pepper=salt_vs_pepper, amount=amount, eq_chns=eq_chns) output = image_org * image_mask + image_noise * (1 - image_mask) output *= max_pixel return output