Skip to content

Commit 433cc05

Browse files
committed
add image transformations tutorial
1 parent 2f6da6e commit 433cc05

File tree

10 files changed

+194
-0
lines changed

10 files changed

+194
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5252
- [Skin Cancer Detection using TensorFlow in Python](https://www.thepythoncode.com/article/skin-cancer-detection-using-tensorflow-in-python). ([code](machine-learning/skin-cancer-detection))
5353
- [How to Perform Malaria Cells Classification using TensorFlow 2 and Keras in Python](https://www.thepythoncode.com/article/malaria-cells-classification). ([code](machine-learning/malaria-classification))
5454
- [How to Make a Barcode Reader in Python](https://www.thepythoncode.com/article/making-a-barcode-scanner-in-python). ([code](general/barcode-reader))
55+
- [Image Transformations using OpenCV in Python](https://www.thepythoncode.com/article/image-transformations-using-opencv-in-python). ([code](machine-learning/image-transformation))
5556
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
5657
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
5758
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [Image Transformations using OpenCV in Python](https://www.thepythoncode.com/article/image-transformations-using-opencv-in-python)
2+
To run all the scripts, you need to install the requirements:
3+
- `pip3 install -r requirements.txt`
120 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
5+
# read the input image
6+
img = cv2.imread("city.jpg")
7+
# convert from BGR to RGB so we can plot using matplotlib
8+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9+
# disable x & y axis
10+
plt.axis('off')
11+
# show the image
12+
plt.imshow(img)
13+
plt.show()
14+
15+
# get 200 pixels from 100 to 300 on both x-axis & y-axis
16+
# change that if you will, just make sure you don't exceed cols & rows
17+
cropped_img = img[100:300, 100:300]
18+
# disable x & y axis
19+
plt.axis('off')
20+
# show the resulting image
21+
plt.imshow(cropped_img)
22+
plt.show()
23+
# save the resulting image to disk
24+
plt.imsave("city_cropped.jpg", cropped_img)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import numpy as np
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
5+
# read the input image
6+
img = cv2.imread("city.jpg")
7+
# convert from BGR to RGB so we can plot using matplotlib
8+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9+
# disable x & y axis
10+
plt.axis('off')
11+
# show the image
12+
plt.imshow(img)
13+
plt.show()
14+
15+
# get the image shape
16+
rows, cols, dim = img.shape
17+
18+
# transformation matrix for x-axis reflection
19+
M = np.float32([[1, 0, 0 ],
20+
[0, -1, rows],
21+
[0, 0, 1 ]])
22+
# transformation matrix for y-axis reflection
23+
# M = np.float32([[-1, 0, cols],
24+
# [ 0, 1, 0 ],
25+
# [ 0, 0, 1 ]])
26+
# apply a perspective transformation to the image
27+
reflected_img = cv2.warpPerspective(img,M,(int(cols),int(rows)))
28+
# disable x & y axis
29+
plt.axis('off')
30+
# show the resulting image
31+
plt.imshow(reflected_img)
32+
plt.show()
33+
# save the resulting image to disk
34+
plt.imsave("city_reflected.jpg", reflected_img)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
matplotlib
2+
opencv-python
3+
numpy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
5+
# read the input image
6+
img = cv2.imread("city.jpg")
7+
# convert from BGR to RGB so we can plot using matplotlib
8+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9+
# disable x & y axis
10+
plt.axis('off')
11+
# show the image
12+
plt.imshow(img)
13+
plt.show()
14+
15+
# get the image shape
16+
rows, cols, dim = img.shape
17+
18+
#angle from degree to radian
19+
angle = np.radians(10)
20+
#transformation matrix for Rotation
21+
M = np.float32([[np.cos(angle), -(np.sin(angle)), 0],
22+
[np.sin(angle), np.cos(angle), 0],
23+
[0, 0, 1]])
24+
# apply a perspective transformation to the image
25+
rotated_img = cv2.warpPerspective(img, M, (int(cols),int(rows)))
26+
# disable x & y axis
27+
plt.axis('off')
28+
# show the resulting image
29+
plt.imshow(rotated_img)
30+
plt.show()
31+
# save the resulting image to disk
32+
plt.imsave("city_rotated.jpg", rotated_img)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
5+
# read the input image
6+
img = cv2.imread("city.jpg")
7+
# convert from BGR to RGB so we can plot using matplotlib
8+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9+
# disable x & y axis
10+
plt.axis('off')
11+
# show the image
12+
plt.imshow(img)
13+
plt.show()
14+
15+
# get the image shape
16+
rows, cols, dim = img.shape
17+
18+
#transformation matrix for Scaling
19+
M = np.float32([[1.5, 0 , 0],
20+
[0, 1.8, 0],
21+
[0, 0, 1]])
22+
# apply a perspective transformation to the image
23+
scaled_img = cv2.warpPerspective(img,M,(cols*2,rows*2))
24+
# disable x & y axis
25+
plt.axis('off')
26+
# show the resulting image
27+
plt.imshow(scaled_img)
28+
plt.show()
29+
# save the resulting image to disk
30+
plt.imsave("city_scaled.jpg", scaled_img)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
5+
# read the input image
6+
img = cv2.imread("city.jpg")
7+
# convert from BGR to RGB so we can plot using matplotlib
8+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9+
# disable x & y axis
10+
plt.axis('off')
11+
# show the image
12+
plt.imshow(img)
13+
plt.show()
14+
15+
# get the image shape
16+
rows, cols, dim = img.shape
17+
18+
# transformation matrix for Shearing
19+
# shearing applied to x-axis
20+
M = np.float32([[1, 0.5, 0],
21+
[0, 1 , 0],
22+
[0, 0 , 1]])
23+
# shearing applied to y-axis
24+
# M = np.float32([[1, 0, 0],
25+
# [0.5, 1, 0],
26+
# [0, 0, 1]])
27+
28+
29+
# apply a perspective transformation to the image
30+
sheared_img = cv2.warpPerspective(img,M,(int(cols*1.5),int(rows*1.5)))
31+
# disable x & y axis
32+
plt.axis('off')
33+
# show the resulting image
34+
plt.imshow(sheared_img)
35+
plt.show()
36+
# save the resulting image to disk
37+
plt.imsave("city_sheared.jpg", sheared_img)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
5+
# read the input image
6+
img = cv2.imread("city.jpg")
7+
# convert from BGR to RGB so we can plot using matplotlib
8+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9+
# disable x & y axis
10+
plt.axis('off')
11+
# show the image
12+
plt.imshow(img)
13+
plt.show()
14+
15+
# get the image shape
16+
rows, cols, dim = img.shape
17+
18+
# transformation matrix for translation
19+
M = np.float32([[1, 0, 50],
20+
[0, 1, 50],
21+
[0, 0, 1]])
22+
# apply a perspective transformation to the image
23+
translated_img = cv2.warpPerspective(img, M, (cols, rows))
24+
# disable x & y axis
25+
plt.axis('off')
26+
# show the resulting image
27+
plt.imshow(translated_img)
28+
plt.show()
29+
# save the resulting image to disk
30+
plt.imsave("city_translated.jpg", translated_img)

0 commit comments

Comments
 (0)