-
Notifications
You must be signed in to change notification settings - Fork 6
/
Main.py
232 lines (170 loc) · 6.32 KB
/
Main.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import cv2
import numpy as np
import math
from sklearn import mixture
from scipy import linalg
import itertools
import matplotlib.pyplot as plt
from scipy import linalg
import matplotlib as mpl
from time import time
from scipy import infty
from sklearn import preprocessing
from sklearn.utils import shuffle
from matplotlib import colors as mcolors
from scipy.misc import imfilter, imread
from skimage import color, data, restoration
from scipy.signal import convolve2d as conv2
import matplotlib.cm as cm
from itertools import chain
from skimage import feature
#-----Define Helper Functionas------#
#---------Loads Each Image and Runs GMM Fit---------
color_iter = itertools.cycle(['navy', 'red', 'cornflowerblue', 'gold', 'darkorange','b','cyan'])
# dictionary of color codes for creating segmentation masks
_color_codes = {
1: (171,166, 27),
2: (112, 26, 91,),
3: (61, 42, 61),
4: (19, 118, 140),
5: (227, 25, 227),
6: (139, 69, 19),
7: (56, 161, 48)
}
def plot_results(X, Y_, means, covariances, index, title):
splot = plt.subplot(2, 1, 1 + index)
for i, (mean, covar, color) in enumerate(zip(
means, covariances, color_iter)):
v, w = linalg.eigh(covar)
v = 2. * np.sqrt(2.) * np.sqrt(v)
u = w[0] / linalg.norm(w[0])
# as the DP will not use every component it has access to
# unless it needs it, we shouldn't plot the redundant
# components.
if not np.any(Y_ == i):
continue
plt.scatter(X[Y_ == i, 0], X[Y_ == i, 1], 0.9, color=color)
# Plot an ellipse to show the Gaussian component
angle = np.arctan(u[1] / u[0])
angle = 180. * angle / np.pi # convert to degrees
ell = mpl.patches.Ellipse(mean, v[0], v[1], 180. + angle, color=color)
ell.set_clip_box(splot.bbox)
ell.set_alpha(0.5)
splot.add_artist(ell)
plt.title('Output')
def test(imtest, gmm, dpgmm):
lab1=gmm.predict(imtest)
lab2=dpgmm.predict(imtest)
plot_results(imtest, lab1, gmm.means_, gmm.covariances_, 0
,'Gaussian Mixture')
plot_results(imtest, lab2, dpgmm.means_, dpgmm.covariances_, 1
,'Bayesian Gaussian Mixture with a Dirichlet process prior')
plt.show()
return lab1,lab2
#---------Runs GMM Fit on Each Random Combination of 1000 Points, 'num_patches' number of times---------#
def train(num_patches, image,n_samples,w,h):
for i in range(1, num_patches): #Fit a Gaussian mixture with EM using five components repeatedly with small random samples from the data
imtrain = shuffle(image)
imtrain=imtrain[:1000]
t=time()
gmm = mixture.GaussianMixture(n_components=7, covariance_type='full',
tol=0.001, reg_covar=1e-06, max_iter=1200, n_init=1, init_params='kmeans',
warm_start=True).fit(imtrain)
print "Gaussian Mixture Done in %0.3fs." % (time() - t)
t=time()
# Fit a Dirichlet process Gaussian mixture using five components
dpgmm = mixture.BayesianGaussianMixture(n_components=7, covariance_type='full', weight_concentration_prior_type='dirichlet_distribution',
tol=0.001, reg_covar=1e-06, max_iter=1200, n_init=1, init_params='kmeans', warm_start=True).fit(imtrain)
print "Bayesian Gaussian Mixture Done in %0.3fs." % (time() - t)
return gmm, dpgmm
print "Normal & Bayesian Gaussian Mixture Done"
return gmm, dpgmm
"""
def fix(image,thresh):
#image = color.rgb2gray(image)
image=restoration.denoise_nl_means(image)
gray= cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurval=cv2.Laplacian(np.uint8(gray), cv2.CV_8U).var()
psf = np.ones((5, 5)) / 25
if blurval>thresh:
print "Blurry!"
else:
print "Not Blurry!"
if blurval>thresh:
text="Blur"
image = conv2(image, psf, 'same')
image += 0.1 * image.std() * np.random.standard_normal(image.shape)
deconvolved = restoration.unsupervised_wiener(image, psf, 1, clip=False)
#deblurr= cv2.cvtColor(deconvolved, cv2.COLOR_GRAY2BGR)
deblurr = color.gray2rgb(image)
#print deconvolved
fig = plt.figure()
a=fig.add_subplot(1,2,1)
imgplot = plt.imshow(deblurr)
#show original
a=fig.add_subplot(1,2,2)
imgplot = plt.imshow(image)
plt.show()
return deblurr
return image
"""
def segmented(image,samples,label, num_comp):
#Add dimension to [n,] array
labels=np.expand_dims(label, axis=0)
labels=np.transpose(labels)
for i in range(1,num_comp):
indices=np.where(np.all(labels==i, axis=-1))
indices = np.unravel_index(indices, (w,h), order='C')
type(indices)
indices=np.transpose(indices)
#indices=list(indices)
l = chain.from_iterable(zip(*indices))
for j, (lowercase, uppercase) in enumerate(l):
# set the colour accordingly
image[lowercase,uppercase] = _color_codes[(i)]
return image
def createData(image, n_samples):
#Intialisation for Local Binary Patterns Descriptor
numPoints = 24
#Number of samples per component
radius = 8
img_src = cv2.GaussianBlur(image,(5,5),0)
#blur = cv2.bilateralFilter(img_src,9,75,75)
#blurthresh=100
#imtest = fix(imtest, blurthresh)
imtest=cv2.cvtColor(img_src, cv2.COLOR_BGR2LAB)
img_gray= cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
lbp = feature.local_binary_pattern(img_gray, numPoints,
radius, method="uniform")
lbp=np.reshape(lbp,(n_samples,1))
imtest= np.reshape(imtest, (n_samples, d))
data=np.column_stack((imtest, lbp))
data= preprocessing.normalize(imtest, norm= 'l2')
#data= preprocessing.scale(data);
return data, imtest
##---------Main---------
#LoadImage
img_src = cv2.imread('10.jpg')
w, h, d = original_shape = tuple(img_src.shape)
assert d == 3
# Number of samples per component
n_samples = w*h
#Number of sets of training samples
num_patches=100;
#print w,h
samples, imtest=createData(img_src, n_samples)
#CallTrainStep
gmm, dpgmm=train(num_patches, samples ,n_samples,w,h)
#prepimage(imtest, num_patches)
#Calculate Labels by Testing
lab1,lab2=test(samples, gmm, dpgmm)
#np.set_printoptions(linewidth=300)
#CallSegmentation
seg1=segmented(img_src,samples,lab1, 7)
seg2=segmented(img_src,samples, lab2,7)
#Concatenate Images and Save
vis = np.concatenate((seg1, seg2), axis=1)
cv2.imwrite('segmentation.png', vis)
#rint "lab1=",lab1
#print "lab2=",lab2
cv2.destroyAllWindows()