def translate_image(image, translationMatrix):
""" Translates the image given a translation matrix."""
# which image shape? (ConvNet (3, w, h) vs. Normal (w, h, 3)
reshape = False
prevShape = image.shape
if image.shape[0] == 3 or image.shape[0] == 1:
reshape = True
if image.shape[0] == image.shape[1] or (image.shape[0] == 1 and image.shape[1] == 3): # grayscale 1L, 1L, h, w OR color 1L, 3L, h, w
reshapeVector = (image.shape[2], image.shape[3], image.shape[1])
else:
reshapeVector = (image.shape[1], image.shape[2], image.shape[0]) # single row color or grayscale 1L/3L, h, w
image = image.reshape(reshapeVector)
h, w = image.shape[0], image.shape[1]
image = cv.warpAffine(image, translationMatrix, (w, h))
if reshape:
image = image.reshape(prevShape)
return image
评论列表
文章目录