Translation Image Using Translation Matrix with Python #1

rocky king
4 min readAug 8, 2020

--

Translation Image using Translation Matrix

translation image is part of transformation image that change geometric transformations of image. Translation image is process of shift or move image to other position along distance that you want to move.

move p to p’ along T distance

Image above show p point and move this point to p’ along T distance in 2 dimensions. As simple in coordinate of p is (x,y) when we want to move p to p’. Coordinate of p’ is (x+T,y+T)(actually, T is not always positive. So, coordinate of p’ is (x+T, y+T or x-T, y-T). you can use this method to code in python but now I will use matrix to calculation this method.

In image above show how to calculate new position of each x,y. x,y in equation is position of pixels in image. Equation above have 2 component first component is identity matrix in first row change last component to distance that you want to move this pixel in x coordinate called Tx and second row change last component to distance that is moved too called Ty. Second component is matrix that contain x,y (position in image(order of pixel)) of image and add 1 in third row (From property of matrix col of matrix 1 have to equal row of matrix 2) then bring 2 components dot together. Result is matrix size 2*1 that contain new position of x,y. when you have new position(x’,y’) of pixel, you will change intensity of new position pixel equal intensity from x, y

Coding Part

I use image below is input image and send it to translation_img function that calculation and change intensity follow above algorithm

input image

First -> read input image using OpenCV library and define distance that you want to shift image. It can be positive or negative value. In this example I use shift x coordinate is 100 pixel, shift y coordinate is 100 too.

Second -> create translation_img function take 3 parameter is input_img, shift_distance, shape_of_out_img then extract parameter to some variable.

After that, define translation matrix using numpy library

Then create blank image using np.zeros() and shape is equal shape of input image.

Create 2 dimensional loops for access all pixels in image outer loop access along height, inner loop access along width then create origin_xy matrix and bring 2 matrix dot it together. You will get new position of x,y

Last step is change intensity value of blank image that I create at first. In Addition, carefully about new_xy that less than zero because image position not have negative value it start (0,0) before change intensity you have to check this condition. Last step is return image.

If you want to see image use cv2.imshow() function type follow this code below to see image

This is output from shifted distance = (100,100)

Full version of translation image code

You will see this code in this github

next chapter:

https://medium.com/@bosssds65/how-to-speed-up-time-for-your-own-translation-image-function-2-5ec26776a6b6

--

--