mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-27 19:48:43 +08:00
Merge pull request #1005 from JDWarner/int_idx_patch
Fix NumPy non-integer indexing deprecation warnings
This commit is contained in:
@@ -126,8 +126,8 @@ def _clahe(image, ntiles_x, ntiles_y, clip_limit, nbins=128):
|
||||
x_res = image.shape[1] - image.shape[1] % ntiles_x
|
||||
image = image[: y_res, : x_res]
|
||||
|
||||
x_size = image.shape[1] / ntiles_x # Actual size of contextual regions
|
||||
y_size = image.shape[0] / ntiles_y
|
||||
x_size = image.shape[1] // ntiles_x # Actual size of contextual regions
|
||||
y_size = image.shape[0] // ntiles_y
|
||||
n_pixels = x_size * y_size
|
||||
|
||||
if clip_limit > 0.0: # Calculate actual cliplimit
|
||||
@@ -139,7 +139,7 @@ def _clahe(image, ntiles_x, ntiles_y, clip_limit, nbins=128):
|
||||
|
||||
bin_size = 1 + NR_OF_GREY / nbins
|
||||
aLUT = np.arange(NR_OF_GREY)
|
||||
aLUT /= bin_size
|
||||
aLUT //= bin_size
|
||||
img_blocks = view_as_blocks(image, (y_size, x_size))
|
||||
|
||||
# Calculate greylevel mappings for each contextual region
|
||||
|
||||
@@ -112,7 +112,8 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8),
|
||||
|
||||
# compute orientations integral images
|
||||
orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations))
|
||||
subsample = np.index_exp[cy / 2:cy * n_cellsy:cy, cx / 2:cx * n_cellsx:cx]
|
||||
subsample = np.index_exp[cy // 2:cy * n_cellsy:cy,
|
||||
cx // 2:cx * n_cellsx:cx]
|
||||
for i in range(orientations):
|
||||
#create new integral image for this orientation
|
||||
# isolate orientations in this range
|
||||
|
||||
@@ -2,13 +2,13 @@ import numpy as np
|
||||
from scipy import ndimage
|
||||
from scipy import stats
|
||||
|
||||
from skimage.color import rgb2grey
|
||||
from skimage.util import img_as_float, pad
|
||||
from skimage.feature import peak_local_max
|
||||
from skimage.feature.util import _prepare_grayscale_input_2D
|
||||
from skimage.feature.corner_cy import _corner_fast
|
||||
from ._hessian_det_appx import _hessian_matrix_det
|
||||
from ..transform import integral_image
|
||||
from .._shared.utils import safe_as_int
|
||||
|
||||
|
||||
def _compute_derivatives(image, mode='constant', cval=0):
|
||||
@@ -134,7 +134,7 @@ def hessian_matrix(image, sigma=1, mode='constant', cval=0):
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from skimage.feature import hessian_matrix, hessian_matrix_eigvals
|
||||
>>> from skimage.feature import hessian_matrix
|
||||
>>> square = np.zeros((5, 5))
|
||||
>>> square[2, 2] = 1
|
||||
>>> Hxx, Hxy, Hyy = hessian_matrix(square, sigma=0.1)
|
||||
@@ -342,7 +342,7 @@ def corner_harris(image, method='k', k=0.05, eps=1e-6, sigma=1):
|
||||
A = [(imx**2) (imx*imy)] = [Axx Axy]
|
||||
[(imx*imy) (imy**2)] [Axy Ayy]
|
||||
|
||||
Where imx and imy are the first derivatives averaged with a gaussian filter.
|
||||
Where imx and imy are first derivatives, averaged with a gaussian filter.
|
||||
The corner measure is then defined as::
|
||||
|
||||
det(A) - k * trace(A)**2
|
||||
@@ -423,7 +423,7 @@ def corner_shi_tomasi(image, sigma=1):
|
||||
A = [(imx**2) (imx*imy)] = [Axx Axy]
|
||||
[(imx*imy) (imy**2)] [Axy Ayy]
|
||||
|
||||
Where imx and imy are the first derivatives averaged with a gaussian filter.
|
||||
Where imx and imy are first derivatives, averaged with a gaussian filter.
|
||||
The corner measure is then defined as the smaller eigenvalue of A::
|
||||
|
||||
((Axx + Ayy) - sqrt((Axx - Ayy)**2 + 4 * Axy**2)) / 2
|
||||
@@ -486,7 +486,7 @@ def corner_foerstner(image, sigma=1):
|
||||
A = [(imx**2) (imx*imy)] = [Axx Axy]
|
||||
[(imx*imy) (imy**2)] [Axy Ayy]
|
||||
|
||||
Where imx and imy are the first derivatives averaged with a gaussian filter.
|
||||
Where imx and imy are first derivatives, averaged with a gaussian filter.
|
||||
The corner measure is then defined as::
|
||||
|
||||
w = det(A) / trace(A) (size of error ellipse)
|
||||
@@ -683,7 +683,7 @@ def corner_subpix(image, corners, window_size=11, alpha=0.99):
|
||||
image = pad(image, pad_width=wext, mode='constant', constant_values=0)
|
||||
|
||||
# add pad width, make sure to not modify the input values in-place
|
||||
corners = corners + wext
|
||||
corners = safe_as_int(corners + wext)
|
||||
|
||||
# normal equation arrays
|
||||
N_dot = np.zeros((2, 2), dtype=np.double)
|
||||
@@ -767,9 +767,9 @@ def corner_subpix(image, corners, window_size=11, alpha=0.99):
|
||||
|
||||
# determine corner class (dot or edge)
|
||||
# variance for different models
|
||||
var_dot = np.sum(winx_winx * ryy_dot - 2 * winx_winy * rxy_dot \
|
||||
var_dot = np.sum(winx_winx * ryy_dot - 2 * winx_winy * rxy_dot
|
||||
+ winy_winy * rxx_dot)
|
||||
var_edge = np.sum(winy_winy * ryy_edge + 2 * winx_winy * rxy_edge \
|
||||
var_edge = np.sum(winy_winy * ryy_edge + 2 * winx_winy * rxy_edge
|
||||
+ winx_winx * rxx_edge)
|
||||
|
||||
# test value (F-distributed)
|
||||
|
||||
@@ -32,9 +32,11 @@ def test_hog_color_image_unsupported_error():
|
||||
|
||||
def test_hog_basic_orientations_and_data_types():
|
||||
# scenario:
|
||||
# 1) create image (with float values) where upper half is filled by zeros, bottom half by 100
|
||||
# 1) create image (with float values) where upper half is filled by
|
||||
# zeros, bottom half by 100
|
||||
# 2) create unsigned integer version of this image
|
||||
# 3) calculate feature.hog() for both images, both with 'normalise' option enabled and disabled
|
||||
# 3) calculate feature.hog() for both images, both with 'normalise'
|
||||
# option enabled and disabled
|
||||
# 4) verify that all results are equal where expected
|
||||
# 5) verify that computed feature vector is as expected
|
||||
# 6) repeat the scenario for 90, 180 and 270 degrees rotated images
|
||||
@@ -43,7 +45,7 @@ def test_hog_basic_orientations_and_data_types():
|
||||
width = height = 35
|
||||
|
||||
image0 = np.zeros((height, width), dtype='float')
|
||||
image0[height / 2:] = 100
|
||||
image0[height // 2:] = 100
|
||||
|
||||
for rot in range(4):
|
||||
# rotate by 0, 90, 180 and 270 degrees
|
||||
@@ -52,13 +54,17 @@ def test_hog_basic_orientations_and_data_types():
|
||||
# create uint8 image from image_float
|
||||
image_uint8 = image_float.astype('uint8')
|
||||
|
||||
(hog_float, hog_img_float) = feature.hog(image_float, orientations=4, pixels_per_cell=(8, 8),
|
||||
(hog_float, hog_img_float) = feature.hog(
|
||||
image_float, orientations=4, pixels_per_cell=(8, 8),
|
||||
cells_per_block=(1, 1), visualise=True, normalise=False)
|
||||
(hog_uint8, hog_img_uint8) = feature.hog(image_uint8, orientations=4, pixels_per_cell=(8, 8),
|
||||
(hog_uint8, hog_img_uint8) = feature.hog(
|
||||
image_uint8, orientations=4, pixels_per_cell=(8, 8),
|
||||
cells_per_block=(1, 1), visualise=True, normalise=False)
|
||||
(hog_float_norm, hog_img_float_norm) = feature.hog(image_float, orientations=4, pixels_per_cell=(8, 8),
|
||||
(hog_float_norm, hog_img_float_norm) = feature.hog(
|
||||
image_float, orientations=4, pixels_per_cell=(8, 8),
|
||||
cells_per_block=(1, 1), visualise=True, normalise=True)
|
||||
(hog_uint8_norm, hog_img_uint8_norm) = feature.hog(image_uint8, orientations=4, pixels_per_cell=(8, 8),
|
||||
(hog_uint8_norm, hog_img_uint8_norm) = feature.hog(
|
||||
image_uint8, orientations=4, pixels_per_cell=(8, 8),
|
||||
cells_per_block=(1, 1), visualise=True, normalise=True)
|
||||
|
||||
# set to True to enable manual debugging with graphical output,
|
||||
@@ -66,23 +72,42 @@ def test_hog_basic_orientations_and_data_types():
|
||||
if False:
|
||||
import matplotlib.pyplot as plt
|
||||
plt.figure()
|
||||
plt.subplot(2, 3, 1); plt.imshow(image_float); plt.colorbar(); plt.title('image')
|
||||
plt.subplot(2, 3, 2); plt.imshow(hog_img_float); plt.colorbar(); plt.title('HOG result visualisation (float img)')
|
||||
plt.subplot(2, 3, 5); plt.imshow(hog_img_uint8); plt.colorbar(); plt.title('HOG result visualisation (uint8 img)')
|
||||
plt.subplot(2, 3, 3); plt.imshow(hog_img_float_norm); plt.colorbar(); plt.title('HOG result (normalise) visualisation (float img)')
|
||||
plt.subplot(2, 3, 6); plt.imshow(hog_img_uint8_norm); plt.colorbar(); plt.title('HOG result (normalise) visualisation (uint8 img)')
|
||||
plt.subplot(2, 3, 1)
|
||||
plt.imshow(image_float)
|
||||
plt.colorbar()
|
||||
plt.title('image')
|
||||
plt.subplot(2, 3, 2)
|
||||
plt.imshow(hog_img_float)
|
||||
plt.colorbar()
|
||||
plt.title('HOG result visualisation (float img)')
|
||||
plt.subplot(2, 3, 5)
|
||||
plt.imshow(hog_img_uint8)
|
||||
plt.colorbar()
|
||||
plt.title('HOG result visualisation (uint8 img)')
|
||||
plt.subplot(2, 3, 3)
|
||||
plt.imshow(hog_img_float_norm)
|
||||
plt.colorbar()
|
||||
plt.title('HOG result (normalise) visualisation (float img)')
|
||||
plt.subplot(2, 3, 6)
|
||||
plt.imshow(hog_img_uint8_norm)
|
||||
plt.colorbar()
|
||||
plt.title('HOG result (normalise) visualisation (uint8 img)')
|
||||
plt.show()
|
||||
|
||||
# results (features and visualisation) for float and uint8 images must be almost equal
|
||||
# results (features and visualisation) for float and uint8 images must
|
||||
# be almost equal
|
||||
assert_almost_equal(hog_float, hog_uint8)
|
||||
assert_almost_equal(hog_img_float, hog_img_uint8)
|
||||
|
||||
# resulting features should be almost equal when 'normalise' is enabled or disabled (for current simple testing image)
|
||||
# resulting features should be almost equal when 'normalise' is enabled
|
||||
# or disabled (for current simple testing image)
|
||||
assert_almost_equal(hog_float, hog_float_norm, decimal=4)
|
||||
assert_almost_equal(hog_float, hog_uint8_norm, decimal=4)
|
||||
|
||||
# reshape resulting feature vector to matrix with 4 columns (each corresponding to one of 4 directions),
|
||||
# only one direction should contain nonzero values (this is manually determined for testing image)
|
||||
# reshape resulting feature vector to matrix with 4 columns (each
|
||||
# corresponding to one of 4 directions); only one direction should
|
||||
# contain nonzero values (this is manually determined for testing
|
||||
# image)
|
||||
actual = np.max(hog_float.reshape(-1, 4), axis=0)
|
||||
|
||||
if rot in [0, 2]:
|
||||
@@ -101,8 +126,9 @@ def test_hog_orientations_circle():
|
||||
# scenario:
|
||||
# 1) create image with blurred circle in the middle
|
||||
# 2) calculate feature.hog()
|
||||
# 3) verify that the resulting feature vector contains uniformly distributed values for all orientations,
|
||||
# i.e. no orientation is lost or emphasized
|
||||
# 3) verify that the resulting feature vector contains uniformly
|
||||
# distributed values for all orientations, i.e. no orientation is
|
||||
# lost or emphasized
|
||||
# 4) repeat the scenario for other 'orientations' option
|
||||
|
||||
# size of testing image
|
||||
@@ -114,29 +140,39 @@ def test_hog_orientations_circle():
|
||||
image = ndimage.gaussian_filter(image, 2)
|
||||
|
||||
for orientations in range(2, 15):
|
||||
(hog, hog_img) = feature.hog(image, orientations=orientations, pixels_per_cell=(8, 8),
|
||||
cells_per_block=(1, 1), visualise=True, normalise=False)
|
||||
(hog, hog_img) = feature.hog(image, orientations=orientations,
|
||||
pixels_per_cell=(8, 8),
|
||||
cells_per_block=(1, 1), visualise=True,
|
||||
normalise=False)
|
||||
|
||||
# set to True to enable manual debugging with graphical output,
|
||||
# must be False for automatic testing
|
||||
if False:
|
||||
import matplotlib.pyplot as plt
|
||||
plt.figure()
|
||||
plt.subplot(1, 2, 1); plt.imshow(image); plt.colorbar(); plt.title('image_float')
|
||||
plt.subplot(1, 2, 2); plt.imshow(hog_img); plt.colorbar(); plt.title('HOG result visualisation, orientations=%d' % (orientations))
|
||||
plt.subplot(1, 2, 1)
|
||||
plt.imshow(image)
|
||||
plt.colorbar()
|
||||
plt.title('image_float')
|
||||
plt.subplot(1, 2, 2)
|
||||
plt.imshow(hog_img)
|
||||
plt.colorbar()
|
||||
plt.title('HOG result visualisation, '
|
||||
'orientations=%d' % (orientations))
|
||||
plt.show()
|
||||
|
||||
# reshape resulting feature vector to matrix with N columns (each column corresponds to one direction),
|
||||
# reshape resulting feature vector to matrix with N columns (each
|
||||
# column corresponds to one direction),
|
||||
hog_matrix = hog.reshape(-1, orientations)
|
||||
|
||||
# compute mean values in the resulting feature vector for each direction,
|
||||
# these values should be almost equal to the global mean value (since the image contains a circle),
|
||||
# i.e. all directions have same contribution to the result
|
||||
# compute mean values in the resulting feature vector for each
|
||||
# direction, these values should be almost equal to the global mean
|
||||
# value (since the image contains a circle), i.e., all directions have
|
||||
# same contribution to the result
|
||||
actual = np.mean(hog_matrix, axis=0)
|
||||
desired = np.mean(hog_matrix)
|
||||
assert_almost_equal(actual, desired, decimal=1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from numpy.testing import run_module_suite
|
||||
run_module_suite()
|
||||
np.testing.run_module_suite()
|
||||
|
||||
@@ -18,7 +18,7 @@ def _centre(x, oshape):
|
||||
"""Return an array of oshape from the centre of x.
|
||||
|
||||
"""
|
||||
start = (np.array(x.shape) - np.array(oshape)) / 2. + 1
|
||||
start = (np.array(x.shape) - np.array(oshape)) // 2 + 1
|
||||
out = x[[slice(s, s + n) for s, n in zip(start, oshape)]]
|
||||
return out
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ def reconstruction(seed, mask, method='dilation', selem=None, offset=None):
|
||||
|
||||
# Create a list of strides across the array to get the neighbors within
|
||||
# a flattened array
|
||||
value_stride = np.array(images.strides[1:]) / images.dtype.itemsize
|
||||
value_stride = np.array(images.strides[1:]) // images.dtype.itemsize
|
||||
image_stride = images.strides[0] // images.dtype.itemsize
|
||||
selem_mgrid = np.mgrid[[slice(-o, d - o)
|
||||
for d, o in zip(selem.shape, offset)]]
|
||||
@@ -187,7 +187,8 @@ def reconstruction(seed, mask, method='dilation', selem=None, offset=None):
|
||||
value_map = -value_map
|
||||
|
||||
start = index_sorted[0]
|
||||
reconstruction_loop(value_rank, prev, next, nb_strides, start, image_stride)
|
||||
reconstruction_loop(value_rank, prev, next, nb_strides, start,
|
||||
image_stride)
|
||||
|
||||
# Reshape reconstructed image to original image shape and remove padding.
|
||||
rec_img = value_map[value_rank[:image_stride]]
|
||||
|
||||
@@ -722,6 +722,7 @@ class PolynomialTransform(GeometricTransform):
|
||||
rows = src.shape[0]
|
||||
|
||||
# number of unknown polynomial coefficients
|
||||
order = safe_as_int(order)
|
||||
u = (order + 1) * (order + 2)
|
||||
|
||||
A = np.zeros((rows * 2, u + 1))
|
||||
@@ -729,7 +730,7 @@ class PolynomialTransform(GeometricTransform):
|
||||
for j in range(order + 1):
|
||||
for i in range(j + 1):
|
||||
A[:rows, pidx] = xs ** (j - i) * ys ** i
|
||||
A[rows:, pidx + u / 2] = xs ** (j - i) * ys ** i
|
||||
A[rows:, pidx + u // 2] = xs ** (j - i) * ys ** i
|
||||
pidx += 1
|
||||
|
||||
A[:rows, -1] = xd
|
||||
@@ -741,7 +742,7 @@ class PolynomialTransform(GeometricTransform):
|
||||
# singular value
|
||||
params = - V[-1, :-1] / V[-1, -1]
|
||||
|
||||
self.params = params.reshape((2, u / 2))
|
||||
self.params = params.reshape((2, u // 2))
|
||||
|
||||
def __call__(self, coords):
|
||||
"""Apply forward transformation.
|
||||
@@ -958,6 +959,7 @@ def warp_coords(coord_map, shape, dtype=np.float64):
|
||||
>>> warped_image = map_coordinates(image, coords)
|
||||
|
||||
"""
|
||||
shape = safe_as_int(shape)
|
||||
rows, cols = shape[0], shape[1]
|
||||
coords_shape = [len(shape), rows, cols]
|
||||
if len(shape) == 3:
|
||||
|
||||
@@ -94,7 +94,7 @@ def view_as_blocks(arr_in, block_shape):
|
||||
|
||||
arr_in = np.ascontiguousarray(arr_in)
|
||||
|
||||
new_shape = tuple(arr_shape / block_shape) + tuple(block_shape)
|
||||
new_shape = tuple(arr_shape // block_shape) + tuple(block_shape)
|
||||
new_strides = tuple(arr_in.strides * block_shape) + arr_in.strides
|
||||
|
||||
arr_out = as_strided(arr_in, shape=new_shape, strides=new_strides)
|
||||
|
||||
Reference in New Issue
Block a user