Git Product home page Git Product logo

image-transformation's Introduction

Image-Transformation

Aim

To perform image transformation such as Translation, Scaling, Shearing, Reflection, Rotation and Cropping using OpenCV and Python.

Software Required:

Anaconda - Python 3.7

Algorithm:

Step1:

Import the required libraries.

Step2:

Read the image and convert the image from RGB to BGR.

Step3:

Perform the following Image translations

  1. Image Translation
  2. Image Scaling
  3. Image Shearing
  4. Image Reflection
  5. Image Rotation
  6. Image Cropping

Program:

Developed By: Y SHAVEDHA
Register Number: 212221230095

i)Image Translation

#Translation 
image = cv2.imread('img.jpg')
#convert BGR TO RGB
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
# Disable x and y axis
plt.axis('off')
#show the image
plt.imshow(image)
plt.show()
#get the image shape
rows,cols,dim = image.shape
#Transformation matrix for translation
M = np.float32([[1,0,60],
               [0,1,80],
               [0,0,1]])
# Apply a perspective transformation to the image
translated_image = cv2.warpPerspective(image,M,(cols,rows))

#Disable x,y axis
plt.axis('off')
#show the resulting image
plt.imshow(translated_image)
plt.show()

ii) Image Scaling

#Scaling the image
plt.axis('off')
#show the original image
plt.imshow(image)
plt.show()
M = np.float32([[1.5,0,0],
               [0,1.8,0],
               [0,0,1]])
scaled_image = cv2.warpPerspective(image,M,(cols*2,rows*2))
plt.axis('off')
plt.imshow(scaled_image)
plt.show()

iii)Image shearing

# Shearing 
#shearing applied to x axis
M_x = np.float32([[1,0.5,0],
                 [0,1,0],
                [0,0,1]])
#Shearing applied to y-axis
M_y = np.float32([[1,0,0],
                 [0.5,1,0],
                 [0,0,1]])
sheared_img_xaxis = cv2.warpPerspective(image,M_x,(int(cols*1.5),int(rows*1.5)))
sheared_img_yaxis = cv2.warpPerspective(image,M_y,(int(cols*1.5),int(rows*1.5)))
plt.axis('off')
plt.imshow(sheared_img_xaxis)
plt.show()
plt.axis('off')
plt.imshow(sheared_img_yaxis)
plt.show()

iv)Image Reflection

#Reflection
#Transformation matrix for x_axis
M_x = np.float32([[1,0,0],
                 [0,-1,rows],
                 [0,0,1]])
#Transformation matrix for y_axis
M_y = np.float32([[-1,0,cols],
                 [0,1,0],
                 [0,0,1]])
ref_xaxis = cv2.warpPerspective(image,M_x,(int(cols),int(rows)))
ref_yaxis = cv2.warpPerspective(image,M_y,(int(cols),int(rows)))
plt.axis('off')
plt.imshow(ref_xaxis)
plt.show()
plt.axis('off')
plt.imshow(ref_yaxis)
plt.show()

v)Image Rotation

#Rotation
#Angle from degree to radian
angle = np.radians(10)
#Transform matrix for rotation
M = np.float32([[np.cos(angle),-(np.sin(angle)),0],
               [np.sin(angle),np.cos(angle),0],
               [0,0,1]])
rotated_img = cv2.warpPerspective(image,M,(int(cols),int(rows)))
plt.axis('off')
plt.imshow(rotated_img)
plt.show()

vi)Image Cropping

#cropping
#get 200 pixels from 100 to 300 on both x and y axis
cropped_img = image[100:300,100:300]
plt.axis('off')
plt.imshow(cropped_img)
plt.show()

Output:

i)Image Translation

image

ii) Image Scaling

image

iii)Image shearing

image

iv)Image Reflection

image

v)Image Rotation

image

vi)Image Cropping

image

Result:

Thus the different image transformations such as Translation, Scaling, Shearing, Reflection, Rotation and Cropping are done using OpenCV and python programming.

image-transformation's People

Contributors

etjabajasphin avatar shavedha avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.