Image translation using numpy

对着背影说爱祢 提交于 2021-02-10 06:34:05

问题


I want to perform image translation by a certain amount (shift the image vertically and horizontally).

The problem is that when I paste the cropped image back on the canvas, I just get back a white blank box.

Can anyone spot the issue here?

Many thanks

img_shape = image.shape

# translate image
# percentage of the dimension of the image to translate
translate_factor_x = random.uniform(*translate)
translate_factor_y = random.uniform(*translate)

# initialize a black image the same size as the image
canvas = np.zeros(img_shape)

# get the top-left corner coordinates of the shifted image
corner_x = int(translate_factor_x*img_shape[1])
corner_y = int(translate_factor_y*img_shape[0])

# determine which part of the image will be pasted
mask = image[max(-corner_y, 0):min(img_shape[0], -corner_y + img_shape[0]),
             max(-corner_x, 0):min(img_shape[1], -corner_x + img_shape[1]),
             :]

# determine which part of the canvas the image will be pasted on
target_coords =  [max(0,corner_y),
                    max(corner_x,0),
                    min(img_shape[0], corner_y + img_shape[0]),
                    min(img_shape[1],corner_x + img_shape[1])]

# paste image on selected part of the canvas
canvas[target_coords[0]:target_coords[2], target_coords[1]:target_coords[3],:] = mask
transformed_img = canvas

plt.imshow(transformed_img)

This is what I get:


回答1:


For image translation, you can make use of the somewhat obscure numpy.roll function. In this example I'm going to use a white canvas so it is easier to visualize.

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)

If you want to flip the image so the black part is on the other side, you can use both np.fliplr and np.flipud.

Result: shifted image



来源:https://stackoverflow.com/questions/63367506/image-translation-using-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!