-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpreprocess.py
37 lines (29 loc) · 1.34 KB
/
preprocess.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
import os
import cv2
import pandas as pd
root_dir = os.getcwd()
file_list = ['train.csv', 'val.csv']
image_source_dir = os.path.join(root_dir, 'data/images/')
data_root = os.path.join(root_dir, 'data')
for file in file_list:
image_target_dir = os.path.join(data_root, file.split(".")[0])
if not os.path.exists(image_target_dir):
os.mkdir(image_target_dir)
# read list of image files to process from file
image_list = pd.read_csv(os.path.join(data_root, file), header=None)[0]
image_list = image_list[1:]
print("Start preprocessing images")
for image in image_list:
# open image file
img = cv2.imread(os.path.join(image_source_dir, image))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# perform transformations on image
b = cv2.distanceTransform(img, distanceType=cv2.DIST_L2, maskSize=5)
g = cv2.distanceTransform(img, distanceType=cv2.DIST_L1, maskSize=5)
r = cv2.distanceTransform(img, distanceType=cv2.DIST_C, maskSize=5)
# merge the transformed channels back to an image
transformed_image = cv2.merge((b, g, r))
target_file = os.path.join(image_target_dir, image)
print("Writing target file {}".format(target_file))
cv2.imwrite(target_file, transformed_image)
print("Finished preprocessing images")