簡體   English   中英

在 numpy 中移動圖像

[英]Shifting an image in numpy

我有一個 2d numpy 數組中的圖像。 我想通過 X 和 Y 偏移移動圖像,並希望用零填充幀的 rest。 我看過關於“滾動”function 的討論,但它只適用於 1 個軸。 (除非有人可以指出帶填充的 2d 版本)。 我試過切片,但當移動偏移量有所有可能的方向時我遇到了麻煩。 我不想瀏覽所有 XY 偏移 +/- 排列。 有沒有簡單的通用解決方案? 我有下面的代碼,它適用於 X-offset=+100。 但它會因 X-offset=-100 而崩潰。

謝謝,格特

import matplotlib.pyplot as plt
import scipy.misc        as msc
import numpy             as np

lena = msc.lena()
lena.dtype
(imx,imy)= lena.shape
ox= 100
oy= 20
shift_lena = np.zeros((imx,imy))
shift_lena[0:imy-oy,0:imx-ox] = lena[oy:,ox:]
shift_lena_m = shift_lena.astype(np.int64)
shift_lena_m.dtype
plt.figure(figsize=(10, 3.6))
plt.subplot(131)
plt.imshow(lena, cmap=plt.cm.gray)
plt.subplot(132)
plt.imshow(shift_lena_m, cmap=plt.cm.gray)
plt.subplots_adjust(wspace=0, hspace=0., top=0.99, bottom=0.01, left=0.05, right=0.99)
plt.show()

沒有別的方法可以相應地處理負面和正面的變化:

non = lambda s: s if s<0 else None
mom = lambda s: max(0,s)

ox, oy = 100, 20

shift_lena = numpy.zeros_like(lena)
shift_lena[mom(oy):non(oy), mom(ox):non(ox)] = lena[mom(-oy):non(-oy), mom(-ox):non(-ox)]

您可以使用滾動功能循環移位x和y,然后將偏移量進行零填充

def shift_image(X, dx, dy):
    X = np.roll(X, dy, axis=0)
    X = np.roll(X, dx, axis=1)
    if dy>0:
        X[:dy, :] = 0
    elif dy<0:
        X[dy:, :] = 0
    if dx>0:
        X[:, :dx] = 0
    elif dx<0:
        X[:, dx:] = 0
    return X

要沿特定軸移動,對於 integer 和非整數移動,您可以使用:

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 )
    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

一個簡單的例子:

np.random.seed(1)
img = np.random.uniform(0,10,(3,4)).astype('int')
print( img ) 
shift = 1.5
shifted = shift_img_along_axis( img, axis=1, shift=shift )
print( shifted )

圖像打印:

[[4 7 0 3]
 [1 0 1 3]
 [3 5 4 6]]

移動圖像:

[[3.5 1.5 1.5 0. ]
 [0.5 2.  1.5 0. ]
 [4.5 5.  3.  0. ]]

對於 1.5 的移位,移位圖像中的第一個值是 7 和 0 的平均值,依此類推...如果原始圖像中缺少某個值,則將采用附加值 0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM