擅长:python、mysql、java
<p>您可以使用滚动函数来循环移动x和y,然后将偏移量归零</p>
<pre><code>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
</code></pre>