-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageManipulationTransformation_Part01.py
More file actions
93 lines (74 loc) · 2.33 KB
/
ImageManipulationTransformation_Part01.py
File metadata and controls
93 lines (74 loc) · 2.33 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
"Image Manipulations and Transformations in OpenCV"
"""
import cv2
img = cv2.imread("../Img/3.jpeg")
# Resizing and Scaling
resized = cv2.resize(img, (800, 500))
cv2.imshow("Resized", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("resized.jpg", resized)
# Cropping
cropped = img[100:200, 50:150]
cv2.imshow("Cropped", cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("crop.jpg", cropped)
# Rotation
h, w = img.shape[:2]
center = (h // 2, w // 2)
# Scale as Full 1
M_scalefull = cv2.getRotationMatrix2D(center, 90, scale=1.0)
rotated_img1 = cv2.warpAffine(img, M_scalefull, (w, h))
cv2.imshow("Rotated Img", rotated_img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Scale as Half 0.5
M_scalehalf = cv2.getRotationMatrix2D(center, 90, scale=0.5)
rotated_img2 = cv2.warpAffine(img, M_scalehalf, (w, h))
cv2.imshow("Rotated Img", rotated_img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Scale as Double Zoomed in 2
M_scaledouble = cv2.getRotationMatrix2D(center, 90, scale=2.0)
rotated_img3 = cv2.warpAffine(img, M_scaledouble, (w, h))
cv2.imshow("Rotated Img", rotated_img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Rotated clockwise
M = cv2.getRotationMatrix2D(center, -90, scale=0.5)
rotated_img4 = cv2.warpAffine(img, M, (w, h))
cv2.imshow("Rotated Img", rotated_img4)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Rotated 180 degree
M = cv2.getRotationMatrix2D(center, 180, scale=0.5)
rotated_img5 = cv2.warpAffine(img, M, (w, h))
cv2.imshow("Rotated Img", rotated_img5)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("Rotated_img1.jpeg", rotated_img1)
cv2.imwrite("Rotated_img0.5.jpeg", rotated_img2)
cv2.imwrite("Rotated_img2.jpeg", rotated_img3)
cv2.imwrite("Rotated_img-90.jpeg", rotated_img4)
cv2.imwrite("Rotated_img180.jpeg", rotated_img5)
# Flipping
# Top to Bottom
flipped0 = cv2.flip(img, 0)
cv2.imshow("Flipped", flipped0)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Left to Right
flipped1 = cv2.flip(img, 1)
cv2.imshow("Flipped", flipped1)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Top to Bottom and Left to Right
flippedminus1 = cv2.flip(img, -1)
cv2.imshow("Flipped", flippedminus1)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("Flipped0.jpeg", flipped0)
cv2.imwrite("Flipped1.jpeg", flipped1)
cv2.imwrite("Flipped-1.jpeg", flippedminus1)