Code Monkey home page Code Monkey logo

image-transformatios's Introduction

IMAGE-TRANSFORMATIONS

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 numpy module as np and pandas as pd.

Step2:

Assign the values to variables in the program.

Step3:

Get the values from the user appropriately.

Step4:

Continue the program by implementing the codes of required topics.

Step5:

Thus the program is executed in google colab.

Program:

Developed By: KARTHICK P
Register Number:212222100021

i)Image Translation

import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
    plt.figure(figsize=(6, 6))
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
image_url = 'rose.jpg'  
image = cv2.imread(image_url)
tx = 50
ty = 30  
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]]) 
translated_image = cv2.warpAffine(image, translation_matrix, (image.shape[1], image.shape[0]))
print("Original Image:")
show_image(image)
print("Translated Image:")
show_image(translated_image)

ii) Image Scaling

import numpy as np
import cv2
import matplotlib.pyplot as plt
in_img=cv2.imread("flower1.jpg")
in_img=cv2.cvtColor(in_img,cv2.COLOR_BGR2RGB)
rows,cols,dim=in_img.shape
M=np.float32([[1.5,0 ,0],
              [0,1.8,0],
              [0,0,1]])
scaled_img=cv2.warpPerspective(in_img, M,(cols,rows))
plt.axis('off')
plt.imshow(scaled_img)
plt.show()

iii)Image shearing

import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
    plt.figure(figsize=(6, 6))
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
image_url = 'flower3.jpg'  # Replace with your image URL or file path
image = cv2.imread(image_url)
shear_factor_x = 0.5  
shear_factor_y = 0.2 
shear_matrix = np.float32([[1, shear_factor_x, 0], [shear_factor_y, 1, 0]])
sheared_image = cv2.warpAffine(image, shear_matrix, (image.shape[1], image.shape[0]))
print("Original Image:")
show_image(image)
print("Sheared Image:")
show_image(sheared_image)

iv)Image Reflection

import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
    plt.figure(figsize=(6, 6))
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
image_url = 'flower2.jpg'
image = cv2.imread(image_url)
reflected_image_horizontal = cv2.flip(image, 1)
reflected_image_vertical = cv2.flip(image, 0)
reflected_image_both = cv2.flip(image, -1)
print("Original Image:")
show_image(image)
print("Reflected Horizontally:")
show_image(reflected_image_horizontal)
print("Reflected Vertically:")
show_image(reflected_image_vertical)
print("Reflected Both:")
show_image(reflected_image_both)

v)Image Rotation

import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
    plt.figure(figsize=(6, 6))
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
image_url = 'flower4.jpg'  # Replace with your image URL or file path
image = cv2.imread(image_url)
angle = 45
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
print("Original Image:")
show_image(image)
print("Rotated Image:")
show_image(rotated_image)

vi)Image Cropping

import numpy as np
import cv2
import matplotlib.pyplot as plt
in_img = cv2.imread("flower5.jpg")
in_img = cv2.cvtColor(in_img,cv2.COLOR_BGR2RGB)
plt.imshow(in_img)
plt.show()
cropped_img=in_img[2000:3000 ,1000:2500]
plt.imshow(cropped_img)
plt.show()

Output:

i)Image Translation

WhatsApp Image 2024-03-22 at 8 54 52 AM WhatsApp Image 2024-03-22 at 8 55 16 AM

ii) Image Scaling

WhatsApp Image 2024-03-22 at 8 58 04 AM

WhatsApp Image 2024-03-22 at 8 58 17 AM

iii)Image sharing

WhatsApp Image 2024-03-22 at 8 59 10 AM

WhatsApp Image 2024-03-22 at 8 59 32 AM

iv)Image Reflection

WhatsApp Image 2024-03-22 at 9 02 50 AM WhatsApp Image 2024-03-22 at 9 03 12 AM WhatsApp Image 2024-03-22 at 9 03 05 AM WhatsApp Image 2024-03-22 at 9 02 58 AM

v)Image rotation

WhatsApp Image 2024-03-22 at 9 03 59 AM

vi)Image Cropping

WhatsApp Image 2024-03-22 at 9 04 49 AM WhatsApp Image 2024-03-22 at 9 04 56 AM

Result:

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

image-transformatios's People

Watchers

 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.