I have been trying to do data augmentation for image detection using deep learning framework.
I'm using Opencv3.3 in Python.
My framework is:
- Convert BGR to HSV
- Image transformation like (rotation, scaling, shearing, translation)
- Convert HSV to BGR
The original image is this first one and the result is the following after.
Seeing below, there is some stain onto the red bottle.
I did randomly sample within [-10, 10] for Hue, within [-80, 80] for saturation, and within [-40, 40] for value.
Additionally, following this link, I set my code like this.
class RandomHSV(object):
def __init__(self, hue = None, saturation = None, brightness = None):
if hue:
self.hue = hue
else:
self.hue = 0
if saturation:
self.saturation = saturation
else:
self.saturation = 0
if brightness:
self.brightness = brightness
else:
self.brightness = 0
if type(self.hue) != tuple:
self.hue = (-self.hue, self.hue)
if type(self.saturation) != tuple:
self.saturation = (-self.saturation, self.saturation)
if type(brightness) != tuple:
self.brightness = (-self.brightness, self.brightness)
def __call__(self, img, bboxes):
hue = random.randint(*self.hue)
saturation = random.randint(*self.saturation)
brightness = random.randint(*self.brightness)
img = img.astype(int)
a = np.array([hue, saturation, brightness]).astype(int)
img += np.reshape(a, (1,1,3))
img = np.clip(img, 0, 255)
img[:,:,0] = np.clip(img[:,:,0],0, 179)
img = img.astype(np.uint8)
return img, bboxes