Esempio n. 1
0
def test_affine_estimation():
    # exact solution
    tform = estimate_transform('affine', SRC[:3, :], DST[:3, :])
    assert_almost_equal(tform(SRC[:3, :]), DST[:3, :])

    # over-determined
    tform2 = estimate_transform('affine', SRC, DST)
    assert_almost_equal(tform2.inverse(tform2(SRC)), SRC)

    # via estimate method
    tform3 = AffineTransform()
    tform3.estimate(SRC, DST)
    assert_almost_equal(tform3.params, tform2.params)
def test_affine_estimation():
    # exact solution
    tform = estimate_transform('affine', SRC[:3, :], DST[:3, :])
    assert_array_almost_equal(tform(SRC[:3, :]), DST[:3, :])

    # over-determined
    tform2 = estimate_transform('affine', SRC, DST)
    assert_array_almost_equal(tform2.inverse(tform2(SRC)), SRC)

    # via estimate method
    tform3 = AffineTransform()
    tform3.estimate(SRC, DST)
    assert_array_almost_equal(tform3._matrix, tform2._matrix)
Esempio n. 3
0
def align_image(anchor_mask, movable_mask, movable_image, rescale=False):
    if rescale:
        scale = max(1, (movable_mask.sum() / anchor_mask.sum() * 2 + 1) / 3)
        movable_mask = warp(movable_mask, AffineTransform(scale=(scale, scale)))
        movable_image = warp(movable_image, AffineTransform(scale=(scale, scale)))

    center_anchor = get_mask_center(anchor_mask)
    center_movable = get_mask_center(movable_mask)
    diff = center_movable - center_anchor
    # image coordinates use (col, row) order
    diff = diff[::-1]
    result_mask = warp(movable_mask, AffineTransform(translation=diff))
    result_image = warp(movable_image, AffineTransform(translation=diff))
    return result_mask, result_image
Esempio n. 4
0
def Ransac(observation):
    from skimage import transform
    from skimage.transform import AffineTransform
    from skimage.measure import ransac

    ## Estimate affin transformation without RANSAC
    model = AffineTransform()
    model.estimate(src=observation[:, 2:4], dst=observation[:, 0:2])
    # print("model.params=\n",model.params)
    nbPts = np.shape(observation)[0]
    B = []
    for i in range(nbPts):
        B.append(observation[i, 0])
        B.append(observation[i, 1])
    B = np.asarray(B)
    A = np.zeros((2 * nbPts, 6))
    A[::2, 0] = observation[:, 2]
    A[::2, 1] = observation[:, 3]
    A[::2, 2] = np.ones(nbPts)
    A[1::2, 3] = observation[:, 2]
    A[1::2, 4] = observation[:, 3]
    A[1::2, 5] = np.ones(nbPts)
    P_est_ = model.params.flatten()[:-3]
    np.reshape(P_est_, (len(P_est_), 1))
    absRsid_ = np.abs(np.dot(A, P_est_) - B)
    print("UsingSKimage:")
    print("Estimatedparam=", P_est_)
    print(("max=%.4f,mean=%.4f,std=%.4f,rmse=%.4f") %
          (np.max(absRsid_), np.mean(absRsid_), np.std(absRsid_),
           np.mean(absRsid_**2)))

    print("Using Ransac:")
    model_robust, inliers = ransac((observation[:, 2:4], observation[:, 0:2]),
                                   AffineTransform,
                                   min_samples=3,
                                   residual_threshold=10,
                                   max_trials=1000)
    P_est_R = model_robust.params.flatten()[:-3]
    np.reshape(P_est_R, (len(P_est_R), 1))
    absRsid_R = np.abs(np.dot(A, P_est_R) - B)
    print("Estimated Param:", P_est_R)
    inliers = np.asarray(inliers * 1)
    inliers_ = inliers[inliers.nonzero()]
    print("Number of ouliers=", len(inliers) - len(inliers_))
    print(("max=%.4f,mean=%.4f,std=%.4f,rmse=%.4f") %
          (np.max(absRsid_R), np.mean(absRsid_R), np.std(absRsid_R),
           np.mean(absRsid_R**2)))

    return P_est_R
Esempio n. 5
0
def test_estimate_affine_3d():
    ndim = 3
    src = np.random.random((25, ndim)) * 2**np.arange(7, 7 + ndim)
    matrix = np.array([[4.8, 0.1, 0.2, 25], [0.0, 1.0, 0.1, 30],
                       [0.0, 0.0, 1.0, -2], [0.0, 0.0, 0.0, 1.]])
    tf = AffineTransform(matrix=matrix)
    dst = tf(src)
    dst_noisy = dst + np.random.random((25, ndim))
    tf2 = AffineTransform(dimensionality=ndim)
    tf2.estimate(src, dst_noisy)
    # we check rot/scale/etc more tightly than translation because translation
    # estimation is on the 1 pixel scale
    assert_almost_equal(tf2.params[:, :-1], matrix[:, :-1], decimal=2)
    assert_almost_equal(tf2.params[:, -1], matrix[:, -1], decimal=0)
    _assert_least_squares(tf2, src, dst_noisy)
Esempio n. 6
0
def get_image_crop(full_rgb,
                   rect,
                   scale_rect_x=1.0,
                   scale_rect_y=1.0,
                   shift_x_ratio=0.0,
                   shift_y_ratio=0.0,
                   angle=0.0,
                   out_size=299,
                   order=3):
    """Retrieves image crop.

    # Arguments
        full_rgb: Full image to crop.
        rect: Nominal rectangle to crop.
        scale_rect_x: Amount to scale the rect horizontally.
        scale_rect_y: Amount to scale the rect vertically.
        shift_x_ratio: Amount to shift rect horizontally.
        shift_y_ratio: Amount to shift rect vertically.
        angle: Rotation angle in degrees.
        out_size: Size of one side of output square.
        order: Order to use for transform.

    # Returns
        Cropped image.
    """
    center_x = rect.x + rect.w / 2
    center_y = rect.y + rect.h / 2
    size = int(max(rect.w, rect.h))
    size_x = size * scale_rect_x
    size_y = size * scale_rect_y

    center_x += size * shift_x_ratio
    center_y += size * shift_y_ratio

    scale_x = out_size / size_x
    scale_y = out_size / size_y

    out_center = out_size / 2

    tform = AffineTransform(translation=(center_x, center_y))
    tform = AffineTransform(rotation=angle * math.pi / 180) + tform
    tform = AffineTransform(scale=(1 / scale_x, 1 / scale_y)) + tform
    tform = AffineTransform(translation=(-out_center, -out_center)) + tform
    return skimage.transform.warp(full_rgb,
                                  tform,
                                  mode='edge',
                                  order=order,
                                  output_shape=(out_size, out_size))
def augment_images(images):
    """
    For each image in images, apply several augmentations to create more, 
    diverse training data.

    NOTE: multiplicative increase in training data size
    """
    augmented_images = []
    for img in tqdm(images, desc='Augmenting Images'):
        augmented_images.append(img)  # original
        augmented_images.append(
            np.uint8(255 *
                     gaussian(img, sigma=1.5, multichannel=True)))  # blur
        tfm = AffineTransform(translation=(np.random.randint(-10, 10),
                                           np.random.randint(-10, 10)))
        augmented_images.append(np.uint8(255 *
                                         warp(img, tfm, mode='wrap')))  # shift
        augmented_images.append(
            np.uint8(255 *
                     rotate(img, angle=np.random.randint(5, 20),
                            mode='wrap')))  # rotate left
        augmented_images.append(
            np.uint8(255 *
                     rotate(img, angle=np.random.randint(-20, -5),
                            mode='wrap')))  # rotate right
        augmented_images.append(np.uint8(
            255 * random_noise(img, var=0.005)))  # noise
        augmented_images.append(np.fliplr(img))  # flip

    return augmented_images
def to_multi_input(A_img, n_inputs, transform):
    scales = np.linspace(0.999, 1.001, 30)
    scale = np.random.choice(scales)
    rots = np.linspace(-1 * (np.pi / 300), np.pi / 300, 30)
    rot = np.random.choice(rots)
    shears = np.linspace(-1 * (np.pi / 300), np.pi / 300, 30)
    shear = np.random.choice(shears)
    shifts = np.linspace(-1 * 2, 2, 30)
    shift = np.random.choice(shifts)
    tform = AffineTransform(scale=scale,
                            rotation=rot,
                            shear=shear,
                            translation=shift)
    matrix = np.linalg.inv(tform.params)[:2]
    matrixinv = tform.params[:2]
    A_img = np.asarray(A_img)
    A_all = []
    for n in range(n_inputs):
        if (len(A_all) == 0):
            A_img_transformed = cv2.warpAffine(
                A_img, matrix, (A_img.shape[0], A_img.shape[1]))
        else:
            A_img_transformed = cv2.warpAffine(
                A_img_prev, matrix, (A_img.shape[0], A_img.shape[1]))
        A_img_prev = A_img_transformed.copy()
        A_img_transformed = Image.fromarray(A_img_transformed)
        # A_img_transformed.show()
        A = transform(A_img_transformed)
        A_all.append(A)
    A_all = torch.cat(A_all, 0)
    return A_all
Esempio n. 9
0
    def random_transform(cls, img):
        image_size = img.shape[0]
        d = image_size * 0.2
        tl_top, tl_left, bl_bottom, bl_left, tr_top, tr_right, br_bottom, br_right = np.random.uniform(
            -d, d, size=8)  # Bottom right corner, right margin
        aft = AffineTransform(scale=(1, 1 / 1.2))
        img = warp(img,
                   aft,
                   output_shape=(image_size, image_size),
                   order=1,
                   mode='edge')
        transform = ProjectiveTransform()
        transform.estimate(
            np.array(((tl_left, tl_top), (bl_left, image_size - bl_bottom),
                      (image_size - br_right, image_size - br_bottom),
                      (image_size - tr_right, tr_top))),
            np.array(((0, 0), (0, image_size), (image_size, image_size),
                      (image_size, 0))))

        img = warp(img,
                   transform,
                   output_shape=(image_size, image_size),
                   order=1,
                   mode='edge')
        return img
Esempio n. 10
0
def random_transform(img):
    if np.random.random() < 0.5:
        img = img[:,::-1,:]

    if np.random.random() < 0.5:
        sx = np.random.uniform(0.7, 1.3)
        sy = np.random.uniform(0.7, 1.3)
    else:
        sx = 1.0
        sy = 1.0

    if np.random.random() < 0.5:
        rx = np.random.uniform(-30.0*2.0*np.pi/360.0,+30.0*2.0*np.pi/360.0)
    else:
        rx = 0.0

    if np.random.random() < 0.5:
        tx = np.random.uniform(-10,10)
        ty = np.random.uniform(-10,10)
    else:
        tx = 0.0
        ty = 0.0

    aftrans = AffineTransform(scale=(sx, sy), rotation=rx, translation=(tx,ty))
    img_aug = warp(img,aftrans.inverse,preserve_range=True).astype('uint8')

    return img_aug
Esempio n. 11
0
def rotate_the_astronaut():
    im = astronaut()[..., 0]

    angle = 15

    # use skimage
    trafo = AffineTransform(rotation=np.deg2rad(angle))
    warped_sk = warp(im, trafo, preserve_range=True)
    print(im.shape)
    print(warped_sk.shape)

    matrix = affine_matrix(rotation=[angle])

    # use scipy
    # inv_matrix = np.linalg.inv(matrix)
    # warped_scipy = affine_transform(im, inv_matrix)
    # print(warped_scipy.shape)
    warped_scipy = apply_affine_transformation(im, matrix, use_scipy=True)
    print(warped_scipy.shape)

    # use py-affine
    warped_py = apply_affine_transformation(im, matrix)
    print(warped_py.shape)

    fig, ax = plt.subplots(4)
    ax[0].imshow(im, cmap='gray')
    ax[1].imshow(warped_sk, cmap='gray')
    ax[2].imshow(warped_py, cmap='gray')
    ax[3].imshow(warped_scipy, cmap='gray')
    plt.show()
def shift(image):
    factor = [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]
    changex = random.choice(factor)
    changey = random.choice(factor)

    transform = AffineTransform(translation=(changex, changey))
    return warp(image, transform, mode="wrap")
Esempio n. 13
0
    def apply_trafo(self, transformation: AffineTransform, **kwargs):
        """
        Apply transformation inplace to image and landmarks

        Parameters
        ----------
        transformation : :class:`skimage.transform.AffineTransform`
            transformation to apply
        **kwargs :
            additional keyword arguments

        Returns
        -------
        :class:`BaseSingleImage`
            Transformed Image and Landmarks

        """

        # ensure transformation to be affine
        transformation = AffineTransform(transformation.params)

        self._transformation_history.append(transformation)
        self._transform_img(transformation, **kwargs)

        self._transform_lmk(transformation)

        return self
Esempio n. 14
0
def TF_shear(x, shear=0.0):
    assert len(x.shape) == 3
    h, w, nc = x.shape

    # Perform shear
    xc = warp(x, AffineTransform(shear=shear), mode='edge')
    return xc
Esempio n. 15
0
def test_union():
    tform1 = SimilarityTransform(scale=0.1, rotation=0.3)
    tform2 = SimilarityTransform(scale=0.1, rotation=0.9)
    tform3 = SimilarityTransform(scale=0.1**2, rotation=0.3 + 0.9)
    tform = tform1 + tform2
    assert_array_almost_equal(tform._matrix, tform3._matrix)

    tform1 = AffineTransform(scale=(0.1, 0.1), rotation=0.3)
    tform2 = SimilarityTransform(scale=0.1, rotation=0.9)
    tform3 = SimilarityTransform(scale=0.1**2, rotation=0.3 + 0.9)
    tform = tform1 + tform2
    assert_array_almost_equal(tform._matrix, tform3._matrix)
    assert tform.__class__ == ProjectiveTransform

    tform = AffineTransform(scale=(0.1, 0.1), rotation=0.3)
    assert_array_almost_equal((tform + tform.inverse).params, np.eye(3))
Esempio n. 16
0
def augment_image(img,
                  vflip=.5,
                  hflip=.5,
                  angle=360,
                  shear=.3,
                  seed=np.random.randint(1e6)):
    """ apply random transformations to an image
    vflip/hflip: probabilities
    angle/shear: maximums
    seed: set to apply same transforms on multiple images
    """
    np.random.seed(seed)

    vflip = np.random.random() > vflip
    hflip = np.random.random() > hflip
    angle = np.random.random() * angle
    shear = np.random.random() * shear

    if vflip:
        img = np.flip(img, 0)
    if hflip:
        img = np.flip(img, 1)
    img = rotate(img, angle)
    img = warp(img, inverse_map=AffineTransform(shear=shear))
    img = (img * 255).astype(np.uint8)
    # log.info(f"hflip={hflip}, vflip={vflip}, angle={angle:.0f}, shear={shear:.2f}")

    return img
Esempio n. 17
0
def _rotate(image, angle, center, scale, cval=0):
    """
    Rotate function taken mostly from scikit image. Main difference is that
    this one allows dimensional scaling and records the final translation
    to ensure no image content is lost. This is needed to rotate the seam
    back into the original image.
    """
    rows, cols = image.shape[0], image.shape[1]
    tform1 = SimilarityTransform(translation=center)
    tform2 = SimilarityTransform(rotation=angle)
    tform3 = SimilarityTransform(translation=-center)
    tform4 = AffineTransform(scale=(1 / scale, 1))
    tform = tform4 + tform3 + tform2 + tform1
    corners = np.array([[0, 0], [0, rows - 1], [cols - 1, rows - 1],
                        [cols - 1, 0]])
    corners = tform.inverse(corners)
    minc = corners[:, 0].min()
    minr = corners[:, 1].min()
    maxc = corners[:, 0].max()
    maxr = corners[:, 1].max()
    out_rows = maxr - minr + 1
    out_cols = maxc - minc + 1
    output_shape = np.around((out_rows, out_cols))
    # fit output image in new shape
    translation = (minc, minr)
    tform5 = SimilarityTransform(translation=translation)
    tform = tform5 + tform
    tform.params[2] = (0, 0, 1)
    return tform, warp(image,
                       tform,
                       output_shape=output_shape,
                       order=0,
                       cval=cval,
                       clip=False,
                       preserve_range=True)
Esempio n. 18
0
def random_affine_helper(
    img,
    img_mask,
    intensity=1.0,
    rotation_disabled=True,
    shear_disabled=True,
    scale_disabled=True,
):
    if rotation_disabled:
        rotation = None
    else:
        rotation = random.uniform(-0.15 * intensity, 0.15 * intensity)

    if shear_disabled:
        shear = None
    else:
        shear = random.uniform(-0.15 * intensity, 0.15 * intensity)

    if scale_disabled:
        scale = None
    else:
        scale_rnd = random.uniform(0.9, 1.1)
        scale = (scale_rnd, scale_rnd)

    affine_t = AffineTransform(rotation=rotation, shear=shear, scale=scale)
    return warp_helper(img, affine_t), warp_helper(img_mask, affine_t)
 def __rotate(self, X, Y) -> InterpolateSubdataset:
     return self.__transform(
         X, Y, "ROTATION", self.__rotationFactors, lambda f:
         AffineTransform(rotation=f,
                         shear=self.__defaultShearFactor,
                         scale=(2**self.__defaultLog2StretchFactor, 2**self.
                                __defaultLog2StretchFactor)), (1, ))
Esempio n. 20
0
def im_affine_transform(img, scale, rotation, shear, translation_y, translation_x, return_tform=False):
    # Assumed img in c01. Convert to 01c for skimage
    img = img.transpose(1, 2, 0)
    # Normalize so that the param acts more like im_rotate, im_translate etc
    scale = 1 / scale
    translation_x = - translation_x
    translation_y = - translation_y

    # shift to center first so that image is rotated around center
    center_shift = np.array((img.shape[0], img.shape[1])) / 2. - 0.5
    tform_center = SimilarityTransform(translation=-center_shift)
    tform_uncenter = SimilarityTransform(translation=center_shift)

    rotation = np.deg2rad(rotation)
    tform = AffineTransform(scale=(scale, scale), rotation=rotation,
                            shear=shear,
                            translation=(translation_x, translation_y))
    tform = tform_center + tform + tform_uncenter

    warped_img = warp(img, tform)

    # Convert back from 01c to c01
    warped_img = warped_img.transpose(2, 0, 1)
    warped_img = warped_img.astype(img.dtype)
    if return_tform:
        return warped_img, tform
    else:
        return warped_img
Esempio n. 21
0
def random_affine(image,
                  label,
                  scale_limit=(0.9, 1.1),
                  translation=(-0.0625, 0.0625),
                  shear=None,
                  rotation=None):
    scale = np.random.uniform(*scale_limit, size=2)

    if translation is not None:
        translation = np.random.uniform(*np.multiply(image.shape[:-1],
                                                     translation),
                                        size=2)
    if shear is not None:
        shear = np.random.uniform(*np.deg2rad(shear))
    if rotation is not None:
        rotation = np.random.uniform(*np.deg2rad(rotation))

    tform = AffineTransform(scale=scale,
                            rotation=rotation,
                            shear=shear,
                            translation=translation)
    return [
        warp(item, tform, mode='edge', preserve_range=True)
        for item in [image, label]
    ]
Esempio n. 22
0
def main():
    camera = data.camera()
    # rescale_camera = rescale(camera, scale=0.1)
    # resize_camera = resize(camera,output_shape=(500,500))
    # newcamera = rotate(camera,15)
    # crop_width=((top,left),(bottom,right))
    # crop_right_camera = util.crop(camera,crop_width=((0,0),(0,150)))
    # crop_left_camera = util.crop(camera,crop_width=((0,150),(0,0)))
    # crop_center_camera = util.crop(camera,crop_width=((150,150),(150,150)))
    # tform = AffineTransform(scale=(1.1,1.1),shear=0.7)
    # sh_ro7 = warp(camera,inverse_map=tform,output_shape=(512,512))
    #
    # tform = AffineTransform(shear=0.8, rotation=90)
    # sh_ro8 = warp(camera, inverse_map=tform, output_shape=(512, 512))
    tform = AffineTransform(shear=-0.2, translation=(50, 0))
    sh_ro9 = warp(camera, inverse_map=tform.inverse, output_shape=(512, 512))

    # camera = util.random_noise(camera,mode='salt')

    # fil_camer = rank.median(camera,selem=disk(3))
    # media_camer = rank.mean(camera,selem=disk(3))

    plt.figure()
    plt.subplot(2, 2, 1)
    plt.imshow(camera)
    plt.subplot(2, 2, 3)
    plt.imshow(sh_ro9)

    plt.show()
Esempio n. 23
0
def random_translate(X, steer, intensity=1):
    delta = 15. * intensity
    rand_delta = random.uniform(-delta, delta)
    translate_matrix = AffineTransform(translation=(rand_delta, 0))
    X = warp(X, translate_matrix)
    steer += rand_delta * 0.01
    return X, steer
Esempio n. 24
0
def test_degenerate():
    src = dst = np.zeros((10, 2))

    tform = SimilarityTransform()
    tform.estimate(src, dst)
    assert np.all(np.isnan(tform.params))

    tform = AffineTransform()
    tform.estimate(src, dst)
    assert np.all(np.isnan(tform.params))

    tform = ProjectiveTransform()
    tform.estimate(src, dst)
    assert np.all(np.isnan(tform.params))

    # See gh-3926 for discussion details
    tform = ProjectiveTransform()
    for i in range(20):
        # Some random coordinates
        src = np.random.rand(4, 2) * 100
        dst = np.random.rand(4, 2) * 100

        # Degenerate the case by arranging points on a single line
        src[:, 1] = np.random.rand()
        # Prior to gh-3926, under the above circumstances,
        # a transform could be returned with nan values.
        assert (not tform.estimate(src, dst)
                or np.isfinite(tform.params).all())
    def apply_warp_augmentation_on_an_image(self, image):
        image_size = image.shape[0]
        #aff_tform = AffineTransform(scale=(1, 1/1.2), rotation=1, shear=0.7, translation=(210, 50))
        aff_tform = AffineTransform(scale=(1, 1 / 1.2))
        image = warp(image,
                     aff_tform,
                     output_shape=(image_size, image_size),
                     order=1,
                     mode='edge')

        x = image_size * 0.2
        # top_right - x,y
        # bottom_left - x,y
        # top_right - x,y
        # bottom_right - x,y
        tl_x, tl_y, bl_x, bl_y, tr_x, tr_y, br_x, br_y = np.random.uniform(
            -x, x, size=8)

        src = np.array([[tl_y, tl_x], [bl_y, image_size - bl_x],
                        [image_size - br_y, image_size - br_x],
                        [image_size - tr_y, tr_x]])
        dst = np.array([[0, 0], [0, image_size], [image_size, image_size],
                        [image_size, 0]])

        proj_tform = ProjectiveTransform()
        proj_tform.estimate(src, dst)

        image = warp(image,
                     proj_tform,
                     output_shape=(image_size, image_size),
                     order=1,
                     mode='edge')

        return image
 def __shear(self, X, Y) -> InterpolateSubdataset:
     return self.__transform(
         X, Y, "SHEAR", self.__shearFactors, lambda f: AffineTransform(
             shear=f,
             rotation=self.__defaultRotationFactor,
             scale=(2**self.__defaultLog2StretchFactor, 2**self.
                    __defaultLog2StretchFactor)), (2, ))
Esempio n. 27
0
def augmentator(x, y, mode):
    rotate_values = [0, 5, 10, 15]
    shift_values = [0, 0.1, 0.2, 0.3]
    shear_values = [0, 0.05, 0.1, 0.15]

    rotate_range = rotate_values[mode]
    shift_range = shift_values[mode]
    shear_range = shear_values[mode]

    random_rotate = random.uniform(-rotate_range, rotate_range)
    random_shift = random.uniform(-shift_range, shift_range)
    shift_x = round(x.shape[0] * random_shift)
    shift_y = round(x.shape[0] * random_shift)
    random_shear = random.uniform(-shear_range, shear_range)
    shear_tf = AffineTransform(shear=random_shear)

    x = rotate(x, random_rotate, order=0, reshape=False)
    y = rotate(y, random_rotate, order=0, reshape=False)

    x = shift(x, (shift_x, shift_y, 0), order=0)
    y = shift(y, (shift_x, shift_y, 0), order=0)

    x = warp(x, inverse_map=shear_tf)
    y = warp(y, inverse_map=shear_tf)

    y = (y > 0.5).astype(np.uint8)

    return x, y
def test_mirrored_transformation(fixed_image, moved_image, rotation,
                                 translation):
    '''
    Test whether rotation should be as is, or plus a factor of 180 degrees. Perform reconstruction by both options 
    and choose the one which gives higher correlation between original image and reconstructed image.
    :param fixed_image: [numpy.ndarray] 2d image
    :param moved_image: image after an affine transform was performed on fixed_image
    :param rotation: [float] degrees rotation to perform on moved_image
    :param translation: [list of ints] translation in x and y to perform on moved_image
    :return: [numpy.ndarray] correct reconstructed image
    '''
    # reconstruct image in both ways
    found_transform = AffineTransform(translation=translation)
    registered_img = warp(moved_image, found_transform)
    original_reconstruction = rotate(registered_img, rotation)
    mirror_reconstruction = rotate(registered_img, 180 + rotation)

    # compare correlation to fixed image
    template_match = norm_xcorr.TemplateMatch(fixed_image, 'ncc')
    original_ncc = template_match(original_reconstruction)
    mirror_ncc = template_match(mirror_reconstruction)

    if np.max(original_ncc) > np.max(mirror_ncc):
        return original_reconstruction
    else:
        return mirror_reconstruction
Esempio n. 29
0
 def randomAffine(self, im):
     tform = AffineTransform(
         scale=(self.randRange(0.75, 1.3), self.randRange(0.75, 1.3)),
         translation=(self.randRange(-im.shape[0] // 10, im.shape[0] // 10),
                      self.randRange(-im.shape[1] // 10,
                                     im.shape[1] // 10)))
     return warp(im, tform.inverse, mode='reflect')
Esempio n. 30
0
def test_ransac_geometric():
    random_state = np.random.RandomState(1)

    # generate original data without noise
    src = 100 * random_state.random_sample((50, 2))
    model0 = AffineTransform(scale=(0.5, 0.3),
                             rotation=1,
                             translation=(10, 20))
    dst = model0(src)

    # add some faulty data
    outliers = (0, 5, 20)
    dst[outliers[0]] = (10000, 10000)
    dst[outliers[1]] = (-100, 100)
    dst[outliers[2]] = (50, 50)

    # estimate parameters of corrupted data
    model_est, inliers = ransac((src, dst),
                                AffineTransform,
                                2,
                                20,
                                random_state=random_state)

    # test whether estimated parameters equal original parameters
    assert_almost_equal(model0.params, model_est.params)
    assert np.all(np.nonzero(inliers == False)[0] == outliers)
Esempio n. 31
0
def transform_img(img, mask, vers):
    if vers == 'train':
        theta = randint(-100, 100) / 1800
        zoom = randint(1000, 1100) / 1000
        shear = randint(-100, 100) / 1800
        hrz_trans = randint(-20, 20)
        vrt_trans = randint(-20, 20)
        flip = random.random() < 0.5
    else:
        theta = shear = hrz_trans = vrt_trans = 0
        zoom = 1
        flip = False

    affine = AffineTransform(scale=(zoom, zoom),
                             rotation=theta,
                             shear=shear,
                             translation=(hrz_trans, vrt_trans))

    img = transform.warp(img, affine.inverse)
    mask = transform.warp(mask, affine.inverse)

    if flip:
        img = np.flip(img, 1)
        mask = np.flip(mask, 1)

    return img, mask
Esempio n. 32
0
    min_idx = np.argmin(SSDs)
    return coords_warped_subpix[min_idx]


# find correspondences using simple weighted sum of squared differences
src = []
dst = []
for coord in coords_orig_subpix:
    src.append(coord)
    dst.append(match_corner(coord))
src = np.array(src)
dst = np.array(dst)


# estimate affine transform model using all coordinates
model = AffineTransform()
model.estimate(src, dst)

# robustly estimate affine transform model with RANSAC
model_robust, inliers = ransac((src, dst), AffineTransform, min_samples=3,
                               residual_threshold=2, max_trials=100)
outliers = inliers == False


# compare "true" and estimated transform parameters
print tform.scale, tform.translation, tform.rotation
print model.scale, model.translation, model.rotation
print model_robust.scale, model_robust.translation, model_robust.rotation


# visualize correspondences
Esempio n. 33
0
def plot_annotations(imgid, clustered_annotations):
    try:
        img = mpimg.imread(IMAGE_DIR + imgid + ".JPG")
        pass
    except IOError:
        print "WARNING: couldn't find image '%s'" % imgid
        return False

    # Plot all annotations
    fig = plt.figure()
    ax = plt.gca()
    plt.imshow(img)
    for annotation, cluster_id in clustered_annotations:
        color = COLOR_MAP[cluster_id]
        rect = Rectangle((annotation.left, annotation.top),
                         annotation.width, annotation.height,
                         fill=False, color=color)
        ax.add_patch(rect)
    plt.show()

    # Plot median human and computer annotations
    by_cluster = annotations_by_cluster(clustered_annotations)
    all_medians = { clusterid :
                    (median_annotation(annotations),
                     median_annotation([annotation for annotation in annotations
                                        if annotation[0].is_human]),
                     median_annotation([annotation for annotation in annotations
                                        if not annotation[0].is_human]))
                      for clusterid, annotations in by_cluster.iteritems() }
    plt.figure()
    ax = plt.gca()
    plt.imshow(img)
    for clusterid, medians in all_medians.iteritems():
        color = COLOR_MAP[clusterid]
        for median in medians:
            if median is None: continue
            rect = Rectangle((median.left, median.top),
                             median.width, median.height, fill=False, color=color)
            ax.add_patch(rect)
    plt.show()

    # Affine transform image to consistent shape and plot again.
    from skimage.transform import AffineTransform, warp

    # calculate scale and coreners
    row_scale = float(img.shape[0]) / 400.0
    col_scale = float(img.shape[1]) / 400.0
    src_corners = np.array([[1, 1], [1, 400.0], [400.0, 400.0]]) - 1
    dst_corners = np.zeros(src_corners.shape, dtype=np.double)
    # take into account that 0th pixel is at position (0.5, 0.5)
    dst_corners[:, 0] = col_scale * (src_corners[:, 0] + 0.5) - 0.5
    dst_corners[:, 1] = row_scale * (src_corners[:, 1] + 0.5) - 0.5

    # do the transformation
    tform = AffineTransform()
    tform.estimate(src_corners, dst_corners)
    resized = warp(img, tform, output_shape=[400.0, 400.0], order=1,
                   mode='constant', cval=0)

    # plot the transformed image
    plt.figure()
    ax = plt.gca()
    plt.imshow(resized)
    for clusterid, medians in all_medians.iteritems():
        color = COLOR_MAP[clusterid]

        for median in medians:
            if median is None: continue

            # apply the transformation to each rectangle
            corners = np.array([[median.left, median.top],
                                [median.left + median.width,
                                 median.top + median.height]])
            new_corners = tform.inverse(corners)
            rect = Rectangle(new_corners[0, :],
                             new_corners[1,0] - new_corners[0,0],
                             new_corners[1,1] - new_corners[0,1],
                             fill=False, color=color)
            ax.add_patch(rect)
    plt.show()
    return True