Loading autocnet/matcher/naive_template.py +9 −17 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 autocnet/matcher/tests/test_naive_template.py +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 Loading Loading @@ -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 autocnet/transformation/roi.py +5 −4 Original line number Diff line number Diff line Loading @@ -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: Loading @@ -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, Loading Loading
autocnet/matcher/naive_template.py +9 −17 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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
autocnet/matcher/tests/test_naive_template.py +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 Loading Loading @@ -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
autocnet/transformation/roi.py +5 −4 Original line number Diff line number Diff line Loading @@ -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: Loading @@ -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, Loading