本文整理匯總了Python中scipy.ndimage.interpolation.shift方法的典型用法代碼示例。如果您正苦於以下問題:Python interpolation.shift方法的具體用法?Python interpolation.shift怎麽用?Python interpolation.shift使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.ndimage.interpolation
的用法示例。
在下文中一共展示了interpolation.shift方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: highres
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def highres(y,kind='cubic',res=100):
'''
Interpolate data onto a higher resolution grid by a factor of *res*
Args:
y (1d array/list): signal to be interpolated
kind (str): order of interpolation (see docs for scipy.interpolate.interp1d)
res (int): factor to increase resolution of data via linear interpolation
Returns:
shift (float): offset between target and reference signal
'''
y = np.array(y)
x = np.arange(0, y.shape[0])
f = interp1d(x, y,kind='cubic')
xnew = np.linspace(0, x.shape[0]-1, x.shape[0]*res)
ynew = f(xnew)
return xnew,ynew
示例2: computeBackwardPrior
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def computeBackwardPrior(self, posterior, t):
# determine grid axis along which to shift the distribution
axisToTransform = self.study.observationModel.parameterNames.index(self.selectedParameter)
# compute offset to shift parameter grid
params = {name: value for (name, value) in zip(self.hyperParameterNames, self.hyperParameterValues)}
ftm1 = self.function(t - 1 - self.tOffset, **params)
ft = self.function(t - self.tOffset, **params)
d = ftm1 - ft
# normalize offset with respect to lattice constant of parameter grid
d /= self.latticeConstant[axisToTransform]
# build list for all axes of parameter grid (setting only the selected axis to a non-zero value)
dAll = [0] * len(self.latticeConstant)
dAll[axisToTransform] = d
# shift interpolated version of distribution
newPrior = shift(posterior, dAll, order=3, mode='nearest')
# transformation above may violate proper normalization; re-normalization needed
newPrior /= np.sum(newPrior)
return newPrior
示例3: bounce
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def bounce(dataframe: DataFrame, level):
"""
:param dataframe:
:param level:
:return:
1 if it bounces up
0 if no bounce
-1 if it bounces below
"""
from scipy.ndimage.interpolation import shift
open = dataframe['open']
close = dataframe['close']
touch = shift(touches(dataframe, level), 1, cval=np.NAN)
return np.vectorize(_bounce)(open, close, level, touch)
示例4: _getfilename
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def _getfilename():
global IM, text
fn = filedialog.askopenfilename()
# update what is occurring text box
text.delete(1.0, tk.END)
text.insert(tk.END, "reading image file {:s}\n".format(fn))
canvas.draw()
# read image file
if ".txt" in fn:
IM = np.loadtxt(fn)
else:
IM = imread(fn)
if IM.shape[0] % 2 == 0:
text.insert(tk.END, "make image odd size")
IM = shift(IM, (-0.5, -0.5))[:-1, :-1]
# show the image
_display()
示例5: center_kernel
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def center_kernel(kernel, iterations=20):
"""
given a kernel that might not be perfectly centered, this routine computes its light weighted center and then
moves the center in an iterative process such that it is centered
:param kernel: 2d array (odd numbers)
:param iterations: int, number of iterations
:return: centered kernel
"""
kernel = kernel_norm(kernel)
nx, ny = np.shape(kernel)
if nx %2 == 0:
raise ValueError("kernel needs odd number of pixels")
# make coordinate grid of kernel
x_grid, y_grid = util.make_grid(nx, deltapix=1, left_lower=False)
# compute 1st moments to get light weighted center
x_w = np.sum(kernel * util.array2image(x_grid))
y_w = np.sum(kernel * util.array2image(y_grid))
# de-shift kernel
kernel_centered = de_shift_kernel(kernel, shift_x=-x_w, shift_y=-y_w, iterations=iterations)
return kernel_norm(kernel_centered)
示例6: add_layer2image
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def add_layer2image(grid2d, x_pos, y_pos, kernel, order=1):
"""
adds a kernel on the grid2d image at position x_pos, y_pos with an interpolated subgrid pixel shift of order=order
:param grid2d: 2d pixel grid (i.e. image)
:param x_pos: x-position center (pixel coordinate) of the layer to be added
:param y_pos: y-position center (pixel coordinate) of the layer to be added
:param kernel: the layer to be added to the image
:param order: interpolation order for sub-pixel shift of the kernel to be added
:return: image with added layer, cut to original size
"""
x_int = int(round(x_pos))
y_int = int(round(y_pos))
shift_x = x_int - x_pos
shift_y = y_int - y_pos
kernel_shifted = interp.shift(kernel, [-shift_y, -shift_x], order=order)
return add_layer2image_int(grid2d, x_int, y_int, kernel_shifted)
示例7: test_cutout_source
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def test_cutout_source():
"""
test whether a shifted psf can be reproduced sufficiently well
:return:
"""
kernel_size = 5
image = np.zeros((10, 10))
kernel = np.zeros((kernel_size, kernel_size))
kernel[2, 2] = 1
shift_x = 0.5
shift_y = 0
x_c, y_c = 5, 5
x_pos = x_c + shift_x
y_pos = y_c + shift_y
#kernel_shifted = interp.shift(kernel, [shift_y, shift_x], order=1)
image = image_util.add_layer2image(image, x_pos, y_pos, kernel, order=1)
print(image)
kernel_new = kernel_util.cutout_source(x_pos=x_pos, y_pos=y_pos, image=image, kernelsize=kernel_size)
npt.assert_almost_equal(kernel_new[2, 2], kernel[2, 2], decimal=2)
示例8: test_cutout_source_border
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def test_cutout_source_border():
kernel_size = 7
image = np.zeros((10, 10))
kernel = np.zeros((kernel_size, kernel_size))
kernel[2, 2] = 1
shift_x = +0.1
shift_y = 0
x_c, y_c = 2, 5
x_pos = x_c + shift_x
y_pos = y_c + shift_y
#kernel_shifted = interp.shift(kernel, [shift_y, shift_x], order=1)
image = image_util.add_layer2image(image, x_pos, y_pos, kernel, order=1)
kernel_new = kernel_util.cutout_source(x_pos=x_pos, y_pos=y_pos, image=image, kernelsize=kernel_size)
nx_new, ny_new = np.shape(kernel_new)
print(kernel_new)
assert nx_new == kernel_size
assert ny_new == kernel_size
npt.assert_almost_equal(kernel_new[2, 2], kernel[2, 2], decimal=2)
示例9: test_deshift_subgrid
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def test_deshift_subgrid():
# test the de-shifting with a sharpened subgrid kernel
kernel_size = 5
subgrid = 3
fwhm = 1
kernel_subgrid_size = kernel_size * subgrid
kernel_subgrid = np.zeros((kernel_subgrid_size, kernel_subgrid_size))
kernel_subgrid[7, 7] = 2
kernel_subgrid = kernel_util.kernel_gaussian(kernel_subgrid_size, 1./subgrid, fwhm=fwhm)
kernel = util.averaging(kernel_subgrid, kernel_subgrid_size, kernel_size)
shift_x = 0.18
shift_y = 0.2
shift_x_subgird = shift_x * subgrid
shift_y_subgrid = shift_y * subgrid
kernel_shifted_subgrid = interp.shift(kernel_subgrid, [-shift_y_subgrid, -shift_x_subgird], order=1)
kernel_shifted = util.averaging(kernel_shifted_subgrid, kernel_subgrid_size, kernel_size)
kernel_shifted_highres = kernel_util.subgrid_kernel(kernel_shifted, subgrid_res=subgrid, num_iter=1)
#npt.assert_almost_equal(kernel_shifted_highres[7, 7], kernel_shifted_subgrid[7, 7], decimal=10)
示例10: test_shift_long_dist
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def test_shift_long_dist():
"""
input is a shifted kernel by more than 1 pixel
:return:
"""
kernel_size = 9
kernel = np.zeros((kernel_size, kernel_size))
kernel[4, 4] = 2.
shift_x = 2.
shift_y = 1.
input_kernel = interp.shift(kernel, [-shift_y, -shift_x], order=1)
old_style_kernel = interp.shift(input_kernel, [shift_y, shift_x], order=1)
shifted_new = kernel_util.de_shift_kernel(input_kernel, shift_x, shift_y)
assert kernel[3, 2] == shifted_new[3, 2]
assert np.max(old_style_kernel - shifted_new) < 0.01
示例11: drop_shadow
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def drop_shadow(self, alpha, theta, shift, size, op=0.80):
"""
alpha : alpha layer whose shadow need to be cast
theta : [0,2pi] -- the shadow direction
shift : shift in pixels of the shadow
size : size of the GaussianBlur filter
op : opacity of the shadow (multiplying factor)
@return : alpha of the shadow layer
(it is assumed that the color is black/white)
"""
if size%2==0:
size -= 1
size = max(1,size)
shadow = cv.GaussianBlur(alpha,(size,size),0)
[dx,dy] = shift * np.array([-np.sin(theta), np.cos(theta)])
shadow = op*sii.shift(shadow, shift=[dx,dy],mode='constant',cval=0)
return shadow.astype('uint8')
示例12: setup
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def setup(self, bottom, top):
'''
bottom blob:
[ratemap]: Nx1xFxT
[amsFeature]: NxMxFxT
1.check bottom/top blob vector size
2.read in parameters
shift in T,F(time,frequency) domain
'''
# check input
if len(bottom) != 1:
raise Exception("Need one input to apply jitter.")
self.shift_f = 0 # shift over frequency domain
self.shift_t = 0 # shift over time domain
# self.forwardIter = 0
params = eval(self.param_str) # read in as dictionary
# set parameters according to the train_val.prototxt,
# use python built-in function for dictionary
self.min_shift_f = params.get('min_shift_f',0)
self.max_shift_f = params.get('max_shift_f',0)
self.min_shift_t = params.get('min_shift_t',0)
self.max_shift_t = params.get('max_shift_t',0)
示例13: damage_masks
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def damage_masks(labels, shift=True, scale=True, rotate=True, dilate=True):
"""Damages segmentation masks by random transformations.
Args:
labels: Int32 labels tensor of shape (height, width, 1).
shift: Boolean, whether to damage the masks by shifting.
scale: Boolean, whether to damage the masks by scaling.
rotate: Boolean, whether to damage the masks by rotation.
dilate: Boolean, whether to damage the masks by dilation.
Returns:
The damaged version of labels.
"""
def _damage_masks_np(labels_):
return damage_masks_np(labels_, shift, scale, rotate, dilate)
damaged_masks = tf.py_func(_damage_masks_np, [labels], tf.int32,
name='damage_masks')
damaged_masks.set_shape(labels.get_shape())
return damaged_masks
示例14: damage_masks_np
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def damage_masks_np(labels, shift=True, scale=True, rotate=True, dilate=True):
"""Performs the actual mask damaging in numpy.
Args:
labels: Int32 numpy array of shape (height, width, 1).
shift: Boolean, whether to damage the masks by shifting.
scale: Boolean, whether to damage the masks by scaling.
rotate: Boolean, whether to damage the masks by rotation.
dilate: Boolean, whether to damage the masks by dilation.
Returns:
The damaged version of labels.
"""
unique_labels = np.unique(labels)
unique_labels = np.setdiff1d(unique_labels, [0])
# Shuffle to get random depth ordering when combining together.
np.random.shuffle(unique_labels)
damaged_labels = np.zeros_like(labels)
for l in unique_labels:
obj_mask = (labels == l)
damaged_obj_mask = _damage_single_object_mask(obj_mask, shift, scale,
rotate, dilate)
damaged_labels[damaged_obj_mask] = l
return damaged_labels
示例15: _damage_single_object_mask
# 需要導入模塊: from scipy.ndimage import interpolation [as 別名]
# 或者: from scipy.ndimage.interpolation import shift [as 別名]
def _damage_single_object_mask(mask, shift, scale, rotate, dilate):
"""Performs mask damaging in numpy for a single object.
Args:
mask: Boolean numpy array of shape(height, width, 1).
shift: Boolean, whether to damage the masks by shifting.
scale: Boolean, whether to damage the masks by scaling.
rotate: Boolean, whether to damage the masks by rotation.
dilate: Boolean, whether to damage the masks by dilation.
Returns:
The damaged version of mask.
"""
# For now we just do shifting and scaling. Better would be Affine or thin
# spline plate transformations.
if shift:
mask = _shift_mask(mask)
if scale:
mask = _scale_mask(mask)
if rotate:
mask = _rotate_mask(mask)
if dilate:
mask = _dilate_mask(mask)
return mask