Loading autocnet/graph/edge.py +1 −2 Original line number Diff line number Diff line Loading @@ -4,12 +4,11 @@ from collections import MutableMapping import numpy as np import pandas as pd from autocnet.cg import cg from autocnet.matcher import health from autocnet.matcher import outlier_detector as od from autocnet.matcher import suppression_funcs as spf from autocnet.matcher import subpixel as sp from autocnet.matcher.matcher import FlannMatcher from autocnet.matcher.feature import FlannMatcher from autocnet.transformation.transformations import FundamentalMatrix, Homography from autocnet.vis.graph_view import plot_edge from autocnet.cg import cg Loading autocnet/matcher/matcher.py→autocnet/matcher/feature.py +1 −77 Original line number Diff line number Diff line Loading @@ -2,84 +2,9 @@ import warnings import cv2 import pandas as pd import numpy as np import math from scipy.misc import imresize from scipy.ndimage.interpolation import zoom FLANN_INDEX_KDTREE = 1 # Algorithm to set centers, DEFAULT_FLANN_PARAMETERS = dict(algorithm=FLANN_INDEX_KDTREE, trees=3) def pattern_match(template, image, upsampling=16, func=cv2.TM_CCOEFF_NORMED, error_check=False): """ Call an arbitrary pattern matcher Parameters ---------- template : ndarray The input search template used to 'query' the destination image image : ndarray The image or sub-image to be searched upsampling : int The multiplier to upsample the template and image. func : object The function to be used to perform the template based matching Options: {cv2.TM_CCORR_NORMED, cv2.TM_CCOEFF_NORMED, cv2.TM_SQDIFF_NORMED} In testing the first two options perform significantly better with Apollo data. error_check : bool If True, also apply a different matcher and test that the values are not too divergent. Default, False. Returns ------- x : float The x offset y : float The y offset strength : float The strength of the correlation in the range [-1, 1]. """ different = {cv2.TM_SQDIFF_NORMED: cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR_NORMED: cv2.TM_SQDIFF_NORMED, cv2.TM_CCOEFF_NORMED: cv2.TM_SQDIFF_NORMED} if upsampling < 1: raise ValueError u_template = zoom(template, upsampling, order=3) u_image = zoom(image, upsampling, order=3) result = cv2.matchTemplate(u_image, u_template, method=func) min_corr, max_corr, min_loc, max_loc = cv2.minMaxLoc(result) if func == cv2.TM_SQDIFF or func == cv2.TM_SQDIFF_NORMED: x, y = (min_loc[0], min_loc[1]) else: x, y = (max_loc[0], max_loc[1]) # Compute the idealized shift (image center) ideal_y = u_image.shape[0] / 2 ideal_x = u_image.shape[1] / 2 # Compute the shift from template upper left to template center y += (u_template.shape[0] / 2) x += (u_template.shape[1] / 2) x = (ideal_x - x) / upsampling y = (ideal_y - y) / upsampling return x, y, max_corr DEFAULT_FLANN_PARAMETERS = dict(algorithm=FLANN_INDEX_KDTREE, trees=3) class FlannMatcher(object): Loading Loading @@ -182,4 +107,3 @@ class FlannMatcher(object): return pd.DataFrame(matched, columns=['source_image', 'source_idx', 'destination_image', 'destination_idx', 'distance']) autocnet/matcher/naive_template.py 0 → 100644 +71 −0 Original line number Diff line number Diff line import cv2 from scipy.ndimage.interpolation import zoom def pattern_match(template, image, upsampling=16, func=cv2.TM_CCOEFF_NORMED, error_check=False): """ Call an arbitrary pattern matcher Parameters ---------- template : ndarray The input search template used to 'query' the destination image image : ndarray The image or sub-image to be searched upsampling : int The multiplier to upsample the template and image. func : object The function to be used to perform the template based matching Options: {cv2.TM_CCORR_NORMED, cv2.TM_CCOEFF_NORMED, cv2.TM_SQDIFF_NORMED} In testing the first two options perform significantly better with Apollo data. error_check : bool If True, also apply a different matcher and test that the values are not too divergent. Default, False. Returns ------- x : float The x offset y : float The y offset strength : float The strength of the correlation in the range [-1, 1]. """ different = {cv2.TM_SQDIFF_NORMED: cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR_NORMED: cv2.TM_SQDIFF_NORMED, cv2.TM_CCOEFF_NORMED: cv2.TM_SQDIFF_NORMED} if upsampling < 1: raise ValueError u_template = zoom(template, upsampling, order=3) u_image = zoom(image, upsampling, order=3) result = cv2.matchTemplate(u_image, u_template, method=func) min_corr, max_corr, min_loc, max_loc = cv2.minMaxLoc(result) if func == cv2.TM_SQDIFF or func == cv2.TM_SQDIFF_NORMED: x, y = (min_loc[0], min_loc[1]) else: x, y = (max_loc[0], max_loc[1]) # Compute the idealized shift (image center) ideal_y = u_image.shape[0] / 2 ideal_x = u_image.shape[1] / 2 # Compute the shift from template upper left to template center y += (u_template.shape[0] / 2) x += (u_template.shape[1] / 2) x = (ideal_x - x) / upsampling y = (ideal_y - y) / upsampling return x, y, max_corr autocnet/matcher/subpixel.py +2 −2 Original line number Diff line number Diff line import numpy as np from autocnet.matcher import matcher from autocnet.matcher import naive_template # TODO: look into KeyPoint.size and perhaps use to determine an appropriately-sized search/template. Loading Loading @@ -74,7 +74,7 @@ def subpixel_offset(template, search, **kwargs): Strength of the correspondence in the range [-1, 1] """ x_offset, y_offset, strength = matcher.pattern_match(template, search, **kwargs) x_offset, y_offset, strength = naive_template.pattern_match(template, search, **kwargs) return x_offset, y_offset, strength ''' Loading autocnet/matcher/tests/test_matcher.py +2 −2 Original line number Diff line number Diff line Loading @@ -5,7 +5,7 @@ import warnings import cv2 from .. import matcher from .. import feature from autocnet.examples import get_path sys.path.append(os.path.abspath('..')) Loading @@ -25,7 +25,7 @@ class TestMatcher(unittest.TestCase): self.fd['AS15-M-0297_SML.png'] = sift.detectAndCompute(im2, None) def test_flann_match_k_eq_2(self): fmatcher = matcher.FlannMatcher() fmatcher = feature.FlannMatcher() source_image = self.fd['AS15-M-0296_SML.png'] fmatcher.add(source_image[1], 0) Loading Loading
autocnet/graph/edge.py +1 −2 Original line number Diff line number Diff line Loading @@ -4,12 +4,11 @@ from collections import MutableMapping import numpy as np import pandas as pd from autocnet.cg import cg from autocnet.matcher import health from autocnet.matcher import outlier_detector as od from autocnet.matcher import suppression_funcs as spf from autocnet.matcher import subpixel as sp from autocnet.matcher.matcher import FlannMatcher from autocnet.matcher.feature import FlannMatcher from autocnet.transformation.transformations import FundamentalMatrix, Homography from autocnet.vis.graph_view import plot_edge from autocnet.cg import cg Loading
autocnet/matcher/matcher.py→autocnet/matcher/feature.py +1 −77 Original line number Diff line number Diff line Loading @@ -2,84 +2,9 @@ import warnings import cv2 import pandas as pd import numpy as np import math from scipy.misc import imresize from scipy.ndimage.interpolation import zoom FLANN_INDEX_KDTREE = 1 # Algorithm to set centers, DEFAULT_FLANN_PARAMETERS = dict(algorithm=FLANN_INDEX_KDTREE, trees=3) def pattern_match(template, image, upsampling=16, func=cv2.TM_CCOEFF_NORMED, error_check=False): """ Call an arbitrary pattern matcher Parameters ---------- template : ndarray The input search template used to 'query' the destination image image : ndarray The image or sub-image to be searched upsampling : int The multiplier to upsample the template and image. func : object The function to be used to perform the template based matching Options: {cv2.TM_CCORR_NORMED, cv2.TM_CCOEFF_NORMED, cv2.TM_SQDIFF_NORMED} In testing the first two options perform significantly better with Apollo data. error_check : bool If True, also apply a different matcher and test that the values are not too divergent. Default, False. Returns ------- x : float The x offset y : float The y offset strength : float The strength of the correlation in the range [-1, 1]. """ different = {cv2.TM_SQDIFF_NORMED: cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR_NORMED: cv2.TM_SQDIFF_NORMED, cv2.TM_CCOEFF_NORMED: cv2.TM_SQDIFF_NORMED} if upsampling < 1: raise ValueError u_template = zoom(template, upsampling, order=3) u_image = zoom(image, upsampling, order=3) result = cv2.matchTemplate(u_image, u_template, method=func) min_corr, max_corr, min_loc, max_loc = cv2.minMaxLoc(result) if func == cv2.TM_SQDIFF or func == cv2.TM_SQDIFF_NORMED: x, y = (min_loc[0], min_loc[1]) else: x, y = (max_loc[0], max_loc[1]) # Compute the idealized shift (image center) ideal_y = u_image.shape[0] / 2 ideal_x = u_image.shape[1] / 2 # Compute the shift from template upper left to template center y += (u_template.shape[0] / 2) x += (u_template.shape[1] / 2) x = (ideal_x - x) / upsampling y = (ideal_y - y) / upsampling return x, y, max_corr DEFAULT_FLANN_PARAMETERS = dict(algorithm=FLANN_INDEX_KDTREE, trees=3) class FlannMatcher(object): Loading Loading @@ -182,4 +107,3 @@ class FlannMatcher(object): return pd.DataFrame(matched, columns=['source_image', 'source_idx', 'destination_image', 'destination_idx', 'distance'])
autocnet/matcher/naive_template.py 0 → 100644 +71 −0 Original line number Diff line number Diff line import cv2 from scipy.ndimage.interpolation import zoom def pattern_match(template, image, upsampling=16, func=cv2.TM_CCOEFF_NORMED, error_check=False): """ Call an arbitrary pattern matcher Parameters ---------- template : ndarray The input search template used to 'query' the destination image image : ndarray The image or sub-image to be searched upsampling : int The multiplier to upsample the template and image. func : object The function to be used to perform the template based matching Options: {cv2.TM_CCORR_NORMED, cv2.TM_CCOEFF_NORMED, cv2.TM_SQDIFF_NORMED} In testing the first two options perform significantly better with Apollo data. error_check : bool If True, also apply a different matcher and test that the values are not too divergent. Default, False. Returns ------- x : float The x offset y : float The y offset strength : float The strength of the correlation in the range [-1, 1]. """ different = {cv2.TM_SQDIFF_NORMED: cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR_NORMED: cv2.TM_SQDIFF_NORMED, cv2.TM_CCOEFF_NORMED: cv2.TM_SQDIFF_NORMED} if upsampling < 1: raise ValueError u_template = zoom(template, upsampling, order=3) u_image = zoom(image, upsampling, order=3) result = cv2.matchTemplate(u_image, u_template, method=func) min_corr, max_corr, min_loc, max_loc = cv2.minMaxLoc(result) if func == cv2.TM_SQDIFF or func == cv2.TM_SQDIFF_NORMED: x, y = (min_loc[0], min_loc[1]) else: x, y = (max_loc[0], max_loc[1]) # Compute the idealized shift (image center) ideal_y = u_image.shape[0] / 2 ideal_x = u_image.shape[1] / 2 # Compute the shift from template upper left to template center y += (u_template.shape[0] / 2) x += (u_template.shape[1] / 2) x = (ideal_x - x) / upsampling y = (ideal_y - y) / upsampling return x, y, max_corr
autocnet/matcher/subpixel.py +2 −2 Original line number Diff line number Diff line import numpy as np from autocnet.matcher import matcher from autocnet.matcher import naive_template # TODO: look into KeyPoint.size and perhaps use to determine an appropriately-sized search/template. Loading Loading @@ -74,7 +74,7 @@ def subpixel_offset(template, search, **kwargs): Strength of the correspondence in the range [-1, 1] """ x_offset, y_offset, strength = matcher.pattern_match(template, search, **kwargs) x_offset, y_offset, strength = naive_template.pattern_match(template, search, **kwargs) return x_offset, y_offset, strength ''' Loading
autocnet/matcher/tests/test_matcher.py +2 −2 Original line number Diff line number Diff line Loading @@ -5,7 +5,7 @@ import warnings import cv2 from .. import matcher from .. import feature from autocnet.examples import get_path sys.path.append(os.path.abspath('..')) Loading @@ -25,7 +25,7 @@ class TestMatcher(unittest.TestCase): self.fd['AS15-M-0297_SML.png'] = sift.detectAndCompute(im2, None) def test_flann_match_k_eq_2(self): fmatcher = matcher.FlannMatcher() fmatcher = feature.FlannMatcher() source_image = self.fd['AS15-M-0296_SML.png'] fmatcher.add(source_image[1], 0) Loading