Commit 468d5bdf authored by Jay's avatar Jay Committed by Jason R Laura
Browse files

Coverage improvements, MLE set to NotImplementedError (better in a new PR).

parent 71d2d3f8
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
import os
import sys
import unittest

import numpy as np

sys.path.append(os.path.abspath('..'))

from .. import utils


class TestUtils(unittest.TestCase):

    def test_normalize(self):
        r = np.random.RandomState(12345)
        coords = r.uniform(0,100,size=(100,2))

        normalizer = utils.normalize(coords)

        truth = np.array([[0.036613,  0, -1.995647],
                          [0, 0.036613, -1.862705],
                          [0, 0,  1]])

        np.testing.assert_array_almost_equal(normalizer, truth)
+34 −0
Original line number Diff line number Diff line
import math
import numpy as np


def normalize(a):
    """
    Normalize a set of coordinates such that the origin is
    translated to the center and then scaled isotropically such
    that the average distance from the origin is $\sqrt{2}$.

    Parameters
    ----------
    a : arraylike
        (n,2) array of x,y or (n,3) homogeneous coordinates

    Returns
    -------
    normalizer : ndarray
                 (3,3) transformation matrix
    """

    a = np.asarray(a)

    # Compute the normalization matrix
    centroid = a[:, :2].mean(axis=0)
    dist = np.sqrt(np.sum(((a[:, :2] - centroid)**2), axis=1))
    mean_dist = np.mean(dist)
    sq2 = math.sqrt(2)

    normalizer = np.array([[sq2 / mean_dist, 0, -sq2 / mean_dist * centroid[0]],
                          [0, sq2 / mean_dist,  -sq2 / mean_dist * centroid[1]],
                          [0, 0, 1]])

    return normalizer
 No newline at end of file
+2 −7
Original line number Diff line number Diff line
@@ -179,8 +179,6 @@ class Node(dict, MutableMapping):
                return self._keypoints.loc[index]
            else:
                return self._keypoints
        else:
            return None

    def get_keypoint_coordinates(self, index=None, homogeneous=False):
        """
@@ -425,11 +423,8 @@ class Node(dict, MutableMapping):
        if not hasattr(self, '_keypoints'):
            raise AttributeError('Keypoints must be extracted already, they have not been.')

        if clean_keys:
            mask = np.prod([self._mask_arrays[i] for i in clean_keys], axis=0, dtype=np.bool)
            keypoints = self._keypoints[mask]

        keypoints = self._keypoints[['x', 'y']].values
        matches, mask = self._clean(clean_keys)
        keypoints = self._keypoints[mask][['x', 'y']].values

        ratio = convex_hull_ratio(keypoints, ideal_area)
        return ratio
+12 −33
Original line number Diff line number Diff line
@@ -148,15 +148,15 @@ class TransformationMatrix(np.ndarray):

    @abc.abstractmethod
    def compute_error(self, x1, x2, index=None):
        pass
        raise NotImplementedError

    @abc.abstractmethod
    def recompute_matrix(self):
        pass
        raise NotImplementedError

    @abc.abstractmethod
    def refine(self):
        pass
        raise NotImplementedError


class FundamentalMatrix(TransformationMatrix):
@@ -196,7 +196,14 @@ class FundamentalMatrix(TransformationMatrix):
        ----------
        .. [Hartley2003]
        """

        raise NotImplementedError
        """
        '''
        This still requires additional work.
         - The optimization is exceptionally slow.
         - Iteration is required to add newly discovered correspondences, re-estimate F using MLE,
            and continuing until the number of correspondences stabilizes.
        '''
        p = camera.idealized_camera()
        p1 = camera.estimated_camera_from_f(self)

@@ -224,6 +231,7 @@ class FundamentalMatrix(TransformationMatrix):
        t = pgs[:, 3]
        M = pgs[:, 0:3]
        self[:] = crossform(t).dot(M)
        """

    def refine(self, method=ps.esda.mapclassify.Fisher_Jenks, values=None, bin_id=0, **kwargs):
        """
@@ -341,35 +349,6 @@ class FundamentalMatrix(TransformationMatrix):

        return F_error

    def _normalize(self, a):
        """
        Normalize a set of coordinates such that the origin is
        translated to the center and then scaled isotropically such
        that the average distance from the origin is $\sqrt{2}$.

        Parameters
        ----------
        a : DataFrame
            (n,3) of homogeneous coordinates

        Returns
        -------
        normalized : ndarray
                     (3,3) tranformation matrix
        """

        # Compute the normalization matrix
        centroid = a[['x', 'y']].mean()
        dist = np.sqrt(np.sum(((a[['x', 'y']] - centroid)**2).values, axis=1))
        mean_dist = np.mean(dist)
        sq2 = math.sqrt(2)

        normalizer = np.array([[sq2 / mean_dist, 0, -sq2 / mean_dist * centroid[0]],
                       [0, sq2 / mean_dist,  -sq2 / mean_dist * centroid[1]],
                       [0, 0, 1]])

        return normalizer

    def compute(self, kp1, kp2, method='ransac', reproj_threshold=2.0, confidence=0.99):
        """
        Given two arrays of keypoints compute the fundamental matrix
+2 −2
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ from autocnet.examples import get_path
from autocnet.fileio.io_controlnetwork import to_isis
from autocnet.fileio.io_controlnetwork import write_filelist
from autocnet.graph.network import CandidateGraph
from autocnet.matcher.matcher import FlannMatcher


class TestThreeImageMatching(unittest.TestCase):
@@ -54,6 +53,7 @@ class TestThreeImageMatching(unittest.TestCase):
            edge.ratio_check(clean_keys=['symmetry'], ratio=0.99)

        cg.apply_func_to_edges("compute_homography", clean_keys=['symmetry', 'ratio'])
        cg.compute_fundamental_matrices(clean_keys=['symmetry', 'ratio'])

        # Step: And create a C object
        cg.generate_cnet(clean_keys=['symmetry', 'ratio', 'ransac'])
@@ -63,7 +63,7 @@ class TestThreeImageMatching(unittest.TestCase):
        write_filelist(filelist, 'TestThreeImageMatching_fromlist.lis')

        # Step: Create a correspondence network
        cg.generate_cnet(clean_keys=['symmetry', 'ratio', 'ransac'])
        cg.generate_cnet(clean_keys=['symmetry', 'ratio', 'ransac'], deepen=True)

        to_isis('TestThreeImageMatching.net', cg, mode='wb',
                networkid='TestThreeImageMatching', targetname='Moon')