From 142ad4e7741ab960a06e3ed57ceae57de13194a7 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Wed, 2 Oct 2013 20:15:22 +0530 Subject: [PATCH] Adding tests for orb; fixing some bugs --- skimage/feature/orb.py | 30 +++++----- skimage/feature/orb_cy.pyx | 3 +- skimage/feature/tests/test_orb.py | 93 +++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 skimage/feature/tests/test_orb.py diff --git a/skimage/feature/orb.py b/skimage/feature/orb.py index 63586ff6..8f659394 100644 --- a/skimage/feature/orb.py +++ b/skimage/feature/orb.py @@ -18,7 +18,7 @@ for i in range(-15, 16): def keypoints_orb(image, n_keypoints=500, fast_n=9, fast_threshold=0.08, - harris_k=0.04, downscale=1.2, n_scales=8): + harris_k=0.04, downscale=1.2, n_scales=8): """Detect Oriented Fast keypoints. @@ -201,22 +201,24 @@ def descriptor_orb(image, keypoints, orientations, scales, curr_image = np.ascontiguousarray(pyramid[scale]) curr_scale_mask = scales == scale - curr_scale_kpts = keypoints[curr_scale_mask] / (downscale ** scale) - curr_scale_kpts = np.round(curr_scale_kpts).astype(np.intp) - curr_scale_orientation = orientations[curr_scale_mask] + if np.sum(curr_scale_mask) > 0: + curr_scale_kpts = keypoints[curr_scale_mask] / (downscale ** scale) + curr_scale_kpts = np.round(curr_scale_kpts).astype(np.intp) + curr_scale_orientation = orientations[curr_scale_mask] - border_mask = _mask_border_keypoints(curr_image, curr_scale_kpts, - dist=16) - curr_scale_kpts = curr_scale_kpts[border_mask] - curr_scale_orientation = curr_scale_orientation[border_mask] + border_mask = _mask_border_keypoints(curr_image, curr_scale_kpts, + dist=16) - curr_scale_kpts = np.ascontiguousarray(curr_scale_kpts) - curr_scale_orientation = np.ascontiguousarray(curr_scale_orientation) - curr_scale_descriptors = _orb_loop(curr_image, curr_scale_kpts, - curr_scale_orientation) + curr_scale_kpts = curr_scale_kpts[border_mask] + curr_scale_orientation = curr_scale_orientation[border_mask] - descriptors_list.append(curr_scale_descriptors) - filtered_keypoints_list.append(curr_scale_kpts * downscale ** scale) + curr_scale_kpts = np.ascontiguousarray(curr_scale_kpts) + curr_scale_orientation = np.ascontiguousarray(curr_scale_orientation) + curr_scale_descriptors = _orb_loop(curr_image, curr_scale_kpts, + curr_scale_orientation) + + descriptors_list.append(curr_scale_descriptors) + filtered_keypoints_list.append(curr_scale_kpts * downscale ** scale) descriptors = np.vstack(descriptors_list).view(np.bool) filtered_keypoints = np.vstack(filtered_keypoints_list) diff --git a/skimage/feature/orb_cy.pyx b/skimage/feature/orb_cy.pyx index 21126fbd..b1af3af2 100644 --- a/skimage/feature/orb_cy.pyx +++ b/skimage/feature/orb_cy.pyx @@ -9,7 +9,8 @@ import numpy as np from libc.math cimport sin, cos, round -pos = np.loadtxt("orb_descriptor_positions.txt", dtype=np.int8) +pos = np.loadtxt(os.path.join(os.path.dirname(__file__), + "orb_descriptor_positions.txt"), dtype=np.int8) pos0 = np.ascontiguousarray(pos[:, :2]) pos1 = np.ascontiguousarray(pos[:, 2:]) diff --git a/skimage/feature/tests/test_orb.py b/skimage/feature/tests/test_orb.py new file mode 100644 index 00000000..a577cf22 --- /dev/null +++ b/skimage/feature/tests/test_orb.py @@ -0,0 +1,93 @@ +import numpy as np +from numpy.testing import assert_array_equal, assert_almost_equal +from skimage.feature import keypoints_orb, descriptor_orb +from skimage.data import lena +from skimage.color import rgb2gray + + +def test_keypoints_orb_desired_no_of_keypoints(): + img = rgb2gray(lena()) + keypoints, orientations, scales = keypoints_orb(img, n_keypoints=10, + fast_n=12, + fast_threshold=0.20) + exp_keypoints = np.array([[435, 180], + [436, 180], + [376, 156], + [455, 176], + [435, 180], + [269, 111], + [376, 156], + [311, 173], + [413, 70], + [311, 173]]) + exp_scales = np.array([0, 1, 0, 0, 2, 0, 1, 1, 0, 3]) + exp_orientations = np.array([-175.64733392, -167.94842949, -148.98350192, + -142.03599837, -176.08535837, -53.08162354, + -150.89208271, 97.7693776 , -173.4479964 , + 38.66312042]) + assert_array_equal(exp_keypoints, keypoints) + assert_array_equal(exp_scales, scales) + assert_almost_equal(exp_orientations, np.rad2deg(orientations)) + + +def test_keypoints_orb_less_than_desired_no_of_keypoints(): + img = rgb2gray(lena()) + keypoints, orientations, scales = keypoints_orb(img, n_keypoints=15, + fast_n=12, + fast_threshold=0.33, + downscale=2, n_scales=2) + exp_keypoints = np.array([[ 67, 157], + [247, 146], + [269, 111], + [413, 70], + [435, 180], + [230, 136], + [264, 336], + [330, 148], + [372, 156]]) + exp_scales = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1]) + exp_orientations = np.array([-105.76503839, -96.28973044, -53.08162354, + -173.4479964 , -175.64733392, -106.07927215, + -163.40016243, 75.80865813, -154.73195911]) + assert_array_equal(exp_keypoints, keypoints) + assert_array_equal(exp_scales, scales) + assert_almost_equal(exp_orientations, np.rad2deg(orientations)) + + +def test_descriptor_orb(): + img = rgb2gray(lena()) + keypoints, orientations, scales = keypoints_orb(img, n_keypoints=10, + fast_n=12, + fast_threshold=0.20) + descriptors, filtered_keypoints = descriptor_orb(img, keypoints, orientations, scales) + + exp_filtered_keypoints = np.array([[435, 180], + [376, 156], + [455, 176], + [269, 111], + [413, 70], + [436, 180], + [376, 156], + [311, 173], + [435, 180], + [311, 173]]) + + descriptors_120_129 = np.array([[ True, False, False, True, False, False, False, False, False, False], + [ True, True, False, False, True, False, False, True, False, True], + [False, True, True, False, True, False, True, True, True, True], + [False, False, False, True, True, False, True, False, True, False], + [False, True, True, True, True, False, True, True, True, False], + [ True, False, True, True, True, False, False, False, True, False], + [ True, False, True, False, True, False, True, True, False, True], + [ True, True, True, True, True, True, False, True, True, True], + [ True, True, True, False, True, False, True, True, True, False], + [ True, True, False, True, True, True, False, True, False, True]], + dtype=bool) + + assert_array_equal(exp_filtered_keypoints, filtered_keypoints) + assert_array_equal(descriptors_120_129, descriptors[:, 120:130]) + + +if __name__ == '__main__': + from numpy import testing + testing.run_module_suite()