(
        img,
        brightness_delta=32,
        contrast_low=0.5, contrast_high=1.5,
        saturation_low=0.5, saturation_high=1.5,
        hue_delta=18)
return cv_img.astype(np.float32).transpose((2, 0, 1))[::-1]
A color related data augmentation used in SSD.

    This function is a combination of four augmentation methods:
    brightness, contrast, saturation and hue.

    * brightness: Adding a random offset to the intensity of the image.
    * contrast: Multiplying the intensity of the image by a random scale.
    * saturation: Multiplying the saturation of the image by a random scale.
    * hue: Adding a random offset to the hue of the image randomly.

    This data augmentation is used in training of
    Single Shot Multibox Detector [#]_.

    Note that this function requires :mod:`cv2`.

    .. [#] Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy,
       Scott Reed, Cheng-Yang Fu, Alexander C. Berg.
       SSD: Single Shot MultiBox Detector. ECCV 2016.

    Args:
        img (~numpy.ndarray): An image array to be augmented. This is in
            CHW and RGB format.
        brightness_delta (float): The offset for saturation will be
            drawn from :math:`[-brightness\_delta, brightness\_delta]`.
            The default value is :obj:`32`.
        contrast_low (float): The scale for contrast will be
            drawn from :math:`[contrast\_low, contrast\_high]`.
            The default value is :obj:`0.5`.
        contrast_high (float): See :obj:`contrast_low`.
            The default value is :obj:`1.5`.
        saturation_low (float): The scale for saturation will be
            drawn from :math:`[saturation\_low, saturation\_high]`.
            The default value is :obj:`0.5`.
        saturation_high (float): See :obj:`saturation_low`.
            The default value is :obj:`1.5`.
        hue_delta (float): The offset for hue will be
            drawn from :math:`[-hue\_delta, hue\_delta]`.
            The default value is :obj:`18`.

    Returns:
        An image in CHW and RGB format.
A color related data augmentation used in SSD.
A color related data augmentation used in SSD .
def random_distort(
        img,
        brightness_delta=32,
        contrast_low=0.5, contrast_high=1.5,
        saturation_low=0.5, saturation_high=1.5,
        hue_delta=18):
    """A color related data augmentation used in SSD.

    This function is a combination of four augmentation methods:
    brightness, contrast, saturation and hue.

    * brightness: Adding a random offset to the intensity of the image.
    * contrast: Multiplying the intensity of the image by a random scale.
    * saturation: Multiplying the saturation of the image by a random scale.
    * hue: Adding a random offset to the hue of the image randomly.

    This data augmentation is used in training of
    Single Shot Multibox Detector [#]_.

    Note that this function requires :mod:`cv2`.

    .. [#] Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy,
       Scott Reed, Cheng-Yang Fu, Alexander C. Berg.
       SSD: Single Shot MultiBox Detector. ECCV 2016.

    Args:
        img (~numpy.ndarray): An image array to be augmented. This is in
            CHW and RGB format.
        brightness_delta (float): The offset for saturation will be
            drawn from :math:`[-brightness\_delta, brightness\_delta]`.
            The default value is :obj:`32`.
        contrast_low (float): The scale for contrast will be
            drawn from :math:`[contrast\_low, contrast\_high]`.
            The default value is :obj:`0.5`.
        contrast_high (float): See :obj:`contrast_low`.
            The default value is :obj:`1.5`.
        saturation_low (float): The scale for saturation will be
            drawn from :math:`[saturation\_low, saturation\_high]`.
            The default value is :obj:`0.5`.
        saturation_high (float): See :obj:`saturation_low`.
            The default value is :obj:`1.5`.
        hue_delta (float): The offset for hue will be
            drawn from :math:`[-hue\_delta, hue\_delta]`.
            The default value is :obj:`18`.

    Returns:
        An image in CHW and RGB format.

    """
    import cv2

    cv_img = img[::-1].transpose((1, 2, 0)).astype(np.uint8)

    def convert(img, alpha=1, beta=0):
        img = img.astype(float) * alpha + beta
        img[img < 0] = 0
        img[img > 255] = 255
        return img.astype(np.uint8)

    def brightness(cv_img, delta):
        if random.randrange(2):
            return convert(
                cv_img,
                beta=random.uniform(-delta, delta))
        else:
            return cv_img

    def contrast(cv_img, low, high):
        if random.randrange(2):
            return convert(
                cv_img,
                alpha=random.uniform(low, high))
        else:
            return cv_img

    def saturation(cv_img, low, high):
        if random.randrange(2):
            cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2HSV)
            cv_img[:, :, 1] = convert(
                cv_img[:, :, 1],
                alpha=random.uniform(low, high))
            return cv2.cvtColor(cv_img, cv2.COLOR_HSV2BGR)
        else:
            return cv_img

    def hue(cv_img, delta):
        if random.randrange(2):
            cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2HSV)
            cv_img[:, :, 0] = (
                cv_img[:, :, 0].astype(int) +
                random.randint(-delta, delta)) % 180
            return cv2.cvtColor(cv_img, cv2.COLOR_HSV2BGR)
        else:
            return cv_img

    cv_img = brightness(cv_img, brightness_delta)

    if random.randrange(2):
        cv_img = contrast(cv_img, contrast_low, contrast_high)
        cv_img = saturation(cv_img, saturation_low, saturation_high)
        cv_img = hue(cv_img, hue_delta)
    else:
        cv_img = saturation(cv_img, saturation_low, saturation_high)
        cv_img = hue(cv_img, hue_delta)
        cv_img = contrast(cv_img, contrast_low, contrast_high)

    return cv_img.astype(np.float32).transpose((2, 0, 1))[::-1]
def random_distort ( img , brightness_delta = 32 , contrast_low = 0.5 , contrast_high = 1.5 , saturation_low = 0.5 , saturation_high = 1.5 , hue_delta = 18 ) : import cv2 cv_img = img [ : : - 1 ] . transpose ( ( 1 , 2 , 0 ) ) . astype ( np . uint8 ) def convert ( img , alpha = 1 , beta = 0 ) : img = img . astype ( float ) * alpha + beta img [ img < 0 ] = 0 img [ img > 255 ] = 255 return img . astype ( np . uint8 ) def brightness ( cv_img , delta ) : if random . randrange ( 2 ) : return convert ( cv_img , beta = random . uniform ( - delta , delta ) ) else : return cv_img def contrast ( cv_img , low , high ) : if random . randrange ( 2 ) : return convert ( cv_img , alpha = random . uniform ( low , high ) ) else : return cv_img def saturation ( cv_img , low , high ) : if random . randrange ( 2 ) : cv_img = cv2 . cvtColor ( cv_img , cv2 . COLOR_BGR2HSV ) cv_img [ : , : , 1 ] = convert ( cv_img [ : , : , 1 ] , alpha = random . uniform ( low , high ) ) return cv2 . cvtColor ( cv_img , cv2 . COLOR_HSV2BGR ) else : return cv_img def hue ( cv_img , delta ) : if random . randrange ( 2 ) : cv_img = cv2 . cvtColor ( cv_img , cv2 . COLOR_BGR2HSV ) cv_img [ : , : , 0 ] = ( cv_img [ : , : , 0 ] . astype ( int ) + random . randint ( - delta , delta ) ) % 180 return cv2 . cvtColor ( cv_img , cv2 . COLOR_HSV2BGR ) else : return cv_img cv_img = brightness ( cv_img , brightness_delta ) if random . randrange ( 2 ) : cv_img = contrast ( cv_img , contrast_low , contrast_high ) cv_img = saturation ( cv_img , saturation_low , saturation_high ) cv_img = hue ( cv_img , hue_delta ) else : cv_img = saturation ( cv_img , saturation_low , saturation_high ) cv_img = hue ( cv_img , hue_delta ) cv_img = contrast ( cv_img , contrast_low , contrast_high ) return cv_img . astype ( np . float32 ) . transpose ( ( 2 , 0 , 1 ) ) [ : : - 1 ]