-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocessing.py
More file actions
153 lines (142 loc) · 4.76 KB
/
Copy pathpreprocessing.py
File metadata and controls
153 lines (142 loc) · 4.76 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
import matplotlib.image as mpimg
import numpy as np
import matplotlib.pyplot as plt
import os,sys
import cv2
from scipy import ndimage
from PIL import Image
from helpers_img import *
from sklearn.cluster import KMeans
from sklearn import preprocessing as prp
def rotation(orig, gts, diagonal=False):
"""
Performs the rotation of an image.
"""
ks=[90,180,270]
rotated=[ndimage.rotate(img,k) for img in orig for k in ks]
gt_rotated=[ndimage.rotate(gt_img,k) for gt_img in gts for k in ks]
orig=orig+rotated
gts=gts+gt_rotated
if diagonal:
rotated=[ndimage.rotate(img,45,reshape=False,mode='reflect') for img in orig]
gt_rotated = [ndimage.rotate(gt_img,45,reshape=False,mode='reflect') for gt_img in gts]
orig = orig + rotated
gts = gts + gt_rotated
return orig,gts
def flip(orig,gts):
"""
Flips an image.
"""
rotated=[cv2.flip(img,1) for img in orig]
gt_rotated=[cv2.flip(gt_img,1) for gt_img in gts]
orig=orig+rotated
gts=gts+gt_rotated
return orig,gts
def add_gray_dimension(img):
"""
Obtains the grayscale channel of an image.
"""
out=np.dot(img[...,:3], [0.299, 0.587, 0.114])
shape_one=[out.shape[0], out.shape[1], 1]
out = np.reshape(out, shape_one)
return out
def add_laplacian(img):
"""
Obtains the Gaussian-Laplacian filter of an image, both coloured and in grayscale.
"""
laplbew=ndimage.gaussian_laplace(add_gray_dimension(img),2)
lapl=ndimage.gaussian_laplace(img,2)
return laplbew,lapl
def add_sobel(img):
"""
Obtains the Sobel filter of an image.
"""
sx = ndimage.sobel(img, axis=0, mode='constant')
sy = ndimage.sobel(img, axis=1, mode='constant')
sob = np.hypot(sx, sy)
return sob
def add_segment(im):
"""
Obtains the histogram-based segmentation of an image.
"""
n = 10
l = 256
im = ndimage.gaussian_filter(im, sigma=l/(4.*n))
mask = (im > im.mean()).astype(np.float)
mask += 0.1 * im
img = mask + 0.2*np.random.randn(*mask.shape)
hist, bin_edges = np.histogram(img, bins=60)
bin_centers = 0.5*(bin_edges[:-1] + bin_edges[1:])
binary_img = img > 0.5
open_img = ndimage.binary_opening(binary_img)
# Remove small black hole
close_img = ndimage.binary_closing(open_img)
close_img=add_gray_dimension(close_img)
return close_img
def add_label_kmeans(img,n_cluster, max_iters, threshold):
"""
Obtains a new channel to an image by KMeans clustering to reduce the number of colours.
"""
original_image = img
x,y,z = original_image.shape
processed_image = original_image.reshape(x*y,z)
model = KMeans(n_clusters=n_cluster, random_state=2, init = 'k-means++', n_init = 2).fit(processed_image)
assignments = model.labels_
mu = model.cluster_centers_
new_image = processed_image.reshape(x,y,z)
assignments = assignments.reshape(x,y)
final_img = np.concatenate((new_image,assignments[:,:,np.newaxis]),axis=2)
return final_img
def add_features(img):
"""
Adds the desired channels to the image img.
"""
gray_img = add_gray_dimension(img)
sob = add_sobel(img)
lapbew,lap=add_laplacian(img)
seg=add_segment(img)
img = np.concatenate((img, gray_img), axis = 2)
img = np.concatenate((img, sob), axis = 2)
img = np.concatenate((img, lapbew), axis = 2)
img = np.concatenate((img, lap), axis = 2)
img = np.concatenate((img, seg), axis = 2)
img = add_label_kmeans(img,25, 100, 1e-6)
return img
def extract_features(img):
"""
Extract the features of an image as the mean and variance of each channel.
"""
feat_m = np.mean(img, axis=(0,1))
feat_v = np.var(img, axis=(0,1))
feat = np.append(feat_m, feat_v)
return feat
def poly_features(feats,deg):
"""
Performs feature augmentations by taking the polynomials and the interactions of the features up to degree deg.
"""
poly = prp.PolynomialFeatures(deg)
feats = poly.fit_transform(feats.reshape(1,-1))
feats = feats.reshape(-1,)
return feats
def add_border(imgs,new_size):
"""
Adds a border to an image by mirror boundary conditions.
"""
old_size = imgs.shape[0]
add = int((new_size-old_size)/2)
if add>0:
new_im = imgs[:add,:,:]
new_im = new_im[::-1,:,:]
final_row = np.concatenate((new_im,imgs[:,:]),axis=0)
new_im = imgs[-add:,:,:]
new_im = new_im[::-1,:,:]
final_row = np.concatenate((final_row,new_im[:,:]),axis=0)
new_im = final_row[:,:add,:]
new_im = new_im[:,::-1,:]
final = np.concatenate((new_im,final_row[:,:]),axis=1)
new_im = final_row[:,-add:,:]
new_im = new_im[:,::-1,:]
final = np.concatenate((final,new_im[:,:]),axis=1)
else:
final = imgs
return final