Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image translation using numpy

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:

enter image description here

like image 878
NIM4 Avatar asked Sep 07 '25 04:09

NIM4


1 Answers

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

like image 94
Sebastian Liendo Avatar answered Sep 09 '25 21:09

Sebastian Liendo