Commit 778afd52 authored by Adoram-Kershner, Lauren's avatar Adoram-Kershner, Lauren
Browse files

Merge branch 'subpixelapi' into 'subpixelapi'

Group fixes plus new test

See merge request astrogeology/autocnet!646
parents ab86aa1f 0be0f2de
Loading
Loading
Loading
Loading
+9 −17
Original line number Diff line number Diff line
@@ -143,9 +143,11 @@ def pattern_match(template, image, upsampling=8, metric=cv2.TM_CCOEFF_NORMED, er
    Returns
    -------
    x : float
        The x offset
        The x offset of the template center the image center, the inverse of this shift will 
        need to be applied to the template as a correction.
    y : float
        The y offset
        The y offset of the template center to the image center, the inverse of this shift
        will need to be applied to the template as a correction.
    max_corr : float
               The strength of the correlation in the range [-1, 1].
    result : ndarray
@@ -165,24 +167,14 @@ def pattern_match(template, image, upsampling=8, metric=cv2.TM_CCOEFF_NORMED, er
        u_image = image
    corrmap = cv2.matchTemplate(u_image, u_template, method=metric)
    

    width = (np.asarray(u_image.shape) - np.asarray(corrmap.shape)) // 2  # This needs to be an integer

    # Pad the result array with values outside the valid correlation range
    result = np.pad(corrmap, 
                    ((width[0], width[0]),(width[1], width[1])),
                    mode='constant')  # pads zeros

    if metric == cv2.TM_SQDIFF or metric == cv2.TM_SQDIFF_NORMED:
        matched_y, matched_x = np.unravel_index(np.argmin(result), result.shape)
        matched_y, matched_x = np.unravel_index(np.argmin(corrmap), corrmap.shape)
    else:
        matched_y, matched_x = np.unravel_index(np.argmax(result), result.shape)
        matched_y, matched_x = np.unravel_index(np.argmax(corrmap), corrmap.shape)

    max_corr = result[matched_y, matched_x]
    max_corr = corrmap[matched_y, matched_x]

    # Since the center of the image is (0,0), shift the datum from the upper left to the center.
    u_image_center_y, u_image_center_x = (np.asarray(u_image.shape) - 1) // 2
    shift_y = (matched_y - u_image_center_y) / upsampling
    shift_x = (matched_x - u_image_center_x) / upsampling
    shift_x = (matched_x - ((corrmap.shape[1]-1)/2)) / upsampling
    shift_y = (matched_y - ((corrmap.shape[0]-1)/2)) / upsampling

    return shift_x, shift_y , max_corr, corrmap
+22 −0
Original line number Diff line number Diff line
import pytest

import unittest
import itertools
from .. import naive_template
from autocnet.transformation import roi 
import numpy as np
import cv2

@@ -112,6 +114,26 @@ class TestNaiveTemplate(unittest.TestCase):
        # Test Correlation Strength: At least 0.8
        self.assertGreaterEqual(result_strength, 0.8, "Returned Correlation Strength of %d" % result_strength)

    def test_imposed_shift(self):
        # specifically testing whole numbers, partial numbers (either in image location or
        # tested shifts) would cause the ROI class to pixel lock (reintorpolate) the ROI
        # and this is not the place to test that logic.
        x, y = (5, 6)

        delta_xs = [1, 0, -1] 
        delta_ys = [-2, -1, 0, 1, 2]
        for delta_x, delta_y in itertools.product(delta_xs, delta_ys):
            xi = x + delta_x
            yi = y + delta_y

            ref_roi = roi.Roi(self._test_image, x, y, 4, 5, buffer=1)
            moving_roi = roi.Roi(self._test_image, xi, yi, 3, 3, buffer=0)

            shift_x, shift_y, metrics, corrmap = naive_template.pattern_match(moving_roi.clip(), ref_roi.clip(), upsampling=1)
            
            self.assertEqual(shift_x, float(delta_x))
            self.assertEqual(shift_y, float(delta_y))
    
    def tearDown(self):
        pass
+5 −4
Original line number Diff line number Diff line
@@ -227,10 +227,11 @@ class Roi():
                         (self.buffer*2) + self._remainder_y + (self.size_y*2), 
                         (self.size_y*2+1)+(self.buffer*2))

        # the xi, yi are intentionally handed in backward, because the map_coordinates indexes column major
        pixel_locked = ndimage.map_coordinates(data, 
                                       np.meshgrid(xi, yi, indexing='ij'),
                                       mode='constant',
                                       cval=0.0,
                                       np.meshgrid(yi, xi, indexing='ij'),
                                       mode=mode,
                                       cval=-10.0,
                                       order=3)

        if affine:
@@ -240,7 +241,7 @@ class Roi():
            #                      f"{((self.size_y * 2) + 1, (self.size_x * 2) + 1)} was asked for. Select, " +
            #                      "a smaller region of interest" )

            pixel_locked = tf.warp(pixel_locked, affine.inverse, order=3, mode=mode, cval=0)
            pixel_locked = tf.warp(pixel_locked, affine.inverse, order=3, mode=mode, cval=-10)

        if self.buffer != 0:
            return img_as_float32(pixel_locked[self.buffer:-self.buffer,