-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_preprocessing.py
More file actions
159 lines (117 loc) · 4.48 KB
/
Copy pathimage_preprocessing.py
File metadata and controls
159 lines (117 loc) · 4.48 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import imutils
import cv2
import os
from skimage.color import rgb2gray
import random
import numpy as np
from tumor import Tumor
from keras.preprocessing.image import ImageDataGenerator
from skimage import io
from PIL import Image
import shutil
def augmentAllImages(foldername,datasetIn,datasetOut):
counter = 0
for root, _, _ in os.walk(foldername):
if counter> 0 :
pathName = os.path.basename(root)
augmentImagesInFolder(datasetIn+pathName+"/",datasetOut+pathName+"/",4)
counter+=1
def augmentImagesInFolder(image_directory, save_dir, nr_of_copies):
datagen = ImageDataGenerator(
rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant')
dataset =[]
shutil.rmtree(save_dir)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
my_images = os.listdir(image_directory)
for i, image_name in enumerate(my_images):
if (image_name.split('.')[1]== 'jpg'):
image = io.imread(image_directory + image_name)
image = Image.fromarray(image)
image = image.resize((224,224))
dataset.append(np.array(image))
x = np.array(dataset)
i = 0
for _ in datagen.flow(x, batch_size=len(x), save_to_dir=save_dir, save_prefix='aug', save_format='jpg'):
i += 1
if i > nr_of_copies-1:
break
def divideImages(percent,typeOfCancer):
test = []
rangeType = int(len(typeOfCancer)*percent)
for i in range(rangeType):
move = random.randrange(len(typeOfCancer)-i)
element = typeOfCancer[move]
typeOfCancer = np.delete(typeOfCancer,move,0)
test.append(element)
return typeOfCancer,test
def getRandomLists(data,numberOfImages):
testing = []
training = []
#rangeType = int(len(data)*percent)
for i in range(numberOfImages):
move = random.randrange(numberOfImages-i)
element = data[move]
data = np.delete(data,move,0)
testing.append(element)
training = data
return testing,training
def fileToClass(files,typeOfCancer):
cancerClass = []
for cancer in files:
cancerClass.append(Tumor(cancer,typeOfCancer))
return cancerClass
def getTumorsList(glioma,meningioma,pituary,no):
tumors = []
tumors.extend(glioma)
tumors.extend(meningioma)
tumors.extend(pituary)
tumors.extend(no)
return tumors
def crop_image(img):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# threshold the image, then perform a series of erosions +
# dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
# find contours in thresholded image, then grab the largest one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# find the extreme points
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
# add contour on the image
img_cnt = cv2.drawContours(img.copy(), [c], -1, (0, 255, 255), 4)
# add extreme points
img_pnt = cv2.circle(img_cnt.copy(), extLeft, 8, (0, 0, 255), -1)
img_pnt = cv2.circle(img_pnt, extRight, 8, (0, 255, 0), -1)
img_pnt = cv2.circle(img_pnt, extTop, 8, (255, 0, 0), -1)
img_pnt = cv2.circle(img_pnt, extBot, 8, (255, 255, 0), -1)
# crop
ADD_PIXELS = 0
new_img = img[extTop[1]-ADD_PIXELS:extBot[1]+ADD_PIXELS, extLeft[0]-ADD_PIXELS:extRight[0]+ADD_PIXELS].copy()
return new_img
def loadImages(foldername):
image_dict={}
for root, _, files in os.walk(foldername):
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
images=[]
image_dict[os.path.basename(root)]=images
for file in files:
img_data =cv2.imread(root+"/"+ file)
imageCropped=crop_image(img_data)
imageResized=cv2.resize(imageCropped,(224,224),interpolation=cv2.INTER_CUBIC)
imageGrey=rgb2gray(imageResized)
images.append(imageGrey)
return image_dict