本文整理匯總了Python中scipy.ndimage.interpolation.map_coordinates方法的典型用法代碼示例。如果您正苦於以下問題:Python interpolation.map_coordinates方法的具體用法?Python interpolation.map_coordinates怎麽用?Python interpolation.map_coordinates使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.ndimage.interpolation
的用法示例。
在下文中一共展示了interpolation.map_coordinates方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, alpha=1000, sigma=30, spline_order=1, mode='nearest', random_state=np.random):
"""Elastic deformation of image as described in [Simard2003]_.
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
"""
assert image.ndim == 3
shape = image.shape[:2]
dx = gaussian_filter((random_state.rand(*shape) * 2 - 1),
sigma, mode="constant", cval=0) * alpha
dy = gaussian_filter((random_state.rand(*shape) * 2 - 1),
sigma, mode="constant", cval=0) * alpha
x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), indexing='ij')
indices = [np.reshape(x + dx, (-1, 1)), np.reshape(y + dy, (-1, 1))]
result = np.empty_like(image)
for i in range(image.shape[2]):
result[:, :, i] = map_coordinates(
image[:, :, i], indices, order=spline_order, mode=mode).reshape(shape)
return result
示例2: map_wrap
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def map_wrap(f, coords):
# Create an agumented array, where the last row and column are added at the beginning of the axes
fa = np.empty((f.shape[0] + 1, f.shape[1] + 1))
#fa[1:, 1:] = f
#fa[0, 1:] = f[-1, :]
#fa[1:, 0] = f[:, -1]
#f[0, 0] = f[-1, -1]
fa[:-1, :-1] = f
fa[-1, :-1] = f[0, :]
fa[:-1, -1] = f[:, 0]
fa[-1, -1] = f[0, 0]
# Wrap coordinates
wrapped_coords_x = coords[0, ...] % f.shape[0]
wrapped_coords_y = coords[1, ...] % f.shape[1]
wrapped_coords = np.r_[wrapped_coords_x[None, ...], wrapped_coords_y[None, ...]]
# Interpolate
#return fa, wrapped_coords, map_coordinates(f, wrapped_coords, order=1, mode='constant', cval=np.nan, prefilter=False)
return map_coordinates(fa, wrapped_coords, order=1, mode='constant', cval=np.nan, prefilter=False)
示例3: elastic_distort
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_distort(image, alpha, sigma):
"""Perform elastic distortion on an image.
Here, alpha refers to the scaling factor that controls the intensity of the
deformation. The sigma variable refers to the Gaussian filter standard
deviation.
"""
random_state = numpy.random.RandomState(None)
shape = image.shape
dx = gaussian_filter(
(random_state.rand(*shape) * 2 - 1),
sigma, mode="constant"
) * alpha
dy = gaussian_filter(
(random_state.rand(*shape) * 2 - 1),
sigma, mode="constant"
) * alpha
x, y = numpy.meshgrid(numpy.arange(shape[0]), numpy.arange(shape[1]))
indices = numpy.reshape(y+dy, (-1, 1)), numpy.reshape(x+dx, (-1, 1))
return map_coordinates(image, indices, order=1).reshape(shape)
示例4: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, label, alpha=1000, sigma=30, spline_order=1, mode='nearest', random_state=np.random):
"""Elastic deformation of image as described in [Simard2003]_.
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
"""
#assert image.ndim == 3
image = np.array(image)
label = np.array(label)
shape = image.shape[:2]
dx = gaussian_filter((random_state.rand(*shape) * 2 - 1),
sigma, mode="constant", cval=0) * alpha
dy = gaussian_filter((random_state.rand(*shape) * 2 - 1),
sigma, mode="constant", cval=0) * alpha
x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), indexing='ij')
indices = [np.reshape(x + dx, (-1, 1)), np.reshape(y + dy, (-1, 1))]
result1 = map_coordinates(image, indices, order=spline_order, mode=mode).reshape(shape)
result2 = map_coordinates(label, indices, order=spline_order, mode=mode).reshape(shape)
return Image.fromarray(result1), Image.fromarray(result2)
示例5: TF_elastic_deform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def TF_elastic_deform(img, alpha=1.0, sigma=1.0):
"""Elastic deformation of images as described in Simard 2003"""
assert len(img.shape) == 3
h, w, nc = img.shape
if nc != 1:
raise NotImplementedError("Multi-channel not implemented.")
# Generate uniformly random displacement vectors, then convolve with gaussian kernel
# and finally multiply by a magnitude coefficient alpha
dx = alpha * gaussian_filter(
(np.random.random((h, w)) * 2 - 1), sigma, mode="constant", cval=0
)
dy = alpha * gaussian_filter(
(np.random.random((h, w)) * 2 - 1), sigma, mode="constant", cval=0
)
# Map image to the deformation mesh
x, y = np.meshgrid(np.arange(h), np.arange(w), indexing='ij')
indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1))
return map_coordinates(img.reshape((h,w)), indices, order=1).reshape(h,w,nc)
示例6: mask_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def mask_transform(mask, coordinates):
h, w = mask.shape
nb_mask = mask.max()
yt, xt = coordinates
y_floor, x_floor = np.floor(yt), np.floor(xt)
score = np.zeros((h, w, nb_mask,))
for (y_shift, x_shift) in [(0,0),(0,1),(1,0),(1,1)]:
mask_index = map_coordinates(mask, (y_floor+y_shift, x_floor+x_shift),
order=0, mode='constant')
index = mask_index!=0
dist = np.sqrt((yt[index]-y_floor[index]-y_shift)**2+(xt[index]-x_floor[index]-x_shift)**2)
score[index, mask_index[index]-1] += 1/dist
index = score.sum(2)!=0
mask_trans = np.zeros_like(mask)
mask_trans[index]= score[index].argmax(1)+1
return mask_trans
示例7: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image,elastic_value_x ,elastic_value_y):
"""Elastic deformation of images as described in [Simard2003]_ (with modifications JUST in Y-DIRECTION).
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
"""
shape = image.shape
random_state = np.random.RandomState(None)
nY = shape[0] // 25
nX = shape[1] // 25
sigma = min(shape[1], shape[0]) * 0.0025
alpha_X = elastic_value_x * min(shape[0], shape[1])
alpha_Y = elastic_value_y * min(shape[0], shape[1])
dx = gaussian_filter((random_state.rand(nY, nX) * 2 - 1), sigma)
dy = gaussian_filter((random_state.rand(nY, nX) * 2 - 1), sigma)
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
dx = misc.imresize(dx, [shape[0], shape[1]], interp='bicubic')
dy = misc.imresize(dy, [shape[0], shape[1]], interp='bicubic')
# plt.imshow(dx, cmap=plt.cm.gray)
# plt.show()
dxT = []
dyT = []
for dummy in range(shape[2]):
dxT.append(dx)
dyT.append(dy)
dx = np.dstack(dxT)
dy = np.dstack(dyT)
dx = dx * alpha_X
dy = dy * alpha_Y
indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))
image = map_coordinates(image, indices, order=1).reshape(shape)
return image
示例8: _get_field
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def _get_field(nifti_image, coords, coil_matrix, get_norm=False):
''' This function is also used in the GUI '''
from scipy.ndimage import interpolation
if isinstance(nifti_image, str):
nifti_image = nib.load(nifti_image)
elif isinstance(nifti_image, nib.nifti1.Nifti1Image):
pass
else:
raise NameError('Failed to parse input volume (not string or nibabel nifti1 volume)')
iM = np.dot(np.linalg.pinv(nifti_image.affine),
np.linalg.pinv(coil_matrix))
# gets the coordinates in voxel space
voxcoords = np.dot(iM[:3, :3], coords.T) + iM[:3, 3][:, np.newaxis]
# Interpolates the values of the field in the given coordinates
out = np.zeros((3, voxcoords.shape[1]))
for dim in range(3):
out[dim] = interpolation.map_coordinates(nifti_image.get_data()[..., dim],
voxcoords,
order=1)
# Rotates the field
out = np.dot(coil_matrix[:3, :3], out)
if get_norm:
out = np.linalg.norm(out, axis=0)
return out
示例9: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, severity=1):
c = [(244 * 2, 244 * 0.7, 244 * 0.1), # 244 should have been 224, but ultimately nothing is incorrect
(244 * 2, 244 * 0.08, 244 * 0.2),
(244 * 0.05, 244 * 0.01, 244 * 0.02),
(244 * 0.07, 244 * 0.01, 244 * 0.02),
(244 * 0.12, 244 * 0.01, 244 * 0.02)][severity - 1]
image = np.array(image, dtype=np.float32) / 255.
shape = image.shape
shape_size = shape[:2]
# random affine
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size])
pts2 = pts1 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)
dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dx, dy = dx[..., np.newaxis], dy[..., np.newaxis]
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))
return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255
# /////////////// End Corruptions ///////////////
示例10: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, severity=1):
c = [(244 * 2, 244 * 0.7, 244 * 0.1), # 244 should have been 224, but ultimately nothing is incorrect
(244 * 2, 244 * 0.08, 244 * 0.2),
(244 * 0.05, 244 * 0.01, 244 * 0.02),
(244 * 0.07, 244 * 0.01, 244 * 0.02),
(244 * 0.12, 244 * 0.01, 244 * 0.02)][severity - 1]
image = np.array(image, dtype=np.float32) / 255.
shape = image.shape
shape_size = shape[:2]
# random affine
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size])
pts2 = pts1 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)
dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dx, dy = dx[..., np.newaxis], dy[..., np.newaxis]
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))
return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255
# /////////////// End Distortions ///////////////
# /////////////// Further Setup ///////////////
示例11: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, severity=1):
IMSIZE = 32
c = [(IMSIZE*0, IMSIZE*0, IMSIZE*0.08),
(IMSIZE*0.05, IMSIZE*0.2, IMSIZE*0.07),
(IMSIZE*0.08, IMSIZE*0.06, IMSIZE*0.06),
(IMSIZE*0.1, IMSIZE*0.04, IMSIZE*0.05),
(IMSIZE*0.1, IMSIZE*0.03, IMSIZE*0.03)][severity - 1]
image = np.array(image, dtype=np.float32) / 255.
shape = image.shape
shape_size = shape[:2]
# random affine
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size])
pts2 = pts1 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)
dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dx, dy = dx[..., np.newaxis], dy[..., np.newaxis]
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))
return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255
# /////////////// End Distortions ///////////////
示例12: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, severity=1):
IMSIZE = 64
c = [(IMSIZE*0, IMSIZE*0, IMSIZE*0.08),
(IMSIZE*0.05, IMSIZE*0.3, IMSIZE*0.06),
(IMSIZE*0.1, IMSIZE*0.08, IMSIZE*0.06),
(IMSIZE*0.1, IMSIZE*0.03, IMSIZE*0.03),
(IMSIZE*0.16, IMSIZE*0.03, IMSIZE*0.02)][severity - 1]
image = np.array(image, dtype=np.float32) / 255.
shape = image.shape
shape_size = shape[:2]
# random affine
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size])
pts2 = pts1 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)
dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dx, dy = dx[..., np.newaxis], dy[..., np.newaxis]
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))
return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255
# /////////////// End Distortions ///////////////
# /////////////// Further Setup ///////////////
示例13: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, severity=1):
c = [(360 * 2, 360 * 0.7, 360 * 0.1),
(360 * 2, 360 * 0.08, 360 * 0.2),
(360 * 0.05, 360 * 0.01, 360 * 0.02),
(360 * 0.07, 360 * 0.01, 360 * 0.02),
(360 * 0.12, 360 * 0.01, 360 * 0.02)][severity - 1]
image = np.array(image, dtype=np.float32) / 255.
shape = image.shape
shape_size = shape[:2]
# random affine
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size])
pts2 = pts1 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)
dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]),
c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32)
dx, dy = dx[..., np.newaxis], dy[..., np.newaxis]
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))
return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255
# /////////////// End Distortions ///////////////
# /////////////// Further Setup ///////////////
示例14: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(x: np.ndarray, amplitude: float, axes: AxesLike = None, order: int = 1):
"""Apply a gaussian elastic distortion with a given amplitude to a tensor along the given axes."""
axes = expand_axes(axes, x.shape)
grid_shape = extract(x.shape, axes)
deltas = [gaussian_filter(np.random.uniform(-amplitude, amplitude, grid_shape), 1) for _ in grid_shape]
grid = np.mgrid[tuple(map(slice, grid_shape))] + deltas
return apply_along_axes(partial(map_coordinates, coordinates=grid, order=order), x, axes)
示例15: elastic_transform
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import map_coordinates [as 別名]
def elastic_transform(image, alpha, sigma):
shape = image.shape
dx = gaussian_filter((np.random.rand(*shape) * 2 - 1),
sigma, mode="constant", cval=0) * alpha
dy = gaussian_filter((np.random.rand(*shape) * 2 - 1),
sigma, mode="constant", cval=0) * alpha
x, y = np.meshgrid(np.arange(shape[0]),
np.arange(shape[1]), indexing='ij')
indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1))
return map_coordinates(image, indices, order=1).reshape(shape)