/
ImageWorker.py
298 lines (277 loc) · 11.7 KB
/
ImageWorker.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Created on Jun 20, 2013
This class is the core of two important parts of the project:
firstly, the feature calculation is done here. And secondly, the
image segmentation is done here.
More details on every class/method.
@author: Bibiana and Adria
"""
from scipy import ndimage, fftpack
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure
from math import pi
from skimage.util import dtype
from scipy.ndimage.filters import gaussian_laplace
from skimage.color import rgb2hed
from mahotas.features import texture, zernike_moments
import pywt
from scipy.stats.stats import skew, kurtosis
from Tamura import Tamura
from scipy.stats.mstats_basic import mquantiles
"""
Constant for L resizing
"""
rangeL = (1.0/0.95047)*116.- 16.
"""
Constants for HEDAB resizing
"""
imageTest = np.array([[[255,0,0]]], dtype=np.uint8)
minH = rgb2hed(imageTest)[0][0][0]
imageTest = np.array([[[0,255,255]]], dtype=np.uint8)
maxH = rgb2hed(imageTest)[0][0][0]
rangeH = maxH-minH
imageTest = np.array([[[0,255,0]]], dtype=np.uint8)
minE = rgb2hed(imageTest)[0][0][1]
imageTest = np.array([[[255,0,255]]], dtype=np.uint8)
maxE = rgb2hed(imageTest)[0][0][1]
rangeE = maxE-minE
imageTest = np.array([[[0,0,255]]], dtype=np.uint8)
minDAB = rgb2hed(imageTest)[0][0][2]
imageTest = np.array([[[255,255,0]]], dtype=np.uint8)
maxDAB = rgb2hed(imageTest)[0][0][2]
rangeDAB = maxDAB-minDAB
"""
Base class for operating with images. It forces the user to implement
the getPatch method on the classes that inherit from it. It also has
some general purpose methods for ploting and calculating limits.
"""
class ImageWorker:
def __init__(self, image, rows=None, columns=None, convert=False):
if convert:
self.image = dtype.img_as_float(image)
else:
self.image = image
self.rows = len(image) if rows is None else rows
self.columns = len(image[0]) if columns is None else columns
def showOnlyPatches(self, centers, size):
finalImage = np.zeros(self.image.shape, dtype=np.uint8)
for center in centers:
(beginningi, beginningj, finali, finalj) = self.getLimits(center, size)
finalImage[beginningi:finali, beginningj:finalj, :] = self.image[beginningi:finali, beginningj:finalj, :]
plt.imshow(finalImage)
plt.show()
def getLimits(self, center, size):
beginningi = max(center[0] - (size - 1) / 2, 0)
beginningj = max(center[1] - (size - 1) / 2, 0)
finali = min(beginningi + size, self.rows-1)
finalj = min(beginningj + size, self.columns-1)
return (beginningi, beginningj, finali, finalj)
def getPatch(self, center, size):
raise NotImplementedError("Should have implemented this")
"""
3D image (RGB, HSV, LAB, etc.) worker. It gets the needed features, and it implements
the segmentation operation with getBinaryImage(), which gets the binary image that contains
True for those pixels that were below all the thresholds and True for the ones which were not
below all thresholds.
"""
class RGBImageWorker(ImageWorker):
"""
The only1D is used when we are calculating the features of only those pixels
that pass all the filters.
"""
def getGeneralStatistics(self, hara=False, zern=False, tamura=False, only1D=None):
generalStatistics = []
if self.rows == 1 and self.columns == 1:
for index in range(3):
generalStatistics.append(self.image[0,0,index])
return generalStatistics
if not only1D is None:
im = only1D
generalStatistics.extend(self._calculateStatistics(im, haralick=hara, zernike=zern))
fourierTransform = np.abs(fftpack.fft2(im)) #fourierTransform
generalStatistics.extend(self._calculateStatistics(fourierTransform))
waveletTransform = pywt.dwt2(im, 'sym5')[0]
generalStatistics.extend(self._calculateStatistics(waveletTransform))
waveletFourierTransform = pywt.dwt2(fourierTransform, 'sym5')[0]
generalStatistics.extend(self._calculateStatistics(waveletFourierTransform))
if tamura:
generalStatistics.extend(self.get3Dstatistics(tamura=True))
return generalStatistics
for index in range(3):
im = self.image[:, :, index]
generalStatistics.extend(self._calculateStatistics(im, haralick=hara, zernike=zern))
fourierTransform = np.abs(fftpack.fft2(im)) #fourierTransform
generalStatistics.extend(self._calculateStatistics(fourierTransform))
waveletTransform = pywt.dwt2(im, 'sym5')[0]
generalStatistics.extend(self._calculateStatistics(waveletTransform))
waveletFourierTransform = pywt.dwt2(fourierTransform, 'sym5')[0]
generalStatistics.extend(self._calculateStatistics(waveletFourierTransform))
if tamura:
generalStatistics.extend(self.get3Dstatistics(tamura=True))
return generalStatistics
"""
Features calculated with only one component (for example, R from RGB).
"""
def _calculateStatistics(self, img, haralick=False, zernike=False):
result = []
#3-bin histogram
result.extend(mquantiles(img))
#First four moments
result.extend([img.mean(), img.var(), skew(img,axis=None), kurtosis(img,axis=None)])
#Haralick features
if haralick:
integerImage = dtype.img_as_ubyte(img)
result.extend(texture.haralick(integerImage).flatten())
#Zernike moments
if zernike:
result.extend(zernike_moments(img, int(self.rows)/2 + 1))
return result
"""
Features calculated with the whole image at once.
"""
def get3Dstatistics(self, tamura=False):
result = []
#Tamura features
if tamura:
#result.append(Tamura.coarseness(self.image)) it may not work!
result.append(Tamura.contrast(self.image))
result.extend(Tamura.directionality(self.image))
return result
"""
Plot the histogram of a given channel.
"""
def plotHistogram(self, img):
hist, bin_edges = np.histogram(img, bins=60)
bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
plt.plot(bin_centers, hist, lw=2)
plt.show()
"""
Filter an image based on a percentile.
"""
def filterImage(self, im, threshold):
if self.ploting:
self.plotHistogram(im)
return im < np.percentile(im,threshold)
"""
V from HSV color space
"""
def getV(self):
preV = np.asanyarray(self.image)
preV = dtype.img_as_float(preV)
return preV.max(-1)
"""
Y from XYZ color space
"""
def getY(self):
arr = np.asanyarray(self.image)
arr = dtype.img_as_float(arr)
mask = arr > 0.04045
arr[mask] = np.power((arr[mask] + 0.055) / 1.055, 2.4)
arr[~mask] /= 12.92
Y = arr[:, :, 0] * 0.2126 + arr[:, :, 1] * 0.7152 + arr[:, :, 2] * 0.0722
return Y
"""
It returns L from LAB color space and L from LUV color space
"""
def getL(self):
Y = self.getY()
Y = Y / 0.95047
mask = Y > 0.008856
Y2 = np.power(Y, 1. / 3.)
Y[mask] = Y2[mask]
Y[~mask] = 7.787 * Y[~mask] + 16. / 116.
L = (116. * Y) - 16.
L2 = (116. * Y2) - 16.
return (L, L2)
"""
It returns the thresholded (segmented) image, as well as
the HED image and BRVL image.
"""
def getBinaryImage(self):
self.ploting = False
HEDAB = rgb2hed(self.image)
R = self.image[:, :, 0]
G = self.image[:, :, 1]
B = self.image[:, :, 2]
H = HEDAB[:,:,0]
E = HEDAB[:,:,1]
DAB = HEDAB[:,:,2]
BR = B*2/((1+R+G)*(1+B+R+G)) #Blue-ratio image
V = self.getV() #From HSV
(L, L2) = self.getL() #From CIELAB and CIELUV
BRSmoothed = ndimage.gaussian_filter(BR,1)
LSmoothed = ndimage.gaussian_filter(L,1)
VSmoothed = ndimage.gaussian_filter(V,1)
HSmoothed = ndimage.gaussian_filter(H,1)
ESmoothed = ndimage.gaussian_filter(E,1)
RSmoothed = ndimage.gaussian_filter(R,1)
DABSmoothed = ndimage.gaussian_filter(DAB,1)
imLLog = self.filterImage(gaussian_laplace(LSmoothed,9), 85) == False
imVLog = self.filterImage(gaussian_laplace(VSmoothed, 9), 85) == False
imELog = self.filterImage(gaussian_laplace(ESmoothed,9), 84) == False
imRLog = self.filterImage(gaussian_laplace(RSmoothed, 9), 84) == False
imDABLog = self.filterImage(gaussian_laplace(DABSmoothed,9), 50)
imHLog = self.filterImage(gaussian_laplace(HSmoothed,9), 8)
imLog = self.filterImage(gaussian_laplace(BRSmoothed,9), 9)
imR = self.filterImage(R, 2.5)
imB = self.filterImage(B, 10.5)
imV = self.filterImage(V, 6.5)
imL = self.filterImage(L, 2.5)
imL2 = self.filterImage(L2, 2.5)
imE = self.filterImage(E, 18)
imH = self.filterImage(H, 95) == False
imDAB = self.filterImage(DAB, 55) == False
imBR = self.filterImage(BR, 63) == False
binaryImg = imR & imV & imB & imL & imL2 & imE & imH & imDAB & imLog & imBR & imLLog & imVLog & imELog & imHLog & imRLog & imDABLog
openImg = ndimage.binary_opening(binaryImg, iterations=2)
closedImg = ndimage.binary_closing(openImg, iterations=8)
if self.ploting:
plt.imshow(self.image)
plt.show()
plt.imshow(imR)
plt.show()
plt.imshow(imV)
plt.show()
plt.imshow(imB)
plt.show()
plt.imshow(imL)
plt.show()
plt.imshow(closedImg)
plt.show()
BRVL = np.zeros(self.image.shape)
BRVL[:,:,0] = BR
BRVL[:,:,1] = V
BRVL[:,:,2] = L/rangeL
#ResizeHEDAB, from 0 to 1.
HEDAB[:,:,0] = (H - minH)/rangeH
HEDAB[:,:,1] = (E - minE)/rangeE
HEDAB[:,:,2] = (DAB - minDAB)/rangeDAB
return (BinaryImageWorker(closedImg, self.rows, self.columns),
RGBImageWorker(HEDAB, self.rows, self.columns),
RGBImageWorker(BRVL, self.rows, self.columns), BinaryImageWorker(binaryImg, self.rows, self.columns))
def getPatch(self, center, size):
(beginningi, beginningj, finali, finalj) = self.getLimits(center, size)
subImageWorker = RGBImageWorker(self.image[beginningi:finali, beginningj:finalj, :], finali - beginningi, finalj - beginningj)
return subImageWorker
"""
It gets the binary characteristics of an image, such as area, peremiter or roundness.
It also gets the centers of all the blobs from the binary thresholded image.
"""
class BinaryImageWorker(ImageWorker):
def getPatch(self, center, size):
(beginningi, beginningj, finali, finalj) = self.getLimits(center, size)
subImageWorker = BinaryImageWorker(self.image[beginningi:finali, beginningj:finalj], finali - beginningi, finalj - beginningj)
return subImageWorker
def getGeneralStatistics(self):
area = np.sum(self.image)
perimeterArray = [len(x) for x in measure.find_contours(self.image, 0.5)]
perimeter = max(perimeterArray) if len(perimeterArray) != 0 else 0
roundness = 4 * area * pi / (perimeter * perimeter) if perimeter != 0 else 0
finalStatistics = [area, perimeter, roundness, len(self.getCenters())]
return finalStatistics
def getCenters(self):
lbl = ndimage.label(self.image)
centers = ndimage.measurements.center_of_mass(self.image, lbl[0], range(1, lbl[1] + 1))
centers = np.round(centers)
return centers