Unverified Commit 1e2dabd7 authored by jlaura's avatar jlaura Committed by GitHub
Browse files

affine centered on the ROI (#484)

* affine centered on the ROI

* Updates for comments

* remove missed phase args in func signature

* Updates for passing tests

* Updates with tests

* Update for comments.

* Updates for additional comments.

* comments addressed
parent db398139
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -106,7 +106,11 @@ def generate_ground_points(Session, ground_mosaic, nspts_func=lambda x: int(roun
        p = Point(*coord)
        print(f'point {i}'),
 
        
        linessamples = isis.point_info(ground_mosaic.file_name, p.x, p.y, 'ground')
        if linessamples is None:
            print('unable to find point in ground image')
            continue
        line = linessamples[0].get('Line')
        sample = linessamples[0].get('Sample')

@@ -263,6 +267,7 @@ def propagate_point(Session,
        sx, sy = m["sample"], m["line"]

        for i,image in images.iterrows():
            # When grounding to THEMIS the df has a PATH to the QUAD
            dest_image = GeoDataset(image["path"])

            if os.path.basename(m['path']) == os.path.basename(image['path']):
@@ -273,7 +278,6 @@ def propagate_point(Session,
                print(f'prop point: dest_image: {dest_image}')
                print(f'prop point: (sx, sy): ({sx}, {sy})')
                x,y, dist, metrics, corrmap = geom_match(base_image, dest_image, sx, sy, \
                        size_x=size_x, size_y=size_y, \
                        template_kwargs=template_kwargs, \
                        verbose=verbose)
            except Exception as e:
+3 −3
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ def pattern_match_autoreg(template, image, subpixel_size=3, max_scaler=0.2, func

    return x, y, max_corr

def pattern_match(template, image, upsampling=16, func=cv2.TM_CCOEFF_NORMED, error_check=False):
def pattern_match(template, image, upsampling=16, metric=cv2.TM_CCOEFF_NORMED, error_check=False):
    """
    Call an arbitrary pattern matcher using a subpixel approach where the template and image
    are upsampled using a third order polynomial.
@@ -135,10 +135,10 @@ def pattern_match(template, image, upsampling=16, func=cv2.TM_CCOEFF_NORMED, err
        u_template = template
        u_image = image

    result = cv2.matchTemplate(u_image, u_template, method=func)
    result = cv2.matchTemplate(u_image, u_template, method=metric)
    _, max_corr, min_loc, max_loc = cv2.minMaxLoc(result)

    if func == cv2.TM_SQDIFF or func == cv2.TM_SQDIFF_NORMED:
    if metric == cv2.TM_SQDIFF or metric == cv2.TM_SQDIFF_NORMED:
        x, y = (min_loc[0], min_loc[1])
    else:
        x, y = (max_loc[0], max_loc[1])
+291 −175

File changed.

Preview size limit exceeded, changes collapsed.

+33 −11
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ import sys
import unittest
from unittest.mock import patch

from skimage import transform as tf

import pytest

import numpy as np
@@ -36,7 +38,6 @@ def test_clip_roi(center_x, center_y, size, expected):

    assert clip.mean() == expected

def test_subpixel_template(apollo_subsets):
def clip_side_effect(*args, **kwargs):
    if np.array_equal(a, args[0]):
        return a, 0, 0
@@ -48,6 +49,8 @@ def test_subpixel_template(apollo_subsets):
        bx = int(bx)
        by = int(by)
        return b[by-10:by+11, bx-10:bx+11], bxr, byr

def test_subpixel_template(apollo_subsets):
    a = apollo_subsets[0]
    b = apollo_subsets[1]
    with patch('autocnet.matcher.subpixel.clip_roi', side_effect=clip_side_effect):
@@ -59,6 +62,25 @@ def test_subpixel_template(apollo_subsets):
    assert nx == 50.5
    assert ny == 52.4375

def test_estimate_affine_transformation():
    a = [[0,1], [0,0], [1,0], [1,1], [0,1]]
    b = [[1, 2], [1, 1], [2, 1], [2, 2], [1, 2]]
    transform = sp.estimate_affine_transformation(a,b)
    assert isinstance(transform, tf.AffineTransform)

def test_subpixel_transformed_template(apollo_subsets):
    a = apollo_subsets[0]
    b = apollo_subsets[1]
    transform = tf.AffineTransform(rotation=math.radians(1), scale=(1.1,1.1))
    with patch('autocnet.matcher.subpixel.clip_roi', side_effect=clip_side_effect):
        nx, ny, strength, _ = sp.subpixel_transformed_template(a.shape[1]/2, a.shape[0]/2,
                                                b.shape[1]/2, b.shape[0]/2,
                                                a, b, transform, upsampling=16)

    assert strength >= 0.84
    assert nx == pytest.approx(51.18894)
    assert ny == pytest.approx(54.36261)

@pytest.mark.parametrize("convergence_threshold, expected", [(2.0, (50.49, 52.08, (0.039507, -9.5e-20)))])
def test_iterative_phase(apollo_subsets, convergence_threshold, expected):
    a = apollo_subsets[0]
+13 −8
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ class Roi():
    bottom_y : int
               The bottom image coordinate in imge space
    """
    def __init__(self, geodataset, x, y, size_x=200, size_y=200):
        self.geodataset = geodataset
    def __init__(self, data, x, y, size_x=200, size_y=200):
        self.data = data

        self.x = x
        self.y = y
@@ -68,10 +68,10 @@ class Roi():
        """
        try:
            # Geodataset object
            raster_size = self.geodataset.raster_size
            raster_size = self.data.raster_size
        except: 
            # Numpy array in y,x form
            raster_size = self.geodataset.shape[::-1]
            raster_size = self.data.shape[::-1]

        # what is the extent that can actually be extracted?
        left_x = self._x - self.size_x
@@ -90,15 +90,20 @@ class Roi():

        return list(map(int, [left_x, right_x, top_y, bottom_y]))

    @property
    def center(self):
        ie = self.image_extent
        return (ie[1] - ie[0])/2, (ie[3]-ie[2])/2

    def clip(self, dtype=None):
        pixels = self.image_extent
        if isinstance(self.geodataset, np.ndarray):
            array = self.geodataset[pixels[2]:pixels[3]+1,
        if isinstance(self.data, np.ndarray):
            array = self.data[pixels[2]:pixels[3]+1,
                                         pixels[0]:pixels[1]+1]
        else:
            # Have to reformat to [xstart, ystart, xnumberpixels, ynumberpixels]
            pixels = [pixels[0], pixels[2], pixels[1]-pixels[0], pixels[3]-pixels[2]]
            array = self.geodataset.read_array(pixels=pixels, dtype=dtype)
            array = self.data.read_array(pixels=pixels, dtype=dtype)

        return array