Harmonize all ndimage usage across the library

Only two forms remain in use:

- `from scipy import ndimage as ndi`
- `from scipy.ndimage import function`
This commit is contained in:
Juan Nunez-Iglesias
2015-06-09 15:18:37 +10:00
parent 82a5d0c5d9
commit 0d134987f9
47 changed files with 173 additions and 185 deletions
@@ -71,9 +71,9 @@ ax.set_title('Canny detector')
These contours are then filled using mathematical morphology.
"""
from scipy import ndimage
from scipy import ndimage as ndi
fill_coins = ndimage.binary_fill_holes(edges)
fill_coins = ndi.binary_fill_holes(edges)
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(fill_coins, cmap=plt.cm.gray, interpolation='nearest')
@@ -158,8 +158,8 @@ individually.
from skimage.color import label2rgb
segmentation = ndimage.binary_fill_holes(segmentation - 1)
labeled_coins, _ = ndimage.label(segmentation)
segmentation = ndi.binary_fill_holes(segmentation - 1)
labeled_coins, _ = ndi.label(segmentation)
image_label_overlay = label2rgb(labeled_coins, image=coins)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))
@@ -547,7 +547,7 @@ available in `skimage`.
from time import time
from scipy.ndimage.filters import percentile_filter
from scipy.ndimage import percentile_filter
from skimage.morphology import dilation
from skimage.filters.rank import median, maximum
+3 -3
View File
@@ -17,7 +17,7 @@ the hysteresis thresholding.
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from scipy import ndimage as ndi
from skimage import feature
@@ -26,8 +26,8 @@ from skimage import feature
im = np.zeros((128, 128))
im[32:-32, 32:-32] = 1
im = ndimage.rotate(im, 15, mode='constant')
im = ndimage.gaussian_filter(im, 4)
im = ndi.rotate(im, 15, mode='constant')
im = ndi.gaussian_filter(im, 4)
im += 0.2 * np.random.random(im.shape)
# Compute the Canny filter for two values of sigma
+7 -8
View File
@@ -14,10 +14,9 @@ for classification, which is based on the least squared error for simplicity.
"""
from __future__ import print_function
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage as nd
from scipy import ndimage as ndi
from skimage import data
from skimage.util import img_as_float
@@ -27,7 +26,7 @@ from skimage.filters import gabor_kernel
def compute_feats(image, kernels):
feats = np.zeros((len(kernels), 2), dtype=np.double)
for k, kernel in enumerate(kernels):
filtered = nd.convolve(image, kernel, mode='wrap')
filtered = ndi.convolve(image, kernel, mode='wrap')
feats[k, 0] = filtered.mean()
feats[k, 1] = filtered.var()
return feats
@@ -71,23 +70,23 @@ ref_feats[2, :, :] = compute_feats(wall, kernels)
print('Rotated images matched against references using Gabor filter banks:')
print('original: brick, rotated: 30deg, match result: ', end='')
feats = compute_feats(nd.rotate(brick, angle=190, reshape=False), kernels)
feats = compute_feats(ndi.rotate(brick, angle=190, reshape=False), kernels)
print(image_names[match(feats, ref_feats)])
print('original: brick, rotated: 70deg, match result: ', end='')
feats = compute_feats(nd.rotate(brick, angle=70, reshape=False), kernels)
feats = compute_feats(ndi.rotate(brick, angle=70, reshape=False), kernels)
print(image_names[match(feats, ref_feats)])
print('original: grass, rotated: 145deg, match result: ', end='')
feats = compute_feats(nd.rotate(grass, angle=145, reshape=False), kernels)
feats = compute_feats(ndi.rotate(grass, angle=145, reshape=False), kernels)
print(image_names[match(feats, ref_feats)])
def power(image, kernel):
# Normalize images for better comparison.
image = (image - image.mean()) / image.std()
return np.sqrt(nd.convolve(image, np.real(kernel), mode='wrap')**2 +
nd.convolve(image, np.imag(kernel), mode='wrap')**2)
return np.sqrt(ndi.convolve(image, np.real(kernel), mode='wrap')**2 +
ndi.convolve(image, np.imag(kernel), mode='wrap')**2)
# Plot a selection of the filter bank kernels and their responses.
results = []
+2 -2
View File
@@ -11,7 +11,7 @@ segmentations.
"""
import numpy as np
from scipy import ndimage as nd
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from skimage.filters import sobel
@@ -30,7 +30,7 @@ markers[coins < 30.0 / 255] = background
markers[coins > 150.0 / 255] = foreground
ws = watershed(edges, markers)
seg1 = nd.label(ws == foreground)[0]
seg1 = ndi.label(ws == foreground)[0]
# make segmentation using SLIC superpixels
seg2 = slic(coins, n_segments=117, max_iter=160, sigma=1, compactness=0.75,
+2 -2
View File
@@ -14,7 +14,7 @@ See Wikipedia_ for more details on the algorithm.
"""
from scipy import ndimage
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from skimage.morphology import watershed, disk
@@ -30,7 +30,7 @@ denoised = rank.median(image, disk(2))
# find continuous region (low gradient) --> markers
markers = rank.gradient(denoised, disk(5)) < 10
markers = ndimage.label(markers)[0]
markers = ndi.label(markers)[0]
#local gradient
gradient = rank.gradient(denoised, disk(2))
+2 -2
View File
@@ -21,7 +21,7 @@ a skeleton by iterative morphological thinnings.
"""
import numpy as np
from scipy import ndimage
from scipy import ndimage as ndi
from skimage.morphology import medial_axis
import matplotlib.pyplot as plt
@@ -43,7 +43,7 @@ def microstructure(l=256):
generator = np.random.RandomState(1)
points = l * generator.rand(2, n**2)
mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1
mask = ndimage.gaussian_filter(mask, sigma=l/(4.*n))
mask = ndi.gaussian_filter(mask, sigma=l/(4.*n))
return mask > mask.mean()
data = microstructure(l=64)
+2 -2
View File
@@ -10,7 +10,7 @@ size of the dilation. Locations where the original image is equal to the
dilated image are returned as local maxima.
"""
from scipy import ndimage
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from skimage.feature import peak_local_max
from skimage import data, img_as_float
@@ -19,7 +19,7 @@ im = img_as_float(data.coins())
# image_max is the dilation of im with a 20*20 structuring element
# It is used within peak_local_max function
image_max = ndimage.maximum_filter(im, size=20, mode='constant')
image_max = ndi.maximum_filter(im, size=20, mode='constant')
# Comparison between image_max and im to find the coordinates of local maxima
coordinates = peak_local_max(im, min_distance=20)
@@ -21,7 +21,6 @@ values, and use the random walker for the segmentation.
"""
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
from skimage.segmentation import random_walker
+1 -1
View File
@@ -21,7 +21,7 @@ import matplotlib.pyplot as plt
from skimage import data
from skimage.feature import register_translation
from skimage.feature.register_translation import _upsampled_dft
from scipy.ndimage.fourier import fourier_shift
from scipy.ndimage import fourier_shift
image = data.camera()
shift = (-2.4, 1.32)
+3 -3
View File
@@ -26,7 +26,7 @@ See Wikipedia_ for more details on the algorithm.
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from scipy import ndimage as ndi
from skimage.morphology import watershed
from skimage.feature import peak_local_max
@@ -42,10 +42,10 @@ image = np.logical_or(mask_circle1, mask_circle2)
# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance to the background
distance = ndimage.distance_transform_edt(image)
distance = ndi.distance_transform_edt(image)
local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((3, 3)),
labels=image)
markers = ndimage.label(local_maxi)[0]
markers = ndi.label(local_maxi)[0]
labels = watershed(-distance, markers, mask=image)
fig, axes = plt.subplots(ncols=3, figsize=(8, 2.7))