-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreprocess.py
70 lines (52 loc) · 1.72 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
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
'''
Preprocess e csv writing
Data preprocess:
* rescale to equal size (32 x 32)
* convert to grayscale
* mask black pixels
* join mask and grayscale
'''
from PIL import Image, ImageEnhance, ImageOps
import os
import numpy as np
import pandas as pd
# Set wd to source file location
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
gt_size = (32,32)
row_list = []
folders = ['Uninfected', 'Parasitized']
for cat in [0,1]:
names = os.listdir(os.path.join('data', folders[cat]))
for name in names:
img = Image.open(os.path.join('data', folders[cat], name))
# Create B/W contrast enhanced
opt = img.convert(mode='L')
enhancer = ImageEnhance.Contrast(opt)
opt = enhancer.enhance(10)
# Create Mask
mask = img
pixels_mask = mask.load()
for i in range(mask.size[0]):
for j in range(mask.size[1]):
if pixels_mask[i,j] == (0,0,0):
pixels_mask[i,j] = (255, 255, 255)
# Join Mask over B/W
pixels_opt = opt.load()
for i in range(mask.size[0]):
for j in range(mask.size[1]):
if pixels_mask[i,j] == (255, 255, 255):
pixels_opt[i,j] = 255
# Invert
opt = ImageOps.invert(opt)
# Resize
opt = opt.resize(gt_size)
# Finalize
array = np.array(opt).reshape(gt_size[0]*gt_size[1])
row = np.append(cat, array) # append label
row_list.append(row)
# Build Df
colnames = ['label'] + list(range(gt_size[0]*gt_size[1]))
df = pd.DataFrame(row_list, columns=colnames)
# Write csv
df.to_csv('dataset/dat_stain.csv', index=False)