在Scikit-image [Python]中移动图像



我想让函数这样做,但它不存在:

from skimage.transform import shift
shifted = shift(image, translation=(15.2, 35.7),
                mode='wrap', preserve_range=True)

您能帮我使用skimage.transform.affinetransform编写功能吗?

from skimage.transform import AffineTransform
def shift(image, translation):
    transform = AffineTransform(translation=translation)
    # How to do it???
    shifted = transform(image)   # Does not work, documentation for usage present
                                 # of this class is not present...
    return shifted

然而,功能scipy.ndimage.interpolation.shift可以做我想要的,它的速度很慢 - 比旋转慢了10-20倍。numpy.roll也不在桌子上,因为它不支持分数翻译。


文档有些意义:http://scikit-image.org/docs/stable/api/skimage.transform.html#skimage.transform.affinetransform

似乎在起作用。但是,如果有人知道更简单,更快的方式 - 请让我知道。

from skimage.transform import AffineTransform, warp
def shift(image, vector):
    transform = AffineTransform(translation=vector)
    shifted = warp(image, transform, mode='wrap', preserve_range=True)
    shifted = shifted.astype(image.dtype)

您可以在2D中进行:

shift_matrix = np.array( [ [ 1, 0,  -15.2,], [0, 1, -35.7 ] , [0, 0, 1] ] ) 
shifted= scipy.ndimage.affine_transform( image, shift_matrix )

,但仍然相对较慢。自制功能可能是:

def shift_img_along_axis( img, axis=0, shift = 1 , constant_values=0):
    """ shift array along a specific axis. New value is taken as weighted by the two distances to the assocaited original pixels.
    CHECKED : Works for floating shift ! ok.
    NOTE: at the border of image, when not enough original pixel is accessible, data will be meaned with regard to additional constant_values. 
    constant_values: value to set to pixels with no association in original image img 
    RETURNS : shifted image. 
    A.Mau. """
    intshift = int(shift)
    remain0 = abs( shift - int(shift) )
    remain1 = 1-remain0 #if shift is uint : remain1=1 and remain0 =0
    npad = int( np.ceil( abs( shift ) ) )  #ceil relative to 0. ( 0.5=> 1 and -0.5=> -1 )
    # npad = int( abs( shift+ 0.5*[-1,1][shift>0] ) ) 
    
    pad_arg = [(0,0)]*img.ndim
    pad_arg[axis] = (npad,npad)
    bigger_image = np.pad( img, pad_arg, 'constant', constant_values=constant_values) 
    
    part1 = remain1*bigger_image.take( np.arange(npad+intshift, npad+intshift+img.shape[axis]) ,axis)
    if remain0==0:
        shifted = part1
    else:
        if shift>0:
            part0 = remain0*bigger_image.take( np.arange(npad+intshift+1, npad+intshift+1+img.shape[axis]) ,axis) #
        else:
            part0 = remain0*bigger_image.take( np.arange(npad+intshift-1, npad+intshift-1+img.shape[axis]) ,axis) #
        shifted = part0 + part1
    return shifted

最新更新