From 38bdd3e52377fc38a2e879c030a984397dba954a Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Fri, 1 Nov 2013 01:14:55 +0530 Subject: [PATCH] Made recarray changes in docstrings and tests --- skimage/feature/orb.py | 96 ++++++++++++++----------------- skimage/feature/tests/test_orb.py | 91 ++++++++++++++--------------- 2 files changed, 85 insertions(+), 102 deletions(-) diff --git a/skimage/feature/orb.py b/skimage/feature/orb.py index 9b394dd9..55058a8e 100644 --- a/skimage/feature/orb.py +++ b/skimage/feature/orb.py @@ -57,12 +57,8 @@ def keypoints_orb(image, n_keypoints=500, fast_n=9, fast_threshold=0.08, Returns ------- - keypoints : (N, 2) ndarray - The oFAST keypoints. - orientations : (N,) ndarray - The orientations of the N extracted keypoints. - scales : (N,) ndarray - The scales of the N extracted keypoints. + keypoints : record array + Record array with fields row, col, octave, orientation, response. References ---------- @@ -75,25 +71,20 @@ def keypoints_orb(image, n_keypoints=500, fast_n=9, fast_threshold=0.08, >>> from skimage.feature import keypoints_orb, descriptor_orb >>> square = np.zeros((50, 50)) >>> square[20:30, 20:30] = 1 - >>> keypoints, orientations, scales = keypoints_orb(square, n_keypoints=8, n_scales=2) + >>> keypoints = keypoints_orb(square, n_keypoints=8, n_scales=2) >>> keypoints.shape - (8, 2) - >>> keypoints - array([[29, 29], - [29, 20], - [20, 29], - [20, 20], - [15, 15], - [15, 20], - [20, 15], - [20, 20]]) - >>> orientations - array([-2.35619449, -0.78539816, 2.35619449, 0.78539816, 0.78539816, - 2.35619449, -0.78539816, -2.35619449]) - >>> np.rad2deg(orientations) - array([-135., -45., 135., 45., 45., 135., -45., -135.]) - >>> scales - array([0, 0, 0, 0, 1, 1, 1, 1]) + (8,) + >>> keypoints.row + array([ 29. , 29. , 20. , 20. , 20.4, 20.4, 28.8, 28.8]) + >>> keypoints.col + array([ 29. , 20. , 29. , 20. , 28.8, 20.4, 28.8, 20.4]) + >>> keypoints.octave + array([ 1. , 1. , 1. , 1. , 1.2, 1.2, 1.2, 1.2]) + >>> np.rad2deg(keypoints.orientation) + array([-135., -45., 135., 45., 135., 45., -135., -45.]) + >>> keypoints.response + array([ 21.4776577 , 21.4776577 , 21.4776577 , 21.4776577 , + 14.03845308, 14.03845308, 14.03845308, 14.03845308]) """ @@ -121,30 +112,31 @@ def keypoints_orb(image, n_keypoints=500, fast_n=9, fast_threshold=0.08, harris_response_list.append(harris_response[corners[:, 0], corners[:, 1]]) - keypoints = np.vstack(keypoints_list) + keypoints_array = np.vstack(keypoints_list) orientations = np.hstack(orientations_list) octaves = downscale ** np.hstack(scales_list) harris_measure = np.hstack(harris_response_list) - kpts_recarray = _create_keypoint_recarray(keypoints[:, 0], keypoints[:, 1], - octaves, orientations, - harris_measure) + keypoints = _create_keypoint_recarray(keypoints_array[:, 0], + keypoints_array[:, 1], + octaves, orientations, + harris_measure) - if kpts_recarray.shape[0] < n_keypoints: - return kpts_recarray + if keypoints.shape[0] < n_keypoints: + return keypoints else: best_indices = harris_measure.argsort()[::-1][:n_keypoints] - return kpts_recarray[best_indices] + return keypoints[best_indices] -def descriptor_orb(image, kpts_recarray, downscale=1.2, n_scales=8): +def descriptor_orb(image, keypoints, downscale=1.2, n_scales=8): """Compute rBRIEF descriptors of input keypoints. Parameters ---------- image : 2D ndarray Input grayscale image. - kpts_recarray : (N, 2) ndarray - Array of N input keypoint locations in the format (row, col). + keypoints : record array + Record array with fields row, col, octave, orientation, response. downscale : float Downscale factor for the image pyramid. Should be the same as that used in ``keypoints_orb``. @@ -159,8 +151,9 @@ def descriptor_orb(image, kpts_recarray, downscale=1.2, n_scales=8): filtering out those near the image border. Size of each descriptor is 32 bytes or 256 bits. filtered_keypoints : (P, 2) ndarray - Location i.e. (row, col) of P keypoints after removing out those that - are near border. + Record array with fields row, col, octave, orientation, response for + P keypoints obtained after removing out those that are near the + border. References ---------- @@ -174,15 +167,12 @@ def descriptor_orb(image, kpts_recarray, downscale=1.2, n_scales=8): >>> from skimage.feature import keypoints_orb, descriptor_orb >>> square = np.zeros((50, 50)) >>> square[20:30, 20:30] = 1 - >>> keypoints, orientations, scales = keypoints_orb(square, n_keypoints=8, - ... n_scales=2) + >>> keypoints = keypoints_orb(square, n_keypoints=8, n_scales=2) >>> keypoints.shape - (8, 2) - >>> descriptors, filtered_keypoints = descriptor_orb(square, keypoints, - ... orientations, scales, - ... n_scales=2) + (8,) + >>> descriptors, filtered_keypoints = descriptor_orb(square, keypoints, n_scales=2) >>> filtered_keypoints.shape - (8, 2) + (8,) >>> descriptors.shape (8, 256) @@ -192,31 +182,31 @@ def descriptor_orb(image, kpts_recarray, downscale=1.2, n_scales=8): pyramid = list(pyramid_gaussian(image, n_scales - 1, downscale)) descriptors_list = [] - kpts_recarray_list = [] + keypoints_list = [] for scale in range(n_scales): curr_image = np.ascontiguousarray(pyramid[scale]) - curr_scale_mask = (np.log(kpts_recarray.octave) / + curr_scale_mask = (np.log(keypoints.octave) / np.log(downscale)).astype(np.intp) == scale if np.sum(curr_scale_mask) > 0: - curr_kpts_recarray = kpts_recarray[curr_scale_mask] - curr_scale_kpts = np.squeeze(np.dstack((curr_kpts_recarray.row / curr_kpts_recarray.octave, - curr_kpts_recarray.col / curr_kpts_recarray.octave))) + curr_keypoints = keypoints[curr_scale_mask] + curr_scale_kpts = np.squeeze(np.dstack((curr_keypoints.row / curr_keypoints.octave, + curr_keypoints.col / curr_keypoints.octave))) border_mask = _mask_border_keypoints(curr_image, curr_scale_kpts, dist=16) - curr_kpts_recarray = curr_kpts_recarray[border_mask] + curr_keypoints = curr_keypoints[border_mask] curr_scale_kpts = np.ascontiguousarray(curr_scale_kpts[border_mask].astype(np.intp)) - curr_scale_orientation = np.ascontiguousarray(curr_kpts_recarray.orientation) + curr_scale_orientation = np.ascontiguousarray(curr_keypoints.orientation) curr_scale_descriptors = _orb_loop(curr_image, curr_scale_kpts, curr_scale_orientation) descriptors_list.append(curr_scale_descriptors) - kpts_recarray_list.append(curr_kpts_recarray) + keypoints_list.append(curr_keypoints) descriptors = np.vstack(descriptors_list).view(np.bool) - filtered_kpts_recarray = np.hstack(kpts_recarray_list) - return descriptors, filtered_kpts_recarray + filtered_keypoints = np.hstack(keypoints_list) + return descriptors, filtered_keypoints.view(np.recarray) diff --git a/skimage/feature/tests/test_orb.py b/skimage/feature/tests/test_orb.py index a577cf22..1333b6b1 100644 --- a/skimage/feature/tests/test_orb.py +++ b/skimage/feature/tests/test_orb.py @@ -7,73 +7,66 @@ 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]) + keypoints = keypoints_orb(img, n_keypoints=10, fast_n=12, + fast_threshold=0.20) + exp_row = np.array([ 435. , 435.6 , 376. , 455. , 434.88, 269. , + 375.6 , 310.8 , 413. , 311.04]) + exp_col = np.array([ 180. , 180. , 156. , 176. , 180. , 111. , + 156. , 172.8, 70. , 172.8]) + + exp_octaves = np.array([ 1. , 1.2 , 1. , 1. , 1.44 , 1. , + 1.2 , 1.2 , 1. , 1.728]) + 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)) + exp_response = np.array([ 0.96770745, 0.81027306, 0.72376257, + 0.5626413 , 0.5097993 , 0.44351774, + 0.39154173, 0.39084861, 0.39063076, + 0.37602487]) + assert_almost_equal(exp_row, keypoints.row) + assert_almost_equal(exp_col, keypoints.col) + assert_almost_equal(exp_octaves, keypoints.octave) + assert_almost_equal(exp_response, keypoints.response) + assert_almost_equal(exp_orientations, np.rad2deg(keypoints.orientation)) def test_keypoints_orb_less_than_desired_no_of_keypoints(): img = rgb2gray(lena()) - keypoints, orientations, scales = keypoints_orb(img, n_keypoints=15, + keypoints = 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_row = np.array([ 67., 247., 269., 413., 435., 230., 264., + 330., 372.]) + exp_col = np.array([ 157., 146., 111., 70., 180., 136., 336., + 148., 156.]) + + exp_octaves = np.array([ 1., 1., 1., 1., 1., 2., 2., 2., 2.]) + 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)) + exp_response = np.array([ 0.13197835, 0.24931321, 0.44351774, + 0.39063076, 0.96770745, 0.04935129, + 0.21431068, 0.15826555, 0.42403573]) + + assert_almost_equal(exp_row, keypoints.row) + assert_almost_equal(exp_col, keypoints.col) + assert_almost_equal(exp_octaves, keypoints.octave) + assert_almost_equal(exp_response, keypoints.response) + assert_almost_equal(exp_orientations, np.rad2deg(keypoints.orientation)) 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]]) + keypoints = keypoints_orb(img, n_keypoints=10, fast_n=12, + fast_threshold=0.20) + descriptors, filtered_keypoints = descriptor_orb(img, keypoints) 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], + [ 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], @@ -81,10 +74,10 @@ def test_descriptor_orb(): [ 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]], + [ True, False, False, False, False, False, True, True, True, False]], dtype=bool) - assert_array_equal(exp_filtered_keypoints, filtered_keypoints) + assert_array_equal(descriptors_120_129, descriptors[:, 120:130])