Commit c8385245 authored by Kelvin Rodriguez's avatar Kelvin Rodriguez
Browse files

updated tests

parent 1a04a857
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -117,6 +117,8 @@ def pattern_match(template, image, upsampling=16, metric=cv2.TM_CCOEFF_NORMED, e
    if upsampling < 1:
        raise ValueError

    print(template)
    print(image)
    # Fit a 3rd order polynomial to upsample the images
    if upsampling != 1:
        u_template = zoom(template, upsampling, order=3)
@@ -128,6 +130,8 @@ def pattern_match(template, image, upsampling=16, metric=cv2.TM_CCOEFF_NORMED, e
    result = cv2.matchTemplate(u_image, u_template, method=metric)
    _, max_corr, min_loc, max_loc = cv2.minMaxLoc(result)
    
    print("min/max loc: ", min_loc, max_loc)

    if metric == cv2.TM_SQDIFF or metric == cv2.TM_SQDIFF_NORMED:
        x, y = (min_loc[0], min_loc[1])
    else:
@@ -141,6 +145,9 @@ def pattern_match(template, image, upsampling=16, metric=cv2.TM_CCOEFF_NORMED, e
    y += (u_template.shape[0] / 2.)
    x += (u_template.shape[1] / 2.)

    x = (x - ideal_x) / upsampling
    y = (y - ideal_y) / upsampling
    print("x,ideal x, ", x, ideal_x, (x - ideal_x) / upsampling)
    print("y,ideal y, ", y, ideal_y, (y - ideal_y) / upsampling)

    x = (ideal_x - x) / upsampling
    y = (ideal_y - y) / upsampling
    return x, y, max_corr, result
+6 −59
Original line number Diff line number Diff line
@@ -149,59 +149,6 @@ def check_image_size(imagesize):
    return x,y


def clip_roi(img, center_x, center_y, size_x=200, size_y=200, dtype="uint64"):
    """
    Given an input image, clip a square region of interest
    centered on some pixel at some size.
    Parameters
    ----------
    img : ndarray or object
          The input image to be clipped or an object
          with a read_array method that takes a pixels
          argument in the form [xstart, ystart, xstop, ystop]
    center_x : Numeric
               The x coordinate to the center of the roi
    center_y : Numeric
               The y coordinate to the center of the roi
    img_size : int
               1/2 of the total image size. This value is the
               number of pixels grabbed from each side of the center
    Returns
    -------
    clipped_img : ndarray
                  The clipped image
    """

    try:
        raster_size = img.raster_size
    except:
        # x,y form
        raster_size = img.shape[::-1]
    axr, ax = modf(center_x)
    ayr, ay = modf(center_y)

    if ax + size_x > raster_size[0]:
        size_x = floor(raster_size[0] - center_x)
    if ax - size_x < 0:
        size_x = int(ax)
    if ay + size_y > raster_size[1]:
        size_y =floor(raster_size[1] - center_y)
    if ay - size_y < 0:
        size_y = int(ay)

    # Read from the upper left origin
    pixels = [ax-size_x, ay-size_y, size_x*2, size_y*2]
    pixels = list(map(int, pixels))  #
    if isinstance(img, np.ndarray):
        subarray = img[pixels[1]:pixels[1] + pixels[3] + 1, pixels[0]:pixels[0] + pixels[2] + 1]
    else:
        try:
            subarray = img.read_array(pixels=pixels, dtype=dtype)
        except:
            return None, 0, 0
    return subarray, axr, ayr


def subpixel_phase(reference_roi, moving_roi, affine=tf.AffineTransform(), **kwargs):
    """
    Apply the spectral domain matcher to a search and template image. To
@@ -262,7 +209,7 @@ def subpixel_phase(reference_roi, moving_roi, affine=tf.AffineTransform(), **kwa
                                                                                **kwargs)
    # get shifts in input pixel space 
    shift_x, shift_y = affine.inverse([shift_x, shift_y])[0]
    new_affine = tf.AffineTransform(translation=(shift_x, shift_y))
    new_affine = tf.AffineTransform(translation=(-shift_x, -shift_y))
    return new_affine, error, diffphase


@@ -1563,11 +1510,11 @@ def fourier_mellen(ref_image, moving_image, affine=tf.AffineTransform(), verbose
      Error returned by the iterative phase matcher
    """
    # Get the affine transformation for scale + rotation invariance
    affine = estimate_logpolar_transform(ref_image.array(), moving_image.array(), verbose=verbose)
    affine = estimate_logpolar_transform(ref_image.array, moving_image.array, verbose=verbose)

    # warp the source image to match the destination
    ref_warped = tf.warp(ref_image.array(), affine)
    sx, sy = affine.inverse(np.asarray(ref_image.array().shape)/2)[0]
    ref_warped = ref_image.clip(affine)
    sx, sy = affine.inverse(np.asarray(ref_image.array.shape)/2)[0]

    # get translation with iterative phase
    newx, newy, error = iterative_phase(sx, sy, sx, sy, ref_warped, moving_image, **phase_kwargs)
@@ -1583,8 +1530,8 @@ def fourier_mellen(ref_image, moving_image, affine=tf.AffineTransform(), verbose

        ax[2].imshow(ref_image)
        ax[2].set_title("Image 1")
        ax[2].axhline(y=ref_image.array().shape[0]/2, color="red", linestyle="-", alpha=1, linewidth=1)
        ax[2].axvline(x=ref_image.array().shape[1]/2, color="red", linestyle="-", alpha=1, linewidth=1)
        ax[2].axhline(y=ref_image.array.shape[0]/2, color="red", linestyle="-", alpha=1, linewidth=1)
        ax[2].axvline(x=ref_image.array.shape[1]/2, color="red", linestyle="-", alpha=1, linewidth=1)

        ax[1].imshow(moving_image)
        ax[3].imshow(moving_image)
+19 −32
Original line number Diff line number Diff line
@@ -48,30 +48,19 @@ def test_prep_subpixel(nmatches, nstrengths):
    assert arrs[2].shape == (nmatches, nstrengths)
    assert arrs[0][0] == 0

@pytest.mark.parametrize("center_x, center_y, size, expected", [(4, 4, 9, 404),
                                                          (55.4, 63.1, 27, 6355)])
def test_clip_roi(center_x, center_y, size, expected):
    img = np.arange(10000).reshape(100, 100)

    clip, axr, ayr = sp.clip_roi(img, center_x, center_y, size)

    assert clip.mean() == expected


def test_subpixel_template(apollo_subsets):
    a = apollo_subsets[0]
    b = apollo_subsets[1]
    
    ref_roi = roi.Roi(a, a.shape[0]/2, a.shape[1]/2, 10, 10)
    moving_roi = roi.Roi(b, math.floor(b.shape[0]/2), math.floor(b.shape[1]/2), 51, 51)
    ref_roi = roi.Roi(a, a.shape[0]/2, a.shape[1]/2, 41, 41)
    moving_roi = roi.Roi(b, math.floor(b.shape[0]/2), math.floor(b.shape[1]/2), 11, 11)

    affine, strength, corrmap = sp.subpixel_template(ref_roi, moving_roi, upsampling=16)
    print(corrmap)
    print(affine)

    assert strength >= 0.99
    assert affine.translation[0] == 80.0625
    assert affine.translation[1] == 82
    assert strength >= 0.80
    assert affine.translation[0] == -0.0625
    assert affine.translation[1] == 2.0625



@@ -82,16 +71,14 @@ def test_subpixel_transformed_template(apollo_subsets):
    moving_center = math.floor(b.shape[0]/2), math.floor(b.shape[1]/2)
    transform = tf.AffineTransform(rotation=math.radians(1), scale=(1.1,1.1))
    ref_roi = roi.Roi(a, a.shape[0]/2, a.shape[1]/2, 10, 10)
    moving_roi = roi.Roi(b, *moving_center, 51, 51)
    moving_roi = roi.Roi(b, *moving_center, 21, 21)

    # with patch('autocnet.transformation.roi.Roi.clip', side_effect=clip_side_effect):
    affine, strength, corrmap = sp.subpixel_template(ref_roi, moving_roi, transform, upsampling=16)
    
    print(corrmap)
    print(affine)
    assert strength >= 0.83
    assert affine.translation[0] == pytest.approx(70.68980522)
    assert affine.translation[1] == pytest.approx(68.20849946)
    assert affine.translation[0] == pytest.approx(-19.0882595)
    assert affine.translation[1] == pytest.approx(-19.2152450)


def test_estimate_logpolar_transform(iris_pair):
@@ -106,18 +93,21 @@ def test_estimate_logpolar_transform(iris_pair):

def test_fourier_mellen(iris_pair):
    img1, img2 = iris_pair 
    nx, ny, error = sp.fourier_mellen(img1, img2, phase_kwargs = {"reduction" : 11, "size":(401, 401), "convergence_threshold" : 1, "max_dist":100}) 

    ref_roi = roi.Roi(img1, img1.shape[1]/2, img1.shape[0]/2, 401, 401)
    moving_roi = roi.Roi(img2, img2.shape[1]/2, img2.shape[0]/2, 401, 401)
    nx, ny, error = sp.fourier_mellen(ref_roi, moving_roi, phase_kwargs = {"reduction" : 11, "size":(401, 401), "convergence_threshold" : 1, "max_dist":100}) 
    
    assert pytest.approx(nx, 0.01) == 996.39 
    assert pytest.approx(ny, 0.01) ==  984.912 
    assert pytest.approx(error, 0.01) == 0.0422 


@pytest.mark.parametrize("convergence_threshold, expected", [(2.0, (50.49, 52.44, -9.5e-20))])
@pytest.mark.parametrize("convergence_threshold, expected", [(2.0, (50.51, 48.57, -9.5e-20))])
def test_iterative_phase(apollo_subsets, convergence_threshold, expected):
    reference_image = apollo_subsets[0]
    walking_image = apollo_subsets[1]
    image_size = (51, 51)
    image_size = (31, 31)

    ref_x, ref_y = reference_image.shape[0]/2, reference_image.shape[1]/2
    walk_x, walk_y = walking_image.shape[0]/2, walking_image.shape[1]/2
@@ -146,7 +136,7 @@ def test_check_image_size(data, expected):
    (4, 3, 4, 3, (3,3), (2,2), (4,3)),  # Increase the search image size
    (4, 3, 4, 3, (3,3), (2,2), (4,3)), # Increase the template size
    (4, 3, 3, 3, (3,3), (2,2), (4,3)), # Move point in the x-axis
    (4, 3, 5, 4, (3,3), (2,2), (4,3)), # Move point in the other x-direction
    (4, 3, 5, 3, (3,3), (2,2), (4,3)), # Move point in the other x-direction
    (4, 3, 4, 2, (3,3), (2,2), (4,3)), # Move point negative in the y-axis
    (4, 3, 4, 4, (3,3), (2,2), (4,3))  # Move point positive in the y-axis

@@ -178,10 +168,9 @@ def test_subpixel_template_cooked(x, y, x1, y1, image_size, template_size, expec
    ref_roi = roi.Roi(test_image, x, y, *image_size)
    moving_roi = roi.Roi(t_shape, x1, y1, *template_size)
    new_affine, corr, corrmap = sp.subpixel_template(ref_roi, moving_roi, upsampling=1)
    print(new_affine)
    nx, ny = new_affine.inverse([x1,y1])[0]
    # should be 1.0 
    assert corr >= .99  # geq because sometime returning weird float > 1 from OpenCV
    nx, ny = new_affine([x1,y1])[0]
    # should be 1.0, but in one test the windos has extra 1's so correlation goes down
    assert corr >= .8  # geq because sometime returning weird float > 1 from OpenCV
    assert nx == expected[0]
    assert ny == expected[1]

@@ -191,7 +180,6 @@ def test_subpixel_template_cooked(x, y, x1, y1, image_size, template_size, expec
    (4, 3, 3, 2, (2,2), (3,2)), # Increase the template size
    (4, 3, 2, 2, (2,2), (3,2)), # Move point in the x-axis
    (4, 3, 4, 3, (2,2), (3,2)), # Move point in the other x-direction
    (4, 3, 3, 1, (2,2), (3,2)), # Move point negative in the y-axis; also tests size reduction
    (4, 3, 3, 3, (2,2), (3,2))  # Move point positive in the y-axis

])
@@ -210,7 +198,6 @@ def test_subpixel_phase_cooked(x, y, x1, y1, image_size, expected):
                           (0, 1, 1, 1, 0, 0, 1, 0, 1),
                           (0, 0, 0, 0, 0, 0, 1, 1, 1)), dtype=np.uint8)

    # Should yield (-3, 3) offset from image center
    t_shape = np.array(((0, 0, 0, 0, 0, 0, 0),
                        (0, 0, 1, 1, 1, 0, 0),
                        (0, 0, 0, 1, 0, 0, 0),
@@ -222,6 +209,6 @@ def test_subpixel_phase_cooked(x, y, x1, y1, image_size, expected):
    walking_roi = roi.Roi(t_shape, x1, y1, size_x=image_size[0], size_y=image_size[1])

    affine, metrics, _ = sp.subpixel_phase(reference_roi, walking_roi)
    dx, dy = affine.inverse((x1, y1))[0]
    dx, dy = affine((x1, y1))[0]
    assert dx == expected[0]
    assert dy == expected[1]
+3 −8
Original line number Diff line number Diff line
@@ -127,14 +127,9 @@ class Roi():
        top_y = self._y - self.size_y
        bottom_y = self._y + self.size_y

        if left_x < 0:
            left_x = 0
        if top_y < 0:
            top_y = 0
        if right_x > raster_size[0]:
            right_x = raster_size[0]
        if bottom_y > raster_size[1]:
            bottom_y = raster_size[1]
        if left_x < 0 or top_y < 0 or right_x > raster_size[0] or bottom_y > raster_size[1]:
            raise IndexError(f"Input window size {(self.size_x, self.size_y)}) at center {(self.x, self.y)} is out of the image bounds") 

        print("extents:", list(map(int, [left_x, right_x, top_y, bottom_y])))
        print("center:", self.x, self.y, self.size_x, self.size_y) 
        return list(map(int, [left_x, right_x, top_y, bottom_y]))