Commit eae8d272 authored by Kelvinrr's avatar Kelvinrr
Browse files

updated notebooks

parent d9a2385d
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
%% Cell type:code id: tags:
%% Cell type:code id: tags:


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


from autocnet.examples import get_path
from autocnet.examples import get_path
from autocnet.graph.network import CandidateGraph
from autocnet.graph.network import CandidateGraph
from autocnet.graph.edge import Edge
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 ciratefi




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


from bisect import bisect_left
from bisect import bisect_left


from scipy.ndimage.interpolation import rotate
from scipy.ndimage.interpolation import rotate


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


%matplotlib inline
%matplotlib inline
%pylab inline
%pylab inline
```
```


%% Output
%% Output


    Populating the interactive namespace from numpy and matplotlib
    Populating the interactive namespace from numpy and matplotlib


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


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


# Create Basic Structures
# Create Basic Structures


%% Cell type:code id: tags:
%% Cell type:code id: tags:


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


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


#Match
#Match
cg.match_features()
cg.match_features()


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




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


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


cg.suppress(clean_keys=['symmetry', 'ratio', 'subpixel'])
cg.suppress(clean_keys=['symmetry', 'ratio', 'subpixel'])
```
```


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


# Do Stuff
# Do Stuff


%% Cell type:code id: tags:
%% Cell type:code id: tags:


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


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


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


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


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


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


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


    # Get the template and search windows
    # 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, 5)
    s_template = rotate(s_template, 0)
    s_template = rotate(s_template, 0)
    s_template = imresize(s_template, 1.)
    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, 11)
    d_search = rotate(d_search, 0)
    d_search = rotate(d_search, 0)
    d_search = imresize(d_search, 1.)
    d_search = imresize(d_search, 1.)


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


    result = ciratefi.ciratefi(s_template, d_search, upsampling=10., alpha=math.pi/4,
    result = ciratefi.ciratefi(s_template, d_search, upsampling=10., alpha=math.pi/4,
                     cifi_thresh=70, rafi_thresh=70, tefi_thresh=100,
                     cifi_thresh=70, rafi_thresh=70, tefi_thresh=100,
                     use_percentile=True, radii=list(range(1,3)), verbose=True)
                     use_percentile=True, radii=list(range(1,3)), verbose=True)
    print(result)
    print(result)
    break
    break
```
```


%% Output
%% Output










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


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
```
```
+1 −1
Original line number Original line Diff line number Diff line
%% Cell type:code id: tags:
%% Cell type:code id: tags:


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


from autocnet.examples import get_path
from autocnet.examples import get_path
from autocnet.graph.network import CandidateGraph
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 autocnet.matcher.suppression_funcs import distance


from IPython.display import display
from IPython.display import display


%pylab inline
%pylab inline
```
```


%% Output
%% Output


    Populating the interactive namespace from numpy and matplotlib
    Populating the interactive namespace from numpy and matplotlib


%% Cell type:code id: tags:
%% Cell type:code id: tags:


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


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


#Match
#Match
cg.match_features()
cg.match_features()


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


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


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


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


## Suppression
## Suppression
Create a suppression object using a default error tolerance and count.  Supply a custom function that suppresses based upon the distance between matches.
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:
%% Cell type:code id: tags:


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


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


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


### Suppression and Do/Undo
### 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 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 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.
The plotting calls remain the same for all of these example, only the first line of each is altered.


--------
--------


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


$k=10$ and $k_{error}$ defaults to 10%
$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.*
*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:
%% Cell type:code id: tags:


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


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


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


$k = 50$
$k = 50$


%% Cell type:code id: tags:
%% Cell type:code id: tags:


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


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


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


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


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
#cg.edge[0][1].suppress(clean_keys=['fundamental'], func=distance, k=100, k_error=0.25)
#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
# Plot, in blue the points that passed all outlier detectors so far
#cg.edge[0][1].plot(clean_keys=['fundamental'], line_kwargs={'linewidth':0})
#cg.edge[0][1].plot(clean_keys=['fundamental'], line_kwargs={'linewidth':0})
# Overlay, in red, the points that remain after suppression
# 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'})
#cg.edge[0][1].plot(clean_keys=['suppression'], line_kwargs={'linewidth':0}, scatter_kwargs={'color':'red'})
```
```


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


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


%% Cell type:code id: tags:
%% Cell type:code id: tags:


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


%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:


## Rollback
## 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).
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:
%% Cell type:code id: tags:


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


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


%% Cell type:code id: tags:
%% Cell type:code id: tags:


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