Skip to content

Commit

Permalink
Image Transformations In Open-CV
Browse files Browse the repository at this point in the history
  • Loading branch information
0xajinkya committed Oct 20, 2022
1 parent dffdac0 commit 6ef351e
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions imageTransformations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import cv2 as cv
import numpy as np

img = cv.imread('Photos/KenKaneki.jpg')

cv.imshow('Ken Kaneki', img)

# 1. Translation
def translate(img, x, y):
transMat = np.float32([[1,0,x], [0,1,y]])
dimensions = (img.shape[1], img.shape[0])
return cv.warpAffine(img, transMat, dimensions)

# -x ==> Left
# -y ==> Up
# x ==> Right
# y ==> Down

# translated = translate(img, 100, 100)
translated = translate(img, -100, -100)
cv.imshow('Translated Ken', translated)

# 2. Rotation
def rotate(img, angle, rotPoint=None):
(height, width) = img.shape[:2]

if rotPoint is None:
rotPoint = (width//2, height//2)

rotMat = cv.getRotationMatrix2D(rotPoint, angle, 1.0)
dimensions = (width, height)

return cv.warpAffine(img, rotMat, dimensions)

# rotated = rotate(img, 45)
rotated = rotate(img, -45)
cv.imshow('Rotated Ken', rotated)

rotated_rotated = rotate(rotated, -45)
cv.imshow('Doubly Rotated Ken', rotated_rotated)

# 3. Resizing
resized = cv.resize(img, (500,500), interpolation=cv.INTER_CUBIC)
cv.imshow('Rotated', resized)

# 4. Flipping
flip = cv.flip(img, -1)
cv.imshow('Flipped Ken', flip)

# 5. Cropping
cropped = img[200:400, 300:400]
cv.imshow('Cropped Ken', cropped)

cv.waitKey()

0 comments on commit 6ef351e

Please sign in to comment.