Commit eae8d272 authored by Kelvinrr's avatar Kelvinrr
Browse files

updated notebooks

parent d9a2385d
Loading
Loading
Loading
Loading
+1 −1
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.matcher import FlannMatcher
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
```

%% Output

    Populating the interactive namespace from numpy and matplotlib

    WARNING: pylab import has clobbered these variables: ['e']
    `%matplotlib` prevents importing * from pylab and numpy

%% 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'])
```

%% 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']

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 = rotate(s_template, 0)
    s_template = imresize(s_template, 1.)

    d_search = sp.clip_roi(e.destination.geodata, d_keypoint, 11)
    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,
                     cifi_thresh=70, rafi_thresh=70, tefi_thresh=100,
                     use_percentile=True, radii=list(range(1,3)), verbose=True)
    print(result)
    break
```

%% Output





    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-10-52559c337e48> in <module>()
         41     result = ciratefi.ciratefi(s_template, d_search, upsampling=10., alpha=math.pi/4,
         42                      cifi_thresh=70, rafi_thresh=70, tefi_thresh=100,
    ---> 43                      use_percentile=True, radii=list(range(1,3)), verbose=True)
         44     print(result)
         45     break
    /Users/kelvinrodriguez/Repos/autocnet/autocnet/matcher/ciratefi.py in ciratefi(template, search_image, upsampling, cifi_thresh, rafi_thresh, tefi_thresh, use_percentile, alpha, scales, radii, verbose)
        613     # Perform last filter tefi
        614     results = tefi(template, search_image, sg_candidate_points, best_scales, best_rotation,
    --> 615                    thresh=tefi_thresh, alpha=math.pi/4, use_percentile=True, upsampling=upsampling, verbose=verbose)
        616
        617     # return the points found
    /Users/kelvinrodriguez/Repos/autocnet/autocnet/matcher/ciratefi.py in tefi(template, search_image, candidate_pixels, best_scales, best_angles, scales, upsampling, thresh, alpha, use_percentile, verbose)
        460     best_angles = np.asarray(best_angles, dtype=np.float32)
        461     if not best_angles.size or not np.any(best_angles):
    --> 462         raise ValueError('Input best angle list is empty')
        463
        464     best_scales = np.asarray(best_scales, dtype=float)
    ValueError: Input best angle list is empty

%% Cell type:code id: tags:

``` python
```
+1 −1
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.matcher.matcher import FlannMatcher
from autocnet.matcher.feature import FlannMatcher
from autocnet.matcher.suppression_funcs import distance

from IPython.display import display

%pylab inline
```

%% Output

    Populating the interactive namespace from numpy and matplotlib

%% Cell type:code id: tags:

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

#Apply SIFT to extract features
cg.extract_features(method='sift')

#Match
cg.match_features()

#Apply outlier detection
cg.symmetry_checks()
cg.ratio_checks()

m = cg.edge[0][1].masks

#Compute a fundamental matrix
cg.compute_fundamental_matrices(clean_keys=['ratio', 'symmetry'])
```

%% Cell type:markdown id: tags:

## Suppression
Create a suppression object using a default error tolerance and count.  Supply a custom function that suppresses based upon the distance between matches.

%% Cell type:code id: tags:

``` python
#figsize(12,12)
#cg.edge[0][1].suppress(clean_keys=['fundamental'], func=distance)

# Plot, in blue the points that passed all outlier detectors so far
#cg.edge[0][1].plot(clean_keys=['fundamental'], line_kwargs={'linewidth':0})
# Overlay, in red, the points that remain after suppression
#cg.edge[0][1].plot(clean_keys=['suppression'], line_kwargs={'linewidth':0}, scatter_kwargs={'color':'red'})
```

%% Cell type:markdown id: tags:

### Suppression and Do/Undo
The suppression object, associated with each edge is a stateful observable.  This means that other objects can observe the suppression object.  If the suppression object changes, all of the observers are notified and can take whatever action they have registered.  In addition to being observable, the suppression object keeps a history of itself.  This supports do/undo functionality (that alerts observers).

The cell above created that object with a custom distance function.  The cells below alter $k$, the desired number of points, and $k_{error}$, the acceptable percentage of error in $k$.  These changes are then rolled back and forth.

The plotting calls remain the same for all of these example, only the first line of each is altered.

--------

%% Cell type:markdown id: tags:

$k=10$ and $k_{error}$ defaults to 10%

*Take note of the bad point, in the left image, that has made it through the ratio, symmetry, and fundamental matrix computation tests.*

%% Cell type:code id: tags:

``` python
#cg.edge[0][1].suppress(clean_keys=['fundamental'], func=distance, k=10)

# Plot, in blue the points that passed all outlier detectors so far
#cg.edge[0][1].plot(clean_keys=['fundamental'], line_kwargs={'linewidth':0})
# Overlay, in red, the points that remain after suppression
#cg.edge[0][1].plot(clean_keys=['suppression'], line_kwargs={'linewidth':0}, scatter_kwargs={'color':'red'})
```

%% Cell type:markdown id: tags:

$k = 50$

%% Cell type:code id: tags:

``` python
#cg.edge[0][1].suppress(clean_keys=['fundamental'], func=distance, k=50)

# Plot, in blue the points that passed all outlier detectors so far
#cg.edge[0][1].plot(clean_keys=['fundamental'], line_kwargs={'linewidth':0})
# Overlay, in red, the points that remain after suppression
#cg.edge[0][1].plot(clean_keys=['suppression'], line_kwargs={'linewidth':0}, scatter_kwargs={'color':'red'})
```

%% Cell type:markdown id: tags:

$k=100$ and $k_{error} = 25%$

%% Cell type:code id: tags:

``` python
#cg.edge[0][1].suppress(clean_keys=['fundamental'], func=distance, k=100, k_error=0.25)

# Plot, in blue the points that passed all outlier detectors so far
#cg.edge[0][1].plot(clean_keys=['fundamental'], line_kwargs={'linewidth':0})
# Overlay, in red, the points that remain after suppression
#cg.edge[0][1].plot(clean_keys=['suppression'], line_kwargs={'linewidth':0}, scatter_kwargs={'color':'red'})
```

%% Cell type:markdown id: tags:

Using the suppression object we can access some attributes to see how many valid points.

%% Cell type:code id: tags:

``` python
#cg.edge[0][1].suppression.nvalid
```

%% Cell type:markdown id: tags:

## Rollback
Now we will undo that last change.  Perhaps the error was just too high (it was not near 25% in this case, but imagine it was).

%% Cell type:code id: tags:

``` python
#cg.edge[0][1].suppression.rollback()

# Plot, in blue the points that passed all outlier detectors so far
#cg.edge[0][1].plot(clean_keys=['fundamental'], line_kwargs={'linewidth':0})
# Overlay, in red, the points that remain after suppression
#cg.edge[0][1].plot(clean_keys=['suppression'], line_kwargs={'linewidth':0}, scatter_kwargs={'color':'red'})
```

%% Cell type:code id: tags:

``` python
```
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@
    "from autocnet.examples import get_path\n",
    "from autocnet.graph.network import CandidateGraph\n",
    "from autocnet.graph.edge import Edge\n",
    "from autocnet.matcher.matcher import FlannMatcher\n",
    "from autocnet.matcher.feature import FlannMatcher\n",
    "\n",
    "from IPython.display import display\n",
    "\n",