Image Translation in opencv

Image Translation in OpenCV Python

Image Input :





import cv2

import numpy as np

img = cv2.imread('1.jpg')

rows, cols = img.shape[0],img.shape[1]

translation= np.float32([ [1,0,70], [0,1,110] ])

img_translation = cv2.warpAffine(img, translation, (cols,rows), cv2.INTER_LINEAR)

cv2.imshow('Translation', img_translation)

cv2.waitKey(0)


Output:




Code Explanation : 

  • We have first import numpy and cv2
  • rows = img.shape[0] it defines no. of  rows in  our image
  • cols = img.shape[1] it defines no. of cols in our image
  • translation basically means how much we want to translate the image here you will read np.float ([[1,0,70],[0,1,110]]) here 70 defines translation of image 70 pixels in X axis similarly 110 defines translation of image 110 pixels on Y axis
  • To see this translation into effect we will use cv2.wrapAffine comand
    first parameter is image name
    second parameter is translation in our image that we already define in our translation variable matrix
    third parameter tells total no. of rows and cols in our image
    Last parameter tells about interpolation type 

Post a Comment

0 Comments