Make ndim explicit arg for clarity and update docstring

This commit is contained in:
Steven Silvester
2014-09-21 05:48:58 -05:00
parent bcac1b1a6d
commit 2a057f7246
17 changed files with 44 additions and 37 deletions
+6 -6
View File
@@ -143,23 +143,23 @@ def safe_as_int(val, atol=1e-3):
return np.round(val).astype(np.int64)
def assert_nD(array, arg_name='image', ndim=2):
def assert_nD(array, ndim, arg_name='image'):
"""
Verify an arry meets the desired ndims.
Verify an array meets the desired ndims.
Parameters
----------
array : array-like
Input array to be validated
arg_name : str
The name of the array in the original function.
ndim : int or array-like
ndim : int or iterable of ints
Allowable ndim or ndims for the array.
arg_name : str, optional
The name of the array in the original function.
"""
array = np.asanyarray(array)
msg = "The parameter `%s` must be a %s-dimensional array"
if isinstance(ndim, int):
ndim = [ndim]
if not array.ndim in ndim:
msg = "The parameter `%s` must be a %s-dimensional array"
raise ValueError(msg % (arg_name, '-or-'.join([str(n) for n in ndim])))
+1 -1
View File
@@ -149,7 +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?
#
assert_nD(image)
assert_nD(image, 2)
if low_threshold is None:
low_threshold = 0.1 * dtype_limits(image)[1]
+1 -1
View File
@@ -94,7 +94,7 @@ def daisy(img, step=4, radius=15, rings=3, histograms=8, orientations=8,
.. [2] http://cvlab.epfl.ch/alumni/tola/daisy.html
'''
assert_nD(img, 'img')
assert_nD(img, 2, 'img')
img = img_as_float(img)
+1 -1
View File
@@ -60,7 +60,7 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8),
shadowing and illumination variations.
"""
assert_nD(image)
assert_nD(image, 2)
if normalise:
image = sqrt(image)
+3 -3
View File
@@ -170,7 +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`.
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
@@ -274,7 +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`.
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
@@ -383,7 +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.
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
image = integral_image(image)
+1 -1
View File
@@ -138,7 +138,7 @@ class BRIEF(DescriptorExtractor):
Keypoint coordinates as ``(row, col)``.
"""
assert_nD(image)
assert_nD(image, 2)
np.random.seed(self.sample_seed)
+1 -1
View File
@@ -231,7 +231,7 @@ class CENSURE(FeatureDetector):
# (4) Finally, we remove the border keypoints and return the keypoints
# along with its corresponding scale.
assert_nD(image)
assert_nD(image, 2)
num_scales = self.max_scale - self.min_scale
+3 -3
View File
@@ -167,7 +167,7 @@ class ORB(FeatureDetector, DescriptorExtractor):
Input image.
"""
assert_nD(image)
assert_nD(image, 2)
pyramid = self._build_pyramid(image)
@@ -239,7 +239,7 @@ class ORB(FeatureDetector, DescriptorExtractor):
Corresponding orientations in radians.
"""
assert_nD(image)
assert_nD(image, 2)
pyramid = self._build_pyramid(image)
@@ -285,7 +285,7 @@ class ORB(FeatureDetector, DescriptorExtractor):
Input image.
"""
assert_nD(image)
assert_nD(image, 2)
pyramid = self._build_pyramid(image)
+1 -1
View File
@@ -103,7 +103,7 @@ def match_template(image, template, pad_input=False, mode='constant',
[ 0. , 0. , 0. , 0.125, -1. , 0.125],
[ 0. , 0. , 0. , 0.125, 0.125, 0.125]], dtype=float32)
"""
assert_nD(image, ndim=(2, 3))
assert_nD(image, (2, 3))
if image.ndim < template.ndim:
raise ValueError("Dimensionality of template must be less than or "
+1 -1
View File
@@ -279,7 +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)
assert_nD(image, 2)
methods = {
'default': ord('D'),
+2 -1
View File
@@ -1,5 +1,6 @@
import numpy as np
from scipy import ndimage
from skimage._shared.utils import assert_nD
__all__ = ['gabor_kernel', 'gabor_filter']
@@ -112,7 +113,7 @@ def gabor_filter(image, frequency, theta=0, bandwidth=1, sigma_x=None,
.. [2] http://mplab.ucsd.edu/tutorials/gabor.pdf
"""
assert_nD(image, 2)
g = gabor_kernel(frequency, theta, bandwidth, sigma_x, sigma_y, offset)
filtered_real = ndimage.convolve(image, np.real(g), mode=mode, cval=cval)
+11 -11
View File
@@ -81,7 +81,7 @@ def sobel(image, mask=None):
Note that ``scipy.ndimage.sobel`` returns a directional Sobel which
has to be further processed to perform edge detection.
"""
assert_nD(image)
assert_nD(image, 2)
return np.sqrt(hsobel(image, mask)**2 + vsobel(image, mask)**2)
@@ -112,7 +112,7 @@ def hsobel(image, mask=None):
-1 -2 -1
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, HSOBEL_WEIGHTS))
return _mask_filter_result(result, mask)
@@ -145,7 +145,7 @@ def vsobel(image, mask=None):
1 0 -1
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, VSOBEL_WEIGHTS))
return _mask_filter_result(result, mask)
@@ -215,7 +215,7 @@ def hscharr(image, mask=None):
of Kernel Based Image Derivatives.
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, HSCHARR_WEIGHTS))
return _mask_filter_result(result, mask)
@@ -253,7 +253,7 @@ def vscharr(image, mask=None):
of Kernel Based Image Derivatives.
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, VSCHARR_WEIGHTS))
return _mask_filter_result(result, mask)
@@ -281,7 +281,7 @@ def prewitt(image, mask=None):
Return the square root of the sum of squares of the horizontal
and vertical Prewitt transforms.
"""
assert_nD(image)
assert_nD(image, 2)
return np.sqrt(hprewitt(image, mask)**2 + vprewitt(image, mask)**2)
@@ -312,7 +312,7 @@ def hprewitt(image, mask=None):
-1 -1 -1
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, HPREWITT_WEIGHTS))
return _mask_filter_result(result, mask)
@@ -345,7 +345,7 @@ def vprewitt(image, mask=None):
1 0 -1
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, VPREWITT_WEIGHTS))
return _mask_filter_result(result, mask)
@@ -368,7 +368,7 @@ def roberts(image, mask=None):
output : 2-D array
The Roberts' Cross edge map.
"""
assert_nD(image)
assert_nD(image, 2)
return np.sqrt(roberts_positive_diagonal(image, mask)**2 +
roberts_negative_diagonal(image, mask)**2)
@@ -402,7 +402,7 @@ def roberts_positive_diagonal(image, mask=None):
0 -1
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, ROBERTS_PD_WEIGHTS))
return _mask_filter_result(result, mask)
@@ -437,7 +437,7 @@ def roberts_negative_diagonal(image, mask=None):
-1 0
"""
assert_nD(image)
assert_nD(image, 2)
image = img_as_float(image)
result = np.abs(convolve(image, ROBERTS_ND_WEIGHTS))
return _mask_filter_result(result, mask)
+5 -5
View File
@@ -119,7 +119,7 @@ class LPIFilter2D(object):
data : (M,N) ndarray
"""
assert_nD(data, 'data')
assert_nD(data, 2, 'data')
F, G = self._prepare(data)
out = np.dual.ifftn(F * G)
out = np.abs(_centre(out, data.shape))
@@ -157,7 +157,7 @@ def forward(data, impulse_response=None, filter_params={},
>>> filtered = forward(data.coins(), filt_func)
"""
assert_nD(data, 'data')
assert_nD(data, 2, 'data')
if predefined_filter is None:
predefined_filter = LPIFilter2D(impulse_response, **filter_params)
return predefined_filter(data)
@@ -187,7 +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')
assert_nD(data, 2, 'data')
if predefined_filter is None:
filt = LPIFilter2D(impulse_response, **filter_params)
else:
@@ -226,10 +226,10 @@ 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(data, 2, 'data')
if not isinstance(K, float):
assert_nD(K, 'K')
assert_nD(K, 2, 'K')
if predefined_filter is None:
filt = LPIFilter2D(impulse_response, **filter_params)
+2
View File
@@ -23,6 +23,7 @@ References
"""
import numpy as np
from skimage._shared.utils import assert_nD
from . import percentile_cy
from .generic import _handle_input
@@ -37,6 +38,7 @@ __all__ = ['autolevel_percentile', 'gradient_percentile',
def _apply(func, image, selem, out, mask, shift_x, shift_y, p0, p1,
out_dtype=None):
assert_nD(image, 2)
image, selem, out, mask, max_bin = _handle_input(image, selem, out, mask,
out_dtype)
+2
View File
@@ -25,6 +25,7 @@ References
import numpy as np
from skimage import img_as_ubyte
from skimage._shared.utils import assert_nD
from . import bilateral_cy
from .generic import _handle_input
@@ -36,6 +37,7 @@ __all__ = ['mean_bilateral', 'pop_bilateral', 'sum_bilateral']
def _apply(func, image, selem, out, mask, shift_x, shift_y, s0, s1,
out_dtype=None):
assert_nD(image, 2)
image, selem, out, mask, max_bin = _handle_input(image, selem, out, mask,
out_dtype)
+2
View File
@@ -19,6 +19,7 @@ References
import warnings
import numpy as np
from skimage import img_as_ubyte
from skimage._shared.utils import assert_nD
from . import generic_cy
@@ -30,6 +31,7 @@ __all__ = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'maximum', 'mean',
def _handle_input(image, selem, out, mask, out_dtype=None, pixel_size=1):
assert_nD(image, 2)
if image.dtype not in (np.uint8, np.uint16):
image = img_as_ubyte(image)
+1 -1
View File
@@ -66,7 +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)
assert_nD(image, 2)
thresh_image = np.zeros(image.shape, 'double')
if method == 'generic':
scipy.ndimage.generic_filter(image, param, block_size,