|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Blob helper functions.""" |
|
|
|
import numpy as np |
|
import cv2 |
|
import torch |
|
import torch.nn as nn |
|
import random |
|
|
|
def chromatic_transform(im, label=None, d_h=None, d_s=None, d_l=None): |
|
""" |
|
Given an image array, add the hue, saturation and luminosity to the image |
|
""" |
|
|
|
if d_h is None: |
|
d_h = (np.random.rand(1) - 0.5) * 0.02 * 180 |
|
if d_l is None: |
|
d_l = (np.random.rand(1) - 0.5) * 0.2 * 256 |
|
if d_s is None: |
|
d_s = (np.random.rand(1) - 0.5) * 0.2 * 256 |
|
|
|
hls = cv2.cvtColor(im, cv2.COLOR_BGR2HLS) |
|
h, l, s = cv2.split(hls) |
|
|
|
new_h = (h + d_h) % 180 |
|
new_l = np.clip(l + d_l, 0, 255) |
|
new_s = np.clip(s + d_s, 0, 255) |
|
|
|
new_hls = cv2.merge((new_h, new_l, new_s)).astype('uint8') |
|
new_im = cv2.cvtColor(new_hls, cv2.COLOR_HLS2BGR) |
|
|
|
if label is not None: |
|
I = np.where(label > 0) |
|
new_im[I[0], I[1], :] = im[I[0], I[1], :] |
|
return new_im |
|
|
|
|
|
def add_noise(image): |
|
|
|
|
|
r = np.random.rand(1) |
|
|
|
|
|
if r < 0.9: |
|
row,col,ch= image.shape |
|
mean = 0 |
|
var = np.random.rand(1) * 0.3 * 256 |
|
sigma = var**0.5 |
|
gauss = sigma * np.random.randn(row,col) + mean |
|
gauss = np.repeat(gauss[:, :, np.newaxis], ch, axis=2) |
|
noisy = image + gauss |
|
noisy = np.clip(noisy, 0, 255) |
|
else: |
|
|
|
sizes = [3, 5, 7, 9, 11, 15] |
|
size = sizes[int(np.random.randint(len(sizes), size=1))] |
|
kernel_motion_blur = np.zeros((size, size)) |
|
if np.random.rand(1) < 0.5: |
|
kernel_motion_blur[int((size-1)/2), :] = np.ones(size) |
|
else: |
|
kernel_motion_blur[:, int((size-1)/2)] = np.ones(size) |
|
kernel_motion_blur = kernel_motion_blur / size |
|
noisy = cv2.filter2D(image, -1, kernel_motion_blur) |
|
|
|
return noisy |
|
|
|
|
|
def add_noise_depth(image, level = 0.01): |
|
row,col,ch= image.shape |
|
noise_level = random.uniform(0, level) |
|
gauss = noise_level * np.random.randn(row,col) |
|
gauss = np.repeat(gauss[:, :, np.newaxis], ch, axis=2) |
|
noisy = image + gauss |
|
return noisy |
|
|