2 回答

TA貢獻1830條經驗 獲得超9個贊
對于圖像翻譯,您可以使用有點晦澀的numpy.roll功能。在這個例子中,我將使用白色畫布,這樣更容易可視化。
image = np.full_like(original_image, 255)
height, width = image.shape[:-1]
shift = 100
# shift image
rolled = np.roll(image, shift, axis=[0, 1])
# black out shifted parts
rolled = cv2.rectangle(rolled, (0, 0), (width, shift), 0, -1)
rolled = cv2.rectangle(rolled, (0, 0), (shift, height), 0, -1)
如果你想翻轉圖像使黑色部分在另一邊,你可以同時使用np.fliplr和np.flipud。
結果:

TA貢獻1871條經驗 獲得超13個贊
tx這是一個簡單的解決方案,它僅使用數組索引按像素轉換圖像ty,不會翻轉,并且還可以處理負值:
tx, ty = 8, 5 # translation on x and y axis, in pixels
N, M = image.shape
image_translated = np.zeros_like(image)
image_translated[max(tx,0):M+min(tx,0), max(ty,0):N+min(ty,0)] = image[-min(tx,0):M-max(tx,0), -min(ty,0):N-max(ty,0)]
例子:
(請注意,為簡單起見,它不處理tx > M
或 的情況ty > N
)。
添加回答
舉報