Implement assert_nD in filter and feature packages

This commit is contained in:
Steven Silvester
2014-09-20 17:04:11 -05:00
parent d26e62f38d
commit 6a8c5e460f
11 changed files with 32 additions and 21 deletions
+2 -3
View File
@@ -17,6 +17,7 @@ import scipy.ndimage as ndi
from scipy.ndimage import (gaussian_filter,
generate_binary_structure, binary_erosion, label)
from skimage import dtype_limits
from skimage._shared.utils import assert_nD
def smooth_with_function_and_mask(image, function, mask):
@@ -148,9 +149,7 @@ def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None):
# mask by one and then mask the output. We also mask out the border points
# because who knows what lies beyond the edge of the image?
#
if image.ndim != 2:
raise TypeError("The input 'image' must be a two-dimensional array.")
assert_nD(image)
if low_threshold is None:
low_threshold = 0.1 * dtype_limits(image)[1]
+2 -3
View File
@@ -3,6 +3,7 @@ from scipy import sqrt, pi, arctan2, cos, sin, exp
from scipy.ndimage import gaussian_filter
import skimage.color
from skimage import img_as_float, draw
from skimage._shared.utils import assert_nD
def daisy(img, step=4, radius=15, rings=3, histograms=8, orientations=8,
@@ -93,9 +94,7 @@ def daisy(img, step=4, radius=15, rings=3, histograms=8, orientations=8,
.. [2] http://cvlab.epfl.ch/alumni/tola/daisy.html
'''
# Validate image format.
if img.ndim != 2:
raise ValueError('Only grey-level images are supported.')
assert_nD(img, 'img')
img = img_as_float(img)
+3 -3
View File
@@ -1,6 +1,7 @@
import numpy as np
from scipy import sqrt, pi, arctan2, cos, sin
from scipy.ndimage import uniform_filter
from skimage._shared.utils import assert_nD
def hog(image, orientations=9, pixels_per_cell=(8, 8),
@@ -59,8 +60,7 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8),
shadowing and illumination variations.
"""
if image.ndim > 2:
raise ValueError("Currently only supports grey-level images")
assert_nD(image)
if normalise:
image = sqrt(image)
@@ -79,7 +79,7 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8),
# convert uint image to float
# to avoid problems with subtracting unsigned numbers in np.diff()
image = image.astype('float')
gx = np.empty(image.shape, dtype=np.double)
gx[:, 0] = 0
gx[:, -1] = 0
+4 -7
View File
@@ -9,6 +9,7 @@ from skimage.util import img_as_float
from .peak import peak_local_max
from ._hessian_det_appx import _hessian_matrix_det
from skimage.transform import integral_image
from skimage._shared.utils import assert_nD
# This basic blob detection algorithm is based on:
@@ -169,9 +170,7 @@ def blob_dog(image, min_sigma=1, max_sigma=50, sigma_ratio=1.6, threshold=2.0,
-----
The radius of each blob is approximately :math:`\sqrt{2}sigma`.
"""
if image.ndim != 2:
raise ValueError("'image' must be a grayscale ")
assert_nD(image)
image = img_as_float(image)
@@ -275,8 +274,7 @@ def blob_log(image, min_sigma=1, max_sigma=50, num_sigma=10, threshold=.2,
The radius of each blob is approximately :math:`\sqrt{2}sigma`.
"""
if image.ndim != 2:
raise ValueError("'image' must be a grayscale ")
assert_nD(image)
image = img_as_float(image)
@@ -385,8 +383,7 @@ def blob_doh(image, min_sigma=1, max_sigma=30, num_sigma=10, threshold=0.01,
due to the box filters used in the approximation of Hessian Determinant.
"""
if image.ndim != 2:
raise ValueError("'image' must be grayscale ")
assert_nD(image)
image = img_as_float(image)
image = integral_image(image)
+2
View File
@@ -5,6 +5,7 @@ from .util import (DescriptorExtractor, _mask_border_keypoints,
_prepare_grayscale_input_2D)
from .brief_cy import _brief_loop
from skimage._shared.utils import assert_nD
class BRIEF(DescriptorExtractor):
@@ -137,6 +138,7 @@ class BRIEF(DescriptorExtractor):
Keypoint coordinates as ``(row, col)``.
"""
assert_nD(image)
np.random.seed(self.sample_seed)
+3 -1
View File
@@ -9,7 +9,7 @@ from skimage.morphology import octagon, star
from skimage.feature.util import _mask_border_keypoints
from skimage.feature.censure_cy import _censure_dob_loop
from skimage._shared.utils import assert_nD
# The paper(Reference [1]) mentions the sizes of the Octagon shaped filter
# kernel for the first seven scales only. The sizes of the later scales
@@ -231,6 +231,8 @@ class CENSURE(FeatureDetector):
# (4) Finally, we remove the border keypoints and return the keypoints
# along with its corresponding scale.
assert_nD(image)
num_scales = self.max_scale - self.min_scale
image = np.ascontiguousarray(_prepare_grayscale_input_2D(image))
+4
View File
@@ -7,6 +7,7 @@ from skimage.feature.util import (FeatureDetector, DescriptorExtractor,
from skimage.feature import (corner_fast, corner_orientations, corner_peaks,
corner_harris)
from skimage.transform import pyramid_gaussian
from skimage._shared.utils import assert_nD
from .orb_cy import _orb_loop
@@ -166,6 +167,7 @@ class ORB(FeatureDetector, DescriptorExtractor):
Input image.
"""
assert_nD(image)
pyramid = self._build_pyramid(image)
@@ -237,6 +239,7 @@ class ORB(FeatureDetector, DescriptorExtractor):
Corresponding orientations in radians.
"""
assert_nD(image)
pyramid = self._build_pyramid(image)
@@ -282,6 +285,7 @@ class ORB(FeatureDetector, DescriptorExtractor):
Input image.
"""
assert_nD(image)
pyramid = self._build_pyramid(image)
+2 -1
View File
@@ -3,7 +3,7 @@ Methods to characterize image textures.
"""
import numpy as np
from skimage._shared.utils import assert_nD
from ._texture import _glcm_loop, _local_binary_pattern
@@ -279,6 +279,7 @@ def local_binary_pattern(image, P, R, method='default'):
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.214.6851,
2004.
"""
assert_nD(image)
methods = {
'default': ord('D'),
+2 -3
View File
@@ -1,6 +1,7 @@
import numpy as np
from skimage.util import img_as_float
from skimage._shared.utils import assert_nD
class FeatureDetector(object):
@@ -124,9 +125,7 @@ def plot_matches(ax, image1, image2, keypoints1, keypoints2, matches,
def _prepare_grayscale_input_2D(image):
image = np.squeeze(image)
if image.ndim != 2:
raise ValueError("Only 2-D gray-scale images supported.")
assert_nD(image)
return img_as_float(image)
+6
View File
@@ -5,6 +5,7 @@
import numpy as np
from scipy.fftpack import ifftshift
from skimage._shared.utils import assert_nD
eps = np.finfo(float).eps
@@ -118,6 +119,7 @@ class LPIFilter2D(object):
data : (M,N) ndarray
"""
assert_nD(data, 'data')
F, G = self._prepare(data)
out = np.dual.ifftn(F * G)
out = np.abs(_centre(out, data.shape))
@@ -155,6 +157,7 @@ def forward(data, impulse_response=None, filter_params={},
>>> filtered = forward(data.coins(), filt_func)
"""
assert_nD(data, 'data')
if predefined_filter is None:
predefined_filter = LPIFilter2D(impulse_response, **filter_params)
return predefined_filter(data)
@@ -184,6 +187,7 @@ def inverse(data, impulse_response=None, filter_params={}, max_gain=2,
images, construct the LPIFilter2D and specify it here.
"""
assert_nD(data, 'data')
if predefined_filter is None:
filt = LPIFilter2D(impulse_response, **filter_params)
else:
@@ -222,6 +226,8 @@ def wiener(data, impulse_response=None, filter_params={}, K=0.25,
images, construct the LPIFilter2D and specify it here.
"""
assert_nD(data, 'data')
assert_nD(K, 'K')
if predefined_filter is None:
filt = LPIFilter2D(impulse_response, **filter_params)
else:
+2
View File
@@ -6,6 +6,7 @@ __all__ = ['threshold_adaptive',
import numpy as np
import scipy.ndimage
from skimage.exposure import histogram
from skimage._shared.utils import assert_nD
def threshold_adaptive(image, block_size, method='gaussian', offset=0,
@@ -65,6 +66,7 @@ def threshold_adaptive(image, block_size, method='gaussian', offset=0,
>>> func = lambda arr: arr.mean()
>>> binary_image2 = threshold_adaptive(image, 15, 'generic', param=func)
"""
assert_nD(image)
thresh_image = np.zeros(image.shape, 'double')
if method == 'generic':
scipy.ndimage.generic_filter(image, param, block_size,