forked from AmmadHasan/OpenCV_Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformations.py
61 lines (41 loc) · 1.34 KB
/
Transformations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2 as cv
import numpy as np
img = cv.imread('Images/park.jpg')
cv.imshow('park', img)
#1) translation shifting the images
# --x --> left
# --y --> up
# x --> right
# y --> down
def translation(img, x, y):
transMat = np.float32([[1,0,x],[0,1,y]])
dimensions = (img.shape[1], img.shape[0])
# Translated = translation(img, -100, 100)
# cv.imshow('translated', 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) # 45 degree anti-clockwise
crotaed = rotate(img, -45) #45 degree clockwise
cv.imshow('rotated', rotated)
cv.imshow('crotated', crotaed)
rotated_rotated = rotate(crotaed, -45)
cv.imshow('rotated_roatated', rotated_rotated)
#3) resizing
resized = cv.resize(img, (500, 500), interpolation=cv.INTER_CUBIC)
cv.imshow('resized', resized)
#4) flipping
# 0 --> filp vertically
# 1 --> flip horizontally
# -1 --> flip both vertically and horizontally
flip = cv.flip(img, -1)
cv.imshow('0flip', flip)
#cropping
cropped = img[200:400, 300:400]
cv.imshow('cropped', cropped)
cv.waitKey(0)