Commit 127d9ffa authored by jlaura's avatar jlaura Committed by GitHub
Browse files

Merge pull request #147 from Kelvinrr/ciratefi_integration

Ciratefi integration
parents e4795b00 00758e5d
Loading
Loading
Loading
Loading
+16 −8
Changes for autocnet/matcher/ciratefi.py: 16 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -481,8 +481,8 @@ def tefi(template, search_image, candidate_pixels, best_scales, best_angles,

    # check for upsampling
    if upsampling > 1:
        template = zoom(template, upsampling, order=3)
        search_image = zoom(search_image, upsampling, order=3)
        u_template = zoom(template, upsampling, order=3)
        u_search_image = zoom(search_image, upsampling, order=3)

    alpha_list = np.arange(0, 2*math.pi, alpha)
    candidate_pixels *= int(upsampling)
@@ -505,13 +505,13 @@ def tefi(template, search_image, candidate_pixels, best_scales, best_angles,

        max_coeff = -math.inf
        for j in range(scalesxalphas.shape[0]):
            transformed_template = imresize(template, scalesxalphas[j][0])
            transformed_template = imresize(u_template, scalesxalphas[j][0])
            transformed_template = rotate(transformed_template, scalesxalphas[j][1])

            y_window, x_window = (math.floor(transformed_template.shape[0]/2),
                                  math.floor(transformed_template.shape[1]/2))

            cropped_search = search_image[y-y_window:y+y_window+1, x-x_window:x+x_window+1]
            cropped_search = u_search_image[y-y_window:y+y_window+1, x-x_window:x+x_window+1]

            if(y < y_window or x < x_window or cropped_search.shape < transformed_template.shape or
               cropped_search.shape != transformed_template.shape):
@@ -531,16 +531,24 @@ def tefi(template, search_image, candidate_pixels, best_scales, best_angles,
    if use_percentile:
        thresh = np.percentile(tefi_coeffs, int(thresh))

    candidate_pixels = candidate_pixels/upsampling
    result_points = candidate_pixels[np.where(tefi_coeffs >= thresh)]
    result_coeffs = tefi_coeffs[np.where(tefi_coeffs >= thresh)]

    results = candidate_pixels[np.where(tefi_coeffs >= thresh)]
    x = result_points[0][1]
    y = result_points[0][0]

    ideal_y = u_search_image.shape[0] / 2
    ideal_x = u_search_image.shape[1] / 2

    if verbose:  # pragma: no cover
        plt.imshow(image_pixels, interpolation='none')
        plt.scatter(y=results[:, 0], x=results[:, 1], c='w', s=80)
        plt.scatter(y=y/upsampling, x=x/upsampling, c='w', s=80)
        plt.show()

    return results
    x = (ideal_x - x)/upsampling
    y = (ideal_y - y)/upsampling

    return x, y, result_coeffs[0]


def ciratefi(template, search_image, upsampling=1, cifi_thresh=95, rafi_thresh=95, tefi_thresh=100,
+7 −2
Changes for autocnet/matcher/subpixel.py: 7 added lines, 2 removed lines.
Original line number Diff line number Diff line
import numpy as np

from autocnet.matcher import naive_template
from autocnet.matcher import ciratefi


# TODO: look into KeyPoint.size and perhaps use to determine an appropriately-sized search/template.

@@ -49,7 +51,7 @@ def clip_roi(img, center, img_size):
    return clipped_img


def subpixel_offset(template, search, **kwargs):
def subpixel_offset(template, search, method='naive', **kwargs):
    """
    Uses a pattern-matcher on subsets of two images determined from the passed-in keypoints and optional sizes to
    compute an x and y offset from the search keypoint to the template keypoint and an associated strength.
@@ -74,7 +76,10 @@ def subpixel_offset(template, search, **kwargs):
               Strength of the correspondence in the range [-1, 1]
    """

    x_offset, y_offset, strength = naive_template.pattern_match(template, search, **kwargs)
    functions = { 'naive' : naive_template.pattern_match,
                  'ciratefi' : ciratefi.ciratefi}

    x_offset, y_offset, strength = functions[method](template, search, **kwargs)
    return x_offset, y_offset, strength

'''
+5 −6
Changes for autocnet/matcher/tests/test_ciratefi.py: 5 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -142,24 +142,23 @@ class TestCiratefi(unittest.TestCase):
                print(warn)

            self.assertEqual(len(w), 0)
            self.assertIn((np.floor(self.search.shape[0]/2), np.floor(self.search.shape[1]/2)), pixel)
            self.assertTrue(pixel[0][0] == self.search_center[0] and pixel[0][1] == self.search_center[1])
            print(pixel)
            self.assertTrue(np.equal((.5, .5), (pixel[1], pixel[0])).all())

    def test_ciratefi(self):
        results = ciratefi.ciratefi(self.template, self.search, upsampling=10, cifi_thresh=self.cifi_thresh,
                                    rafi_thresh=self.rafi_thresh, tefi_thresh=self.tefi_thresh,
                                    use_percentile=self.use_percentile, alpha=self.alpha, radii=self.radii)

        self.assertEqual(len(results), 1)
        self.assertTrue(np.equal(results[0], self.search_center).all())
        self.assertEqual(len(results), 3)
        self.assertTrue((np.array(results[1], results[0]) < 1).all())

        results = ciratefi.ciratefi(self.offset_template, self.search, upsampling=self.upsampling,
                                    cifi_thresh=self.cifi_thresh, rafi_thresh=self.rafi_thresh,
                                    tefi_thresh=self.tefi_thresh,
                                    use_percentile=self.use_percentile, alpha=self.alpha, radii=self.radii)

        print(results)
        self.assertTrue(np.equal(results[0], np.add(self.search_center, list(self.offset))).all())


    def tearDown(self):
        pass
+124 −0
Changes for notebooks/.ipynb_checkpoints/Ciratefi-checkpoint.ipynb: 124 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import os
import sys
sys.path.insert(0, os.path.abspath('..'))

from autocnet.examples import get_path
from autocnet.graph.network import CandidateGraph
from autocnet.graph.edge import Edge
from autocnet.matcher.feature import FlannMatcher
from autocnet.matcher import ciratefi


from autocnet.matcher import subpixel as sp
from scipy.misc import imresize
import math
import warnings
import cv2

from bisect import bisect_left

from scipy.ndimage.interpolation import rotate

from IPython.display import display
warnings.filterwarnings('ignore')

%matplotlib inline
%pylab inline
```

%% Cell type:markdown id: tags:

# Create Basic Structures

%% Cell type:code id: tags:

``` python
#Point to the adjacency Graph
adjacency = get_path('three_image_adjacency.json')
basepath = get_path('Apollo15')
cg = CandidateGraph.from_adjacency(adjacency, basepath=basepath)

#Apply SIFT to extract features
cg.extract_features(method='sift', extractor_parameters={'nfeatures':300})

#Match
cg.match_features()

# Perform the symmetry check
cg.symmetry_checks()
# Perform the ratio check
cg.ratio_checks(clean_keys = ['symmetry'])
# Create fundamental matrix
cg.compute_fundamental_matrices(clean_keys = ['symmetry', 'ratio'])

# Step: Compute the homographies and apply RANSAC
cg.compute_homographies(clean_keys=['symmetry', 'ratio'])

# Step: Compute subpixel offsets for candidate points
cg.subpixel_register(clean_keys=['ransac'])

cg.edge[0][2].plot(clean_keys=['symmetry', 'ratio'])
```

%% Cell type:markdown id: tags:

# Do Stuff

%% Cell type:code id: tags:

``` python
from scipy.ndimage.interpolation import zoom
from scipy.stats.stats import pearsonr

figsize(10,10)
e = cg.edge[0][2]
matches = e.matches
clean_keys = ['ratio', 'symmetry']

full_offsets = np.zeros((len(matches), 3))

if clean_keys:
    matches, mask = e.clean(clean_keys)

# Preallocate the numpy array to avoid appending and type conversion
edge_offsets = np.empty((len(matches),3))

# for each edge, calculate this for each keypoint pair
for i, (idx, row) in enumerate(matches.iterrows()):
    s_idx = int(row['source_idx'])
    d_idx = int(row['destination_idx'])
    s_kps = e.source.get_keypoints().iloc[s_idx]
    d_kps = e.destination.get_keypoints().iloc[d_idx]

    s_keypoint = e.source.get_keypoints().iloc[s_idx][['x', 'y']].values
    d_keypoint = e.destination.get_keypoints().iloc[d_idx][['x', 'y']].values

    # Get the template and search windows
    s_template = sp.clip_roi(e.source.geodata, s_keypoint, 9)
    s_template = rotate(s_template, 0)
    s_template = imresize(s_template, 1.)

    d_search = sp.clip_roi(e.destination.geodata, d_keypoint, 21)
    d_search = rotate(d_search, 0)
    d_search = imresize(d_search, 1.)

    print(sp.subpixel_offset(s_template, d_search, method='ciratefi', upsampling=16, alpha=math.pi/4,
                     cifi_thresh=70, rafi_thresh=70, tefi_thresh=100,
                     use_percentile=True, radii=list(range(1,5))))

    break
```

%% Cell type:code id: tags:

``` python

```

%% Cell type:code id: tags:

``` python
```
+13 −13
Changes for notebooks/Ciratefi.ipynb: 13 added lines, 13 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import os
import sys
sys.path.insert(0, os.path.abspath('..'))

from autocnet.examples import get_path
from autocnet.graph.network import CandidateGraph
from autocnet.graph.edge import Edge
from autocnet.matcher.feature import FlannMatcher
from autocnet.matcher import ciratefi


from autocnet.matcher import subpixel as sp
from scipy.misc import imresize
import math
import warnings
import cv2

from bisect import bisect_left

from scipy.ndimage.interpolation import rotate

from IPython.display import display
warnings.filterwarnings('ignore')

%matplotlib inline
%pylab inline
```

%% Cell type:markdown id: tags:

# Create Basic Structures

%% Cell type:code id: tags:

``` python
#Point to the adjacency Graph
adjacency = get_path('three_image_adjacency.json')
basepath = get_path('Apollo15')
cg = CandidateGraph.from_adjacency(adjacency, basepath=basepath)

#Apply SIFT to extract features
cg.extract_features(method='sift', extractor_parameters={'nfeatures':300})

#Match
cg.match_features()

# Perform the symmetry check
cg.symmetry_checks()
# Perform the ratio check
cg.ratio_checks(clean_keys = ['symmetry'])
# Create fundamental matrix
cg.compute_fundamental_matrices(clean_keys = ['symmetry', 'ratio'])


# Step: Compute the homographies and apply RANSAC
cg.compute_homographies(clean_keys=['symmetry', 'ratio'])

# Step: Compute subpixel offsets for candidate points
cg.subpixel_register(clean_keys=['ransac'])

cg.suppress(clean_keys=['symmetry', 'ratio', 'subpixel'])
cg.edge[0][2].plot(clean_keys=['symmetry', 'ratio'])
```

%% Cell type:markdown id: tags:

# Do Stuff

%% Cell type:code id: tags:

``` python
from scipy.ndimage.interpolation import zoom
from scipy.stats.stats import pearsonr

figsize(10,10)
e = cg.edge[0][2]
matches = e.matches
clean_keys = ['subpixel']
clean_keys = ['ratio', 'symmetry']

full_offsets = np.zeros((len(matches), 3))

if clean_keys:
    matches, mask = e.clean(clean_keys)

# Preallocate the numpy array to avoid appending and type conversion
edge_offsets = np.empty((len(matches),3))

# for each edge, calculate this for each keypoint pair
for i, (idx, row) in enumerate(matches.iterrows()):
    s_idx = int(row['source_idx'])
    d_idx = int(row['destination_idx'])
    s_kps = e.source.get_keypoints().iloc[s_idx]
    d_kps = e.destination.get_keypoints().iloc[d_idx]

    s_keypoint = e.source.get_keypoints().iloc[s_idx][['x', 'y']].values
    d_keypoint = e.destination.get_keypoints().iloc[d_idx][['x', 'y']].values

    # Get the template and search windows
    s_template = sp.clip_roi(e.source.geodata, s_keypoint, 5)
    s_template = sp.clip_roi(e.source.geodata, s_keypoint, 9)
    s_template = rotate(s_template, 0)
    s_template = imresize(s_template, 1.)

    d_search = sp.clip_roi(e.destination.geodata, d_keypoint, 11)
    d_search = sp.clip_roi(e.destination.geodata, d_keypoint, 21)
    d_search = rotate(d_search, 0)
    d_search = imresize(d_search, 1.)

    imshow(s_template, cmap='Greys')
    show()
    imshow(d_search, cmap='Greys')
    show()

    result = ciratefi.ciratefi(s_template, d_search, upsampling=10., alpha=math.pi/4,
    print(sp.subpixel_offset(s_template, d_search, method='ciratefi', upsampling=16, alpha=math.pi/4,
                     cifi_thresh=70, rafi_thresh=70, tefi_thresh=100,
                     use_percentile=True, radii=list(range(1,3)), verbose=True)
    print(result)
                     use_percentile=True, radii=list(range(1,5))))

    break
```

%% Cell type:code id: tags:

``` python

```

%% Cell type:code id: tags:

``` python
```