Repository navigation
Expand file tree
/
Copy pathpreprocessing.py
More file actions
52 lines (42 loc) · 1.4 KB
/
Copy pathpreprocessing.py
File metadata and controls
52 lines (42 loc) · 1.4 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
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
image_path = "images/sample.jpg"
image = cv2.imread(image_path)
if image is None:
raise FileNotFoundError(f"Image not found at path: {image_path}")
cv2.imshow("Original Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
height, width, channels = image.shape
print(f"Height: {height}, Width: {width}, Channels: {channels}")
resized_image = cv2.resize(image, (640, 360))
cv2.imshow("Resized Image", resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(f"Resized Image Shape: {resized_image.shape}")
resized_height, resized_width = resized_image.shape[:2]
x1 = int(resized_width * 0.2)
x2 = int(resized_width * 0.8)
y1 = int(resized_height * 0.2)
y2 = int(resized_height * 0.8)
cropped_image = resized_image[y1:y2, x1:x2]
cv2.imshow("Cropped Image", cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
rgb_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
plt.imshow(rgb_image)
plt.show()
gray_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
plt.imshow(gray_image, cmap='gray')
plt.show()
# 0-255 = 0-1
normalized_image = resized_image.astype(np.float32) / 255.0
print(normalized_image.min())
print(normalized_image.max())
blurred_image = cv2.GaussianBlur(resized_image, (15, 15), 0)
cv2.imshow("Blurred Image", blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("outputs/resized_image.jpg", cropped_image)