Пример #1
0
 def test_img_transforms(self):
     x = np.random.random((3, 200, 200))
     _ = preprocessing_image.random_rotation(x, 20)
     _ = preprocessing_image.random_shift(x, 0.2, 0.2)
     _ = preprocessing_image.random_shear(x, 2.)
     _ = preprocessing_image.random_zoom(x, (0.5, 0.5))
     _ = preprocessing_image.apply_channel_shift(x, 2, 2)
     _ = preprocessing_image.apply_affine_transform(x, 2)
     with self.assertRaises(ValueError):
         preprocessing_image.random_zoom(x, (0, 0, 0))
     _ = preprocessing_image.random_channel_shift(x, 2.)
    def _apply_transform(self, x, transform_parameters):
        """Applies a transformation to an image according to given parameters.
        # Arguments
            x: 3D tensor, single image.
            transform_parameters: Dictionary with string - parameter pairs
                describing the transformation.
                Currently, the following parameters
                from the dictionary are used:
                - `'theta'`: Float. Rotation angle in degrees.
                - `'tx'`: Float. Shift in the x direction.
                - `'ty'`: Float. Shift in the y direction.
                - `'shear'`: Float. Shear angle in degrees.
                - `'zx'`: Float. Zoom in the x direction.
                - `'zy'`: Float. Zoom in the y direction.
                - `'flip_horizontal'`: Boolean. Horizontal flip.
                - `'flip_vertical'`: Boolean. Vertical flip.
                - `'channel_shift_intensity'`: Float. Channel shift intensity.
                - `'brightness'`: Float. Brightness shift intensity.
        # Returns
            A transformed version of the input (same shape).
        """
        # x is a single image, so it doesn't have image number at index 0
        img_row_axis = self.row_axis - 1
        img_col_axis = self.col_axis - 1
        img_channel_axis = self.channel_axis - 1

        x = apply_affine_transform(x,
                                   transform_parameters.get('theta', 0),
                                   transform_parameters.get('tx', 0),
                                   transform_parameters.get('ty', 0),
                                   transform_parameters.get('shear', 0),
                                   transform_parameters.get('zx', 1),
                                   transform_parameters.get('zy', 1),
                                   row_axis=img_row_axis,
                                   col_axis=img_col_axis,
                                   channel_axis=img_channel_axis,
                                   fill_mode=self.fill_mode,
                                   cval=self.cval)

        if transform_parameters.get('channel_shift_intensity') is not None:
            x = apply_channel_shift(
                x, transform_parameters['channel_shift_intensity'],
                img_channel_axis)

        if transform_parameters.get('flip_horizontal', False):
            x = self._flip_axis(x, img_col_axis)

        if transform_parameters.get('flip_vertical', False):
            x = self._flip_axis(x, img_row_axis)

        if transform_parameters.get('brightness') is not None:
            x = apply_brightness_shift(x, transform_parameters['brightness'])

        return x
Пример #3
0
    def apply_transform(self, x, y, transform_parameters, cval=255., label_cval=0.):
        """Applies a transformation to an image according to given parameters.

        # Arguments
            x: 3D tensor, single image.
            y: 2D tensor, single label
            transform_parameters: Dictionary with string - parameter pairs
                describing the transformation.

        # Returns
            A transformed version of the input (same shape).
        """
        x, y = my_apply_affine_transform(x, y, transform_parameters.get("theta", 0),
                                         transform_parameters.get("tx", 0),
                                         transform_parameters.get("ty", 0),
                                         transform_parameters.get("zx", 1),
                                         transform_parameters.get("zy", 1),
                                         cval=cval, label_cval=label_cval)
        if y.ndim==3:
            y = y[:, :, 0]
        if transform_parameters.get('channel_shift_intensity') is not None:
            x = apply_channel_shift(x,
                                    transform_parameters['channel_shift_intensity'],
                                    self.channel_axis)

        if transform_parameters.get('flip_horizontal', False):
            x = flip_axis(x, self.col_axis)
            y = flip_axis(y, self.col_axis)

        if transform_parameters.get('flip_vertical', False):
            x = flip_axis(x, self.row_axis)
            y = flip_axis(y, self.row_axis)

        if transform_parameters.get('brightness') is not None:
            x = apply_brightness_shift(x, transform_parameters['brightness'])

        return x, y
Пример #4
0
    channel_shift = uniform(-0.1, 0.1)
    # print('angle = '+str(angle))
    # print('shear = ' + str(shear))
    # print('zoom = ' + str(zoom))
    # print('channel shift = ' + str(channel_shift))

    # print(roidb[i]['image'])
    if i%10:
        print(str(i)+'/'+str(len(roidb)))

    img = cv2.imread(os.path.join(input_dir, os.path.basename(roidb[i]['image'])))

    # Debug border
    img = cv2.rectangle(img, (0, 0), (img.shape[1], img.shape[0]), (255, 0, 0), 3)

    img = image.apply_channel_shift(img, channel_shift).astype('uint8')
    h, w, _ = img.shape
    R = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1)
    R = np.vstack((R, [0, 0, 1]))

    cos = np.abs(R[0, 0])
    sin = np.abs(R[0, 1])

    nW = int(((h * sin) + (w * cos)))
    nH = int(((h * cos) + (w * sin)))

    R[0, 2] += (nW / 2) - w / 2 + int(-np.sin(np.deg2rad(shear)) * w)
    R[1, 2] += (nH / 2) - h / 2 + int(np.sin(np.deg2rad(shear)) * h)
    # R[0, 2] *= zoom
    # R[1, 2] *= zoom
Пример #5
0
def prep_im_for_blob(im,
                     pixel_means,
                     target_size,
                     max_size,
                     angle=0,
                     shear=0,
                     channel_shift=0):
    """Prepare an image for use as a network input blob. Specially:
      - Subtract per-channel pixel mean
      - Convert to float32
      - Rescale to each of the specified target size (capped at max_size)
    Returns a list of transformed images, one for each target size. Also returns
    the scale factors that were used to compute each returned image.
    """
    im = im.astype(np.float32, copy=False)
    im -= pixel_means
    im = image.apply_channel_shift(im, channel_shift).astype('uint8')

    h, w, _ = im.shape
    rotation_matrix = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1)
    rotation_matrix = np.vstack((rotation_matrix, [0, 0, 1]))

    cos = np.abs(rotation_matrix[0, 0])
    sin = np.abs(rotation_matrix[0, 1])

    nw = int((h * sin) + (w * cos))
    nh = int((h * cos) + (w * sin))

    rotation_matrix[0, 2] += (nw / 2) - w / 2 + int(
        -np.sin(np.deg2rad(shear)) * w)
    rotation_matrix[1, 2] += (nh / 2) - h / 2 + int(
        np.sin(np.deg2rad(shear)) * h)
    # rotation_matrix[0, 2] *= zoom
    # rotation_matrix[1, 2] *= zoom

    nw += int(-np.sin(np.deg2rad(shear)) * nw)
    nh += int(np.sin(np.deg2rad(shear)) * nh)

    # nw = int(nw * zoom)
    # nh = int(nh * zoom)

    # im_shape = (nw, nh)
    im_size_min = np.min([nw, nh])
    im_size_max = np.max([nw, nh])
    im_scale = float(target_size) / float(im_size_min)
    # Prevent the biggest axis from being more than max_size
    if np.round(im_scale * im_size_max) > max_size:
        im_scale = float(max_size) / float(im_size_max)

    rotation_matrix[0, 2] *= im_scale
    rotation_matrix[1, 2] *= im_scale

    nw = int(nw * im_scale)
    nh = int(nh * im_scale)

    scale_x_matrix = np.asarray([[im_scale, 0, 0], [0, 1, 0], [0, 0, 1]])
    scale_y_matrix = np.asarray([[1, 0, 0], [0, im_scale, 0], [0, 0, 1]])

    scale_matrix = np.dot(scale_x_matrix, scale_y_matrix)
    # transformation_matrix = np.dot(rotation_matrix, scale_matrix)

    shear_matrix = np.asarray([[1, -np.sin(shear), 0], [0, np.cos(shear), 0],
                               [0, 0, 1]])

    # zoom_matrix = np.asarray([[zoom, 0, 0],
    #                 [0, zoom, 0],
    #                 [0, 0, 1]])

    transformation_matrix = np.dot(rotation_matrix, shear_matrix)
    transformation_matrix = np.dot(transformation_matrix, scale_matrix)

    transformed_image = cv2.warpPerspective(im,
                                            transformation_matrix, (nw, nh),
                                            borderMode=cv2.BORDER_REPLICATE)

    # transformation_matrix = np.dot(rotation_matrix, shear_matrix)

    # transformed_image = cv2.resize(
    #     transformed_image,
    #     None,
    #     None,
    #     fx=im_scale,
    #     fy=im_scale,
    #     interpolation=cv2.INTER_LINEAR
    # )
    return transformed_image, transformation_matrix, transformed_image.shape, im_scale