Python skimage.transform.AffineTransform() Examples

The following are 30 code examples of skimage.transform.AffineTransform(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module skimage.transform , or try the search function .
Example #1
Source File: scalable_reference_pattern.py    From pyxem with GNU General Public License v3.0 8 votes vote down vote up
def function(self, x, y):

        signal2D = self.signal.data
        order = self.order
        d11 = self.d11.value
        d12 = self.d12.value
        d21 = self.d21.value
        d22 = self.d22.value
        t1 = self.t1.value
        t2 = self.t2.value

        D = np.array([[d11, d12, t1], [d21, d22, t2], [0.0, 0.0, 1.0]])

        shifty, shiftx = np.array(signal2D.shape[:2]) / 2

        shift = tf.SimilarityTransform(translation=[-shiftx, -shifty])
        tform = tf.AffineTransform(matrix=D)
        shift_inv = tf.SimilarityTransform(translation=[shiftx, shifty])

        transformed = tf.warp(
            signal2D, (shift + (tform + shift_inv)).inverse, order=order
        )

        return transformed 
Example #2
Source File: image_processing_common.py    From tensorflow-litterbox with Apache License 2.0 7 votes vote down vote up
def distort_affine_skimage(image, rotation=10.0, shear=5.0, random_state=None):
    if random_state is None:
        random_state = np.random.RandomState(None)

    rot = np.deg2rad(np.random.uniform(-rotation, rotation))
    sheer = np.deg2rad(np.random.uniform(-shear, shear))

    shape = image.shape
    shape_size = shape[:2]
    center = np.float32(shape_size) / 2. - 0.5

    pre = transform.SimilarityTransform(translation=-center)
    affine = transform.AffineTransform(rotation=rot, shear=sheer, translation=center)
    tform = pre + affine

    distorted_image = transform.warp(image, tform.params, mode='reflect')

    return distorted_image.astype(np.float32) 
Example #3
Source File: transforms.py    From Face-Recognition with MIT License 6 votes vote down vote up
def __call__(self, img):
        img_data = np.array(img)
        h, w, n_chan = img_data.shape
        scale_x = np.random.uniform(*self.scale_range)
        scale_y = np.random.uniform(*self.scale_range)
        scale = (scale_x, scale_y)
        rotation = np.random.uniform(*self.rotation_range)
        shear = np.random.uniform(*self.shear_range)
        translation = (
            np.random.uniform(*self.translation_range) * w,
            np.random.uniform(*self.translation_range) * h
        )
        af = AffineTransform(scale=scale, shear=shear, rotation=rotation,
                             translation=translation)
        img_data1 = warp(img_data, af.inverse)
        img1 = Image.fromarray(np.uint8(img_data1 * 255))
        return img1 
Example #4
Source File: transforms.py    From KagglePlanetPytorch with MIT License 6 votes vote down vote up
def augment_deterministic(
        rotation=0,
        translation=0,
        scale_factor=1,
        shear=0
):
    def call(x):
        scale = scale_factor, scale_factor
        rotation_tmp = rotation

        tf_augment = AffineTransform(
            scale=scale,
            rotation=np.deg2rad(rotation_tmp),
            translation=translation,
            shear=np.deg2rad(shear)
        )
        tf = tf_center + tf_augment + tf_uncenter

        x = warp(x, tf, order=1, preserve_range=True, mode='symmetric')

        return x

    return call 
Example #5
Source File: transforms.py    From KagglePlanetPytorch with MIT License 6 votes vote down vote up
def augment(
        rotation_fn=lambda: np.random.random_integers(0, 360),
        translation_fn=lambda: (np.random.random_integers(-20, 20), np.random.random_integers(-20, 20)),
        scale_factor_fn=random_zoom_range(),
        shear_fn=lambda: np.random.random_integers(-10, 10)
):
    def call(x):
        rotation = rotation_fn()
        translation = translation_fn()
        scale = scale_factor_fn()
        shear = shear_fn()

        tf_augment = AffineTransform(scale=scale, rotation=np.deg2rad(rotation), translation=translation, shear=np.deg2rad(shear))
        tf = tf_center + tf_augment + tf_uncenter

        x = warp(x, tf, order=1, preserve_range=True, mode='symmetric')

        return x

    return call 
Example #6
Source File: JN721.py    From facial_expressions with Apache License 2.0 6 votes vote down vote up
def transform_img__(self, img, fn, emotion):
        self.images[fn] = {'Image': img, 'Emotion': emotion}                                            # Store original
        counter = 0

        self.images["Trans" + str(counter) + "_" + fn] = {'Image': np.fliplr(img), 'Emotion': emotion}  # FLIP the image
        counter += 1

        for deg in range(-10, 15, 5):                                        # ROTATE to be robust to camera orientation
            if deg == 0:
                continue

            self.images["Trans" + str(counter) + "_" + fn] = {'Image': tf.rotate(img, deg), 'Emotion': emotion}
            counter += 1

        lenX, lenY = img.shape                                                           # CROP based on rough heuristic
        for crop_size in range(8, 14, 2):
            cropped = img[lenX / crop_size: - lenX / crop_size, lenY / crop_size: - lenY / crop_size]
            self.images["Trans" + str(counter) + "_" + fn] = {'Image': cropped, 'Emotion': emotion}
            counter += 1

        for i in range(2):                                           # SCALE down images (random factor btw 1.1 to 1.21)
            scale_factor = math.sqrt(1.1) ** np.random.randint(2, 5)
            scaled_img = tf.warp(img, tf.AffineTransform(scale=(scale_factor, scale_factor)))
            self.images["Trans" + str(counter) + "_" + fn] = {'Image': scaled_img, 'Emotion': emotion}
            counter += 1 
Example #7
Source File: preprocessing.py    From face-identification-tpe with MIT License 6 votes vote down vote up
def align_face(self,
                   image,
                   face_rect, *,
                   dim=96,
                   border=0,
                   mask=FaceAlignMask.INNER_EYES_AND_BOTTOM_LIP):
        mask = np.array(mask.value)

        landmarks = self.get_landmarks(image, face_rect)
        proper_landmarks = border + dim * self.face_template[mask]
        A = np.hstack([landmarks[mask], np.ones((3, 1))]).astype(np.float64)
        B = np.hstack([proper_landmarks, np.ones((3, 1))]).astype(np.float64)
        T = np.linalg.solve(A, B).T

        wrapped = tr.warp(image,
                          tr.AffineTransform(T).inverse,
                          output_shape=(dim + 2 * border, dim + 2 * border),
                          order=3,
                          mode='constant',
                          cval=0,
                          clip=True,
                          preserve_range=True)

        return wrapped 
Example #8
Source File: transform_ops_test.py    From addons with Apache License 2.0 6 votes vote down vote up
def test_shear_y():
    image = np.random.randint(low=0, high=255, size=(4, 4, 3), dtype=np.uint8)
    color = tf.constant([255, 0, 255], tf.dtypes.uint8)
    level = tf.random.uniform(shape=(), minval=0, maxval=1)

    tf_image = tf.constant(image)
    sheared_img = transform_ops.shear_y(image=tf_image, level=level, replace=color)
    transform_matrix = transform.AffineTransform(
        np.array([[1, 0, 0], [level.numpy(), 1, 0], [0, 0, 1]])
    )
    expected_img = transform.warp(
        image, transform_matrix, order=0, cval=-1, preserve_range=True
    )

    mask = np.where(expected_img == -1)
    expected_img[mask[0], mask[1], :] = color

    np.testing.assert_equal(sheared_img.numpy(), expected_img) 
Example #9
Source File: transform_ops_test.py    From addons with Apache License 2.0 6 votes vote down vote up
def test_shear_x():
    image = np.random.randint(low=0, high=255, size=(4, 4, 3), dtype=np.uint8)
    color = tf.constant([255, 0, 255], tf.uint8)
    level = tf.random.uniform(shape=(), minval=0, maxval=1)

    tf_image = tf.constant(image)
    sheared_img = transform_ops.shear_x(tf_image, level, replace=color)
    transform_matrix = transform.AffineTransform(
        np.array([[1, level.numpy(), 0], [0, 1, 0], [0, 0, 1]])
    )
    expected_img = transform.warp(
        image, transform_matrix, order=0, cval=-1, preserve_range=True
    )

    mask = np.where(expected_img == -1)
    expected_img[mask[0], mask[1], :] = color

    np.testing.assert_equal(sheared_img.numpy(), expected_img)


# TODO: Parameterize on dtypes 
Example #10
Source File: registration.py    From BIRL with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_affine_components(matrix):
    """ get the main components of Affine transform

    :param ndarray matrix: affine transformation matrix for 2d
    :return dict: dictionary of float values

    >>> mtx = np.array([[ -0.95,   0.1,  65.], [  0.1,   0.95, -60.], [  0.,   0.,   1.]])
    >>> import  pandas as pd
    >>> aff = pd.Series(get_affine_components(mtx)).sort_index()
    >>> aff  # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
    rotation                           173.9...
    scale                    (0.95..., 0.95...)
    shear                              -3.14...
    translation                   (65.0, -60.0)
    dtype: object
    """
    aff = AffineTransform(matrix)
    norm_rotation = norm_angle(np.rad2deg(aff.rotation), deg=True)
    comp = {
        'rotation': float(norm_rotation),
        'translation': tuple(aff.translation.tolist()),
        'scale': aff.scale,
        'shear': aff.shear,
    }
    return comp 
Example #11
Source File: bm_comp_perform.py    From BIRL with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _prepare_images(path_out, im_size=IMAGE_SIZE):
    """ generate and prepare synth. images for registration

    :param str path_out: path to the folder
    :param tuple(int,int) im_size: desired image size
    :return tuple(str,str): paths to target and source image
    """
    image = resize(data.astronaut(), output_shape=im_size, mode='constant')
    img_target = random_noise(image, var=IMAGE_NOISE)
    path_img_target = os.path.join(path_out, NAME_IMAGE_TARGET)
    io.imsave(path_img_target, img_target)

    # warp synthetic image
    tform = AffineTransform(scale=(0.9, 0.9),
                            rotation=0.2,
                            translation=(200, -50))
    img_source = warp(image, tform.inverse, output_shape=im_size)
    img_source = random_noise(img_source, var=IMAGE_NOISE)
    path_img_source = os.path.join(path_out, NAME_IMAGE_SOURCE)
    io.imsave(path_img_source, img_source)
    return path_img_target, path_img_source 
Example #12
Source File: align.py    From pySPM with Apache License 2.0 5 votes vote down vote up
def getTf(self, verbose=False):
        """
        Get the Afdfine transform.
        You can apply it to a pySPM Image (img) with: img.align(this.getTf())
        """
        if verbose:
            print("Transpose: {0[0]}, {0[1]}".format(self.trans))
        return tf.AffineTransform(scale=self.scale, rotation=self.rotation, translation=self.trans) 
Example #13
Source File: jhamski.py    From facial_expressions with Apache License 2.0 5 votes vote down vote up
def warp34(img, name):
    tform = tf.AffineTransform(shear=math.pi / -3.6)

    af_img3 = tf.warp(img, tform)
    save_image(af_img3, name, 'warp3')

    af_img4 = tf.warp(img, tform.inverse)
    save_image(af_img4, name, 'warp4') 
Example #14
Source File: image.py    From OverwatchDataAnalysis with GNU General Public License v3.0 5 votes vote down vote up
def shear(img, shear_rad):
    """
    Shear a image to a given radian.
    @Author: Rigel
    @param img: image to be sheared
    @param shear_rad: radian to which the image will be sheared
    @return: a numpy.ndarray object of this image
    """
    affine_tf = tf.AffineTransform(shear=shear_rad)
    return tf.warp(img, inverse_map=affine_tf) 
Example #15
Source File: image_tfs.py    From tanda with MIT License 5 votes vote down vote up
def TF_translate(img, x, y):
    return warp(img, AffineTransform(translation=(x, y)), mode='edge') 
Example #16
Source File: image_tfs.py    From tanda with MIT License 5 votes vote down vote up
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 
Example #17
Source File: nyu_depth_v2.py    From pytorch-mono-depth with MIT License 5 votes vote down vote up
def __call__(self, image):
        scale = self.random_state.uniform(self.scale_range[0],
                                          self.scale_range[1])
        if isinstance(image, np.ndarray):
            af = AffineTransform(scale=(scale, scale))
            image = warp(image, af.inverse)
            rgb = image[:, :, 0:3]
            depth = image[:, :, 3:4] / scale
            mask = image[:, :, 4:5]
            return np.concatenate([rgb, depth, mask], axis=2)
        else:
            raise Exception('unsupported type') 
Example #18
Source File: geometric.py    From ViolenceDetection with Apache License 2.0 5 votes vote down vote up
def _warp_skimage(self, image, scale_x, scale_y, translate_x_px, translate_y_px, rotate, shear, cval, mode, order):
        height, width = image.shape[0], image.shape[1]
        shift_x = width / 2.0 - 0.5
        shift_y = height / 2.0 - 0.5

        matrix_to_topleft = tf.SimilarityTransform(translation=[-shift_x, -shift_y])
        matrix_transforms = tf.AffineTransform(
            scale=(scale_x, scale_y),
            translation=(translate_x_px, translate_y_px),
            rotation=math.radians(rotate),
            shear=math.radians(shear)
        )
        matrix_to_center = tf.SimilarityTransform(translation=[shift_x, shift_y])
        matrix = (matrix_to_topleft + matrix_transforms + matrix_to_center)
        image_warped = tf.warp(
            image,
            matrix.inverse,
            order=order,
            mode=mode,
            cval=cval,
            preserve_range=True
        )
        # warp changes uint8 to float64, making this necessary
        if image_warped.dtype != image.dtype:
            image_warped = image_warped.astype(image.dtype, copy=False)
        return image_warped 
Example #19
Source File: geometric.py    From ViolenceDetection with Apache License 2.0 5 votes vote down vote up
def _warp_cv2(self, image, scale_x, scale_y, translate_x_px, translate_y_px, rotate, shear, cval, mode, order):
        height, width = image.shape[0], image.shape[1]
        shift_x = width / 2.0 - 0.5
        shift_y = height / 2.0 - 0.5

        matrix_to_topleft = tf.SimilarityTransform(translation=[-shift_x, -shift_y])
        matrix_transforms = tf.AffineTransform(
            scale=(scale_x, scale_y),
            translation=(translate_x_px, translate_y_px),
            rotation=math.radians(rotate),
            shear=math.radians(shear)
        )
        matrix_to_center = tf.SimilarityTransform(translation=[shift_x, shift_y])
        matrix = (matrix_to_topleft + matrix_transforms + matrix_to_center)

        image_warped = cv2.warpAffine(
            image,
            matrix.params[:2],
            #np.zeros((2, 3)),
            dsize=(width, height),
            flags=order,
            borderMode=mode,
            borderValue=cval
        )

        # cv2 warp drops last axis if shape is (H, W, 1)
        if image_warped.ndim == 2:
            image_warped = image_warped[..., np.newaxis]

        return image_warped 
Example #20
Source File: align.py    From pySPM with Apache License 2.0 5 votes vote down vote up
def ImproveShift(self, verbose=False, **kargs):
        tform = tf.AffineTransform(
            scale=self.scale, rotation=self.rotation, translation=(0,0))
        
        O = tf.warp(self.other, tform, output_shape=self.other.shape, preserve_range=True)
        if self.FFT:
            Corr = np.real(np.fft.fftshift(np.fft.ifft2(
                np.conj(np.fft.fft2(self.fixed))*np.fft.fft2(O))))
            cord = np.unravel_index(np.argmax(Corr), self.fixed.shape)
            self.trans = [cord[1]-self.size[0]/2, cord[0]-self.size[1]/2]
        else:
            shift, D = AutoShift(self.fixed, O, shift=[-self.trans[0], self.trans[1]],**kargs)
            if verbose:
                print("ImproveShift",shift,D)
            self.trans = [-shift[0], shift[1]] 
Example #21
Source File: expt_utils.py    From pyxem with GNU General Public License v3.0 5 votes vote down vote up
def convert_affine_to_transform(D, shape):
    """ Converts an affine transform on a diffraction pattern to a suitable
    form for skimage.transform.warp()

    Parameters
    ----------
    D : np.array
        Affine transform to be applied
    shape : tuple
        Shape tuple in form (y,x) for the diffraction pattern

    Returns
    -------
    transformation : np.array
        3x3 numpy array of the transformation to be applied.

    """

    shift_x = (shape[1] - 1) / 2
    shift_y = (shape[0] - 1) / 2

    tf_shift = tf.SimilarityTransform(translation=[-shift_x, -shift_y])
    tf_shift_inv = tf.SimilarityTransform(translation=[shift_x, shift_y])

    # This defines the transform you want to perform
    distortion = tf.AffineTransform(matrix=D)

    # skimage transforms can be added like this, does matrix multiplication,
    # hence the need for the brackets. (Note tf.warp takes the inverse)
    transformation = (tf_shift + (distortion + tf_shift_inv)).inverse

    return transformation 
Example #22
Source File: segmentation.py    From kraken with Apache License 2.0 5 votes vote down vote up
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) 
Example #23
Source File: actions.py    From pychubby with MIT License 5 votes vote down vote up
def perform(self, lf):
        """Perform action.

        Parameters
        ----------
        lf : LandmarkFace
            Instance of a ``LandmarkFace`` before taking the action.

        Returns
        -------
        new_lf : LandmarkFace
            Instance of a ``LandmarkFace`` after taking the action.

        df : DisplacementField
            Displacement field representing the transformation between the old and new image.

        """
        # estimate reference space
        self.reference_space.estimate(lf)

        # transform reference space landmarks
        ref_points = self.reference_space.inp2ref(lf.points)

        tform = AffineTransform(
            scale=(self.scale_x, self.scale_y),
            rotation=self.rotation,
            shear=self.shear,
            translation=(self.translation_x, self.translation_y),
        )
        tformed_ref_points = tform(ref_points)

        # ref2inp
        tformed_inp_points = self.reference_space.ref2inp(tformed_ref_points)

        x_shifts = {i: (tformed_inp_points[i] - lf[i])[0] for i in range(68)}
        y_shifts = {i: (tformed_inp_points[i] - lf[i])[1] for i in range(68)}

        return AbsoluteMove(x_shifts=x_shifts, y_shifts=y_shifts).perform(lf) 
Example #24
Source File: reference.py    From pychubby with MIT License 5 votes vote down vote up
def __init__(self):
        """Construct."""
        self.tform = AffineTransform()
        self.keypoints = {
                         'CHIN': (0, 1),
                         'UPPER_TEMPLE_L': (-1, -1),
                         'UPPER_TEMPLE_R': (1, -1),
                         'UPPERMOST_NOSE': (0, -1),
                         'MIDDLE_NOSTRIL': (0, 0)
                            } 
Example #25
Source File: prep_overstrike.py    From ASR33 with MIT License 5 votes vote down vote up
def load_image(filename):
    if filename in loaded_files:
        return loaded_files[filename]
    img = imageio.imread(filename, as_gray=True).astype(np.float)

    # Warp it to be reasonably squared
    tf = transform.AffineTransform(rotation=ROTATION, shear=SHEAR, translation=TRANSLATION)
    img = transform.warp(img, inverse_map=tf)

    # Normalize the whole image
    # img *= 1.0/(img.max() - img.min())
    img = (img - np.min(img))/np.ptp(img)

    # Normalize on a sigmoid curve to better separate ink from paper
    k = 10
    img = np.sqrt(1 / (1 + np.exp(k * (img - 0.5))))

    # imageio.imsave("chars_overstrike_rot.png", img)
    loaded_files[filename] = img
    return img


# Pull out the image of a character-pair, c1 overstruck with c2.
# The character at (x, y) should be the same as the character at (y, x) but due to printing may be slightly
# different.  We just analyze them all anyway, and it'll sort out in the final mapping.
# (Could double the performance by folding in half, but we don't care really) 
Example #26
Source File: preprocess.py    From 2018DSB with MIT License 5 votes vote down vote up
def get_affine_transform_matrix(h, w, rotation_range=0, width_shift_range=0,
                                height_shift_range=0, shear_range=0, zoom_range=0):
    rotation = np.deg2rad(np.random.uniform(-rotation_range, rotation_range))
    wshift = np.random.uniform(-width_shift_range, width_shift_range)*w
    hshift = np.random.uniform(-height_shift_range,height_shift_range)*h
    shear = np.deg2rad(np.random.uniform(-shear_range, shear_range))
    if np.isscalar(zoom_range):
        zoom_range = [1, zoom_range]
    hzoom, wzoom = np.random.uniform(zoom_range[0], zoom_range[1], 2)
    
    matrix = AffineTransform(scale = (wzoom, hzoom), rotation = rotation,
                             shear = shear, translation = (wshift, hshift))._inv_matrix
    matrix = transform_matrix_offset_center(matrix, h, w)
    return matrix 
Example #27
Source File: bm_comp_perform.py    From BIRL with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def register_image_pair(idx, path_img_target, path_img_source, path_out):
    """ register two images together

    :param int idx: empty parameter for using the function in parallel
    :param str path_img_target: path to the target image
    :param str path_img_source: path to the source image
    :param str path_out: path for exporting the output
    :return tuple(str,float):
    """
    start = time.time()
    # load and denoise reference image
    img_target = io.imread(path_img_target)
    img_target = denoise_wavelet(img_target, wavelet_levels=7, multichannel=True)
    img_target_gray = rgb2gray(img_target)

    # load and denoise moving image
    img_source = io.imread(path_img_source)
    img_source = denoise_bilateral(img_source, sigma_color=0.05,
                                   sigma_spatial=2, multichannel=True)
    img_source_gray = rgb2gray(img_source)

    # detect ORB features on both images
    detector_target = ORB(n_keypoints=150)
    detector_source = ORB(n_keypoints=150)
    detector_target.detect_and_extract(img_target_gray)
    detector_source.detect_and_extract(img_source_gray)
    matches = match_descriptors(detector_target.descriptors,
                                detector_source.descriptors)
    # robustly estimate affine transform model with RANSAC
    model, _ = ransac((detector_target.keypoints[matches[:, 0]],
                       detector_source.keypoints[matches[:, 1]]),
                      AffineTransform, min_samples=25, max_trials=500,
                      residual_threshold=0.95)

    # warping source image with estimated transformations
    img_warped = warp(img_target, model.inverse, output_shape=img_target.shape[:2])
    path_img_warped = os.path.join(path_out, NAME_IMAGE_WARPED % idx)
    io.imsave(path_img_warped, img_warped)
    # summarise experiment
    execution_time = time.time() - start
    return path_img_warped, execution_time 
Example #28
Source File: match_images.py    From models with Apache License 2.0 4 votes vote down vote up
def main(unused_argv):
  # Read features.
  locations_1, _, descriptors_1, _, _ = feature_io.ReadFromFile(
      cmd_args.features_1_path)
  num_features_1 = locations_1.shape[0]
  print(f"Loaded image 1's {num_features_1} features")
  locations_2, _, descriptors_2, _, _ = feature_io.ReadFromFile(
      cmd_args.features_2_path)
  num_features_2 = locations_2.shape[0]
  print(f"Loaded image 2's {num_features_2} features")

  # Find nearest-neighbor matches using a KD tree.
  d1_tree = spatial.cKDTree(descriptors_1)
  _, indices = d1_tree.query(
      descriptors_2, distance_upper_bound=_DISTANCE_THRESHOLD)

  # Select feature locations for putative matches.
  locations_2_to_use = np.array([
      locations_2[i,]
      for i in range(num_features_2)
      if indices[i] != num_features_1
  ])
  locations_1_to_use = np.array([
      locations_1[indices[i],]
      for i in range(num_features_2)
      if indices[i] != num_features_1
  ])

  # Perform geometric verification using RANSAC.
  _, inliers = measure.ransac((locations_1_to_use, locations_2_to_use),
                              transform.AffineTransform,
                              min_samples=3,
                              residual_threshold=20,
                              max_trials=1000)

  print(f'Found {sum(inliers)} inliers')

  # Visualize correspondences, and save to file.
  _, ax = plt.subplots()
  img_1 = mpimg.imread(cmd_args.image_1_path)
  img_2 = mpimg.imread(cmd_args.image_2_path)
  inlier_idxs = np.nonzero(inliers)[0]
  feature.plot_matches(
      ax,
      img_1,
      img_2,
      locations_1_to_use,
      locations_2_to_use,
      np.column_stack((inlier_idxs, inlier_idxs)),
      matches_color='b')
  ax.axis('off')
  ax.set_title('DELF correspondences')
  plt.savefig(cmd_args.output_image) 
Example #29
Source File: __init__.py    From sima with GNU General Public License v2.0 4 votes vote down vote up
def estimate_array_transform(source, target, method='affine'):
    """Calculate an affine transformation from source array to target array.

    Parameters
    ----------
    source : array
        The image to transform
    target : array
        The image used as the template for the transformation
    method : string, optional
        Method to use for transform estimation.

    Returns
    -------
    transform : skimage.transform._geometric.GeometricTransform
        An skimage transform object.

    See Also
    --------
    cv2.estimateRigidTransform
    skimage.transform

    """

    if method == 'affine':
        if not cv2_available:
            raise ImportError('OpenCV >= 2.4.8 required')

        slice_ = tuple(slice(0, min(source.shape[i], target.shape[i]))
                       for i in range(2))
        transform = cv2.estimateRigidTransform(
            to8bit(source[slice_]),
            to8bit(target[slice_]), True)

        if transform is None:
            raise TransformError('Cannot calculate affine transformation ' +
                                 'from source to target')
        else:
            # TODO: make sure the order is correct
            transform_matrix = np.vstack((transform, [0, 0, 1]))
            return tf.AffineTransform(matrix=transform_matrix)
    else:
        raise ValueError('Unrecognized transform method: {}'.format(method)) 
Example #30
Source File: reco_loader.py    From Text-Recognition with GNU Lesser General Public License v2.1 4 votes vote down vote up
def __init__(self, config, type_, channels, profiler = None, **kwargs):

		super().__init__(config, type_, profiler = profiler, **kwargs)
		self.seed()

		self.vertical_flip_prob = self.config['augmentation']['vertical_flip_prob']
		self.horizontal_flip_prob = self.config['augmentation']['horizontal_flip_prob']

		self.distort_prob = config['augmentation']['distort_prob']
		self.hori_tile, self.verti_tile = config['augmentation']['horizontal_tiles'], config['augmentation']['vertical_tiles'] 
		self.distort_mag = config['augmentation']['magnitude']

		self.shear_prob = config['augmentation']['shear_prob']
		self.afine_tf = tf.AffineTransform(shear = config['augmentation']['shear_mag'])

		if type_ == 'train':
			self.art_generator = ArtificialGen(config, self.all_characters)
			self.art_prob = config['augmentation']['art_prob_train']
		elif type_ == 'test':
			self.art_generator = ArtificialGen(config, self.all_characters)
			self.art_prob = config['augmentation']['art_prob_train']
			self.art_prob_test = config['augmentation']['art_prob_test']
			# self.char_distri = dict(zip(self.all_characters, [0]*len(self.all_characters)))

		self.num_channels = channels
		print("Working on",channels,"channeled images for",self.Type)

		if config['varying_width']:
			self.list_or_tensor = 'list'
		else:
			self.list_or_tensor = 'tensor'

		if self.Type != 'test_one' and 'OWN' in self.datasets and len(self.datasets) == 1 and self.Type == 'train':
			#do something
			self.only_OWN = True
			if os.path.exists(self.config['cache_path']+'/OWN_'+self.Type+'_cache.pkl'):
				with open(self.config['cache_path']+'/OWN_'+self.Type+'_cache.pkl', 'rb') as f:
					self.own_cache = pickle.load(f)
					# print(len(self.own_cache['images']))
			else:
				self.make_own_cache()			
		else:
			self.only_OWN = False

		if self.Type == 'test':
			if self.config['grayscale']:
				c = '_1'
			else:
				c = '_3'
			path = self.config['cache_path'] + '/' + '_'.join(self.datasets)+'_'+self.Type+c+'_'+self.config['project']+'.pkl'
			if os.path.exists(path):
				with open(path, 'rb') as f:
					self.test_cache = pickle.load(f)
					print("Loaded test cache")
				# print("Character Distribution:\n",self.test_cache['char_distribution'],'\n')
			else:
				self.make_test_cache()