本文整理汇总了Python中scipy.ndimage.affine_transform方法的典型用法代码示例。如果您正苦于以下问题:Python ndimage.affine_transform方法的具体用法?Python ndimage.affine_transform怎么用?Python ndimage.affine_transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.ndimage
的用法示例。
在下文中一共展示了ndimage.affine_transform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: affine_transform
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def affine_transform(image, affine_value):
shape = image.shape
alpha_affine = min(shape[0], shape[1]) * affine_value
random_state = np.random.RandomState(None)
# Random affine
shape_size = shape[:2]
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 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
M = calcAffineMatrix(pts1, pts2)
R = M[0:2, 0:2]
Off = M[:, 2]
for aD in range(shape[2]):
image[:, :, aD] = scipy_affine_transform(image[:, :, aD], R, offset=Off)
return image
示例2: test_affine_transform09
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform09(self):
data = numpy.array([[4, 1, 3, 2],
[7, 6, 8, 5],
[3, 5, 3, 6]])
for order in range(0, 6):
if (order > 1):
filtered = ndimage.spline_filter(data,
order=order)
else:
filtered = data
out = ndimage.affine_transform(filtered,[[1, 0],
[0, 1]],
[-1, -1], order=order, prefilter=False)
assert_array_almost_equal(out, [[0, 0, 0, 0],
[0, 4, 1, 3],
[0, 7, 6, 8]])
示例3: test_uint64_max
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_uint64_max():
# Test interpolation respects uint64 max. Reported to fail at least on
# win32 (due to the 32 bit visual C compiler using signed int64 when
# converting between uint64 to double) and Debian on s390x.
# Interpolation is always done in double precision floating point, so we
# use the largest uint64 value for which int(float(big)) still fits in
# a uint64.
big = 2**64-1025
arr = np.array([big, big, big], dtype=np.uint64)
# Tests geometric transform (map_coordinates, affine_transform)
inds = np.indices(arr.shape) - 0.1
x = ndimage.map_coordinates(arr, inds)
assert_equal(x[1], int(float(big)))
assert_equal(x[2], int(float(big)))
# Tests zoom / shift
x = ndimage.shift(arr, 0.1)
assert_equal(x[1], int(float(big)))
assert_equal(x[2], int(float(big)))
示例4: test_affine_transform26
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform26(self):
# test homogeneous coordinates
data = numpy.array([[4, 1, 3, 2],
[7, 6, 8, 5],
[3, 5, 3, 6]])
for order in range(0, 6):
if (order > 1):
filtered = ndimage.spline_filter(data, order=order)
else:
filtered = data
tform_original = numpy.eye(2)
offset_original = -numpy.ones((2, 1))
tform_h1 = numpy.hstack((tform_original, offset_original))
tform_h2 = numpy.vstack((tform_h1, [[0, 0, 1]]))
out1 = ndimage.affine_transform(filtered, tform_original,
offset_original.ravel(),
order=order, prefilter=False)
out2 = ndimage.affine_transform(filtered, tform_h1, order=order,
prefilter=False)
out3 = ndimage.affine_transform(filtered, tform_h2, order=order,
prefilter=False)
for out in [out1, out2, out3]:
assert_array_almost_equal(out, [[0, 0, 0, 0],
[0, 4, 1, 3],
[0, 7, 6, 8]])
示例5: map_image
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def map_image(img, out_affine, out_shape, ras2ras=np.array([[1.0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]),
order=1):
"""
Function to map image to new voxel space (RAS orientation)
:param nibabel.MGHImage img: the src 3D image with data and affine set
:param np.ndarray out_affine: trg image affine
:param np.ndarray out_shape: the trg shape information
:param np.ndarray ras2ras: ras2ras an additional maping that should be applied (default=id to just reslice)
:param int order: order of interpolation (0=nearest,1=linear(default),2=quadratic,3=cubic)
:return: mapped Image data array
"""
from scipy.ndimage import affine_transform
from numpy.linalg import inv
# compute vox2vox from src to trg
vox2vox = inv(out_affine) @ ras2ras @ img.affine
# here we apply the inverse vox2vox (to pull back the src info to the target image)
new_data = affine_transform(img.get_data(), inv(vox2vox), output_shape=out_shape, order=order)
return new_data
示例6: transform_coordinate_space
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def transform_coordinate_space(modality_1, modality_2):
"""
Accepts nifty objects
Transfers coordinate space from modality_2 to modality_1
"""
aff_t1 = modality_1.affine
aff_t2 = modality_2.affine
inv_af_2 = np.linalg.inv(aff_t2)
out_shape = modality_1.get_fdata().shape
# desired transformation
T = inv_af_2.dot(aff_t1)
transformed = ndimage.affine_transform(modality_2.get_fdata(), T, output_shape=out_shape)
return transformed
示例7: transform_image
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def transform_image(image, angle=0.0, scale=1.0, aniso=1.0, translation=(0, 0), order=1):
dx, dy = translation
scale = 1.0/scale
c = cos(angle)
s = sin(angle)
sm = np.array([[scale / aniso, 0], [0, scale * aniso]], 'f')
m = np.array([[c, -s], [s, c]], 'f')
m = np.dot(sm, m)
w, h = image.shape
c = np.array([w, h]) / 2.0
d = c - np.dot(m, c) + np.array([dx * w, dy * h])
return ndi.affine_transform(image, m, offset=d, order=order, mode="nearest", output=dtype("f"))
示例8: test_map_coordinates_dts
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_map_coordinates_dts():
# check that ndimage accepts different data types for interpolation
data = np.array([[4, 1, 3, 2],
[7, 6, 8, 5],
[3, 5, 3, 6]])
shifted_data = np.array([[0, 0, 0, 0],
[0, 4, 1, 3],
[0, 7, 6, 8]])
idx = np.indices(data.shape)
dts = (np.uint8, np.uint16, np.uint32, np.uint64,
np.int8, np.int16, np.int32, np.int64,
np.intp, np.uintp, np.float32, np.float64)
for order in range(0, 6):
for data_dt in dts:
these_data = data.astype(data_dt)
for coord_dt in dts:
# affine mapping
mat = np.eye(2, dtype=coord_dt)
off = np.zeros((2,), dtype=coord_dt)
out = ndimage.affine_transform(these_data, mat, off)
assert_array_almost_equal(these_data, out)
# map coordinates
coords_m1 = idx.astype(coord_dt) - 1
coords_p10 = idx.astype(coord_dt) + 10
out = ndimage.map_coordinates(these_data, coords_m1, order=order)
assert_array_almost_equal(out, shifted_data)
# check constant fill works
out = ndimage.map_coordinates(these_data, coords_p10, order=order)
assert_array_almost_equal(out, np.zeros((3,4)))
# check shift and zoom
out = ndimage.shift(these_data, 1)
assert_array_almost_equal(out, shifted_data)
out = ndimage.zoom(these_data, 1)
assert_array_almost_equal(these_data, out)
示例9: test_uint64_max
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_uint64_max():
# Test interpolation respects uint64 max. Reported to fail at least on
# win32 (due to the 32 bit visual C compiler using signed int64 when
# converting between uint64 to double) and Debian on s390x.
big = 2**64-1
arr = np.array([big, big, big], dtype=np.uint64)
# Tests geometric transform (map_coordinates, affine_transform)
inds = np.indices(arr.shape) - 0.1
x = ndimage.map_coordinates(arr, inds)
assert_true(x[1] > (2**63))
# Tests zoom / shift
x = ndimage.shift(arr, 0.1)
assert_true(x[1] > (2**63))
示例10: test_affine_transform01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform01(self):
data = numpy.array([1])
for order in range(0, 6):
out = ndimage.affine_transform(data, [[1]],
order=order)
assert_array_almost_equal(out, [1])
示例11: test_affine_transform02
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform02(self):
data = numpy.ones([4])
for order in range(0, 6):
out = ndimage.affine_transform(data, [[1]],
order=order)
assert_array_almost_equal(out, [1, 1, 1, 1])
示例12: test_affine_transform03
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform03(self):
data = numpy.ones([4])
for order in range(0, 6):
out = ndimage.affine_transform(data, [[1]], -1,
order=order)
assert_array_almost_equal(out, [0, 1, 1, 1])
示例13: test_affine_transform05
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform05(self):
data = numpy.array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
for order in range(0, 6):
out = ndimage.affine_transform(data, [[1, 0],
[0, 1]],
[0, -1], order=order)
assert_array_almost_equal(out, [[0, 1, 1, 1],
[0, 1, 1, 1],
[0, 1, 1, 1]])
示例14: test_affine_transform06
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform06(self):
data = numpy.array([[4, 1, 3, 2],
[7, 6, 8, 5],
[3, 5, 3, 6]])
for order in range(0, 6):
out = ndimage.affine_transform(data, [[1, 0],
[0, 1]],
[0, -1], order=order)
assert_array_almost_equal(out, [[0, 4, 1, 3],
[0, 7, 6, 8],
[0, 3, 5, 3]])
示例15: test_affine_transform07
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import affine_transform [as 别名]
def test_affine_transform07(self):
data = numpy.array([[4, 1, 3, 2],
[7, 6, 8, 5],
[3, 5, 3, 6]])
for order in range(0, 6):
out = ndimage.affine_transform(data, [[1, 0],
[0, 1]],
[-1, 0], order=order)
assert_array_almost_equal(out, [[0, 0, 0, 0],
[4, 1, 3, 2],
[7, 6, 8, 5]])