forked from mfenner1/py_coloc_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageProcessing.py
336 lines (263 loc) · 11.6 KB
/
ImageProcessing.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import Image, math
#from scipy.misc import fromimage
# from scipy import dot
from numpy import (mean, float_, dot, interp,
uint8, uint16, uint64, log10, any as np_any,
all as np_all)
from operator import itemgetter as ig
from DefinitionsAndUtils import *
from NumericalStatistics import quickQuantiles
#
# Return desired quantiles (R,G,B) on an image
#
def channelQuantilesFromImage(img, iles = (.5), useSampling=False):
imageAsList = list(img.getdata())
# if useSample:
# print "*** !!!UNTESTED CODE. UNTESTED CODE!!! ***"
# indices = random.sample(xrange(numPoints), int(numPoints*samplePct))
# sampledlst = ig(*indices)(currentAsList) # a tuple
# evensize = int(math.sqrt(samplePct) * currentImage.size[0])
#
# imageAsList = sampledlst[:evensize*evensize]
pixelCount = len(imageAsList)
# quantiles
redq = quickQuantiles((ig(R)(c) for c in imageAsList),
pixelCount, quantiles=iles)
greenq = quickQuantiles((ig(G)(c) for c in imageAsList),
pixelCount, quantiles=iles)
blueq = quickQuantiles((ig(B)(c) for c in imageAsList),
pixelCount, quantiles=iles)
return redq, greenq, blueq
###################################################
###################################################
###################################################
#
# Apply a threshold {R:rthresh, etc.} to an image
#
###################################################
###################################################
###################################################
# def makePointMapper(threshold):
# def newValue(v, T=threshold):
# slope = (255 / (255.0 - T))
# intercept = -slope * T
# newV = int(slope * v + intercept)
# return max(0, newV)
# return newValue
# thresholds["R"] = 27
# thresholds["B"] = 35
# thresholds["G"] = 35
def thresholdNDArray(array, thresholds, dropSaturated=False):
maxValue = 254 if dropSaturated else 255
# threshold[c] --> 0 and 255 --> maxValue
for c in colors:
array[:,c] = interp(array[:,c],
[thresholds[colorToName[c]], maxValue],
[0,255]
).astype(uint8)
# drop 255 b/c it's saturated
# def thresholdAndDrop255NDArray(array, thresholds):
# # threshold[c] --> 0 and 254 --> 255
# clean = array<255
# array = array[np_all(clean, 1)]
# for c in colors:
# array[:,c] = interp(array[:,c],
# [thresholds[colorToName[c]], 254],
# [0,255]
# ).astype(uint8)
# def thresholdImage(image, thresholds):
# sourceChannels = image.split()
# # 0,1,2 are RGB for this image format
# colorChannelLists = {R : list(sourceChannels[0].getdata()),
# G : list(sourceChannels[1].getdata()),
# B : list(sourceChannels[2].getdata())}
# newColorChannelLists = {}
# for c in colorChannelLists:
# ptMapFunc = makePointMapper(thresholds[colorToName[c]])
# newColorChannelLists[c] = sourceChannels[c].point(ptMapFunc)
# threshedImage = Image.merge(image.mode,
# (newColorChannelLists[R],
# newColorChannelLists[G],
# newColorChannelLists[B]))
# return threshedImage
###################################################
###################################################
###################################################
#
# Image processing with numpy functions
#
###################################################
###################################################
###################################################
# safe for uints/ints
def mydot(a,b):
prods = float_(a)*float_(b)
return prods.sum()
#def mydot(a,b):
# return dot(a.astype(float_), b.astype(float_))
#def mydot(a,b):
# return float_(dot(a, b))
#
# this expects a 1d of pixels, with 2nd dimension as colors
#
# ccc => compute colocalization coefficients
def cccOnFlatArray(ia):
# in flat form, 2nd dimension holds channel;
# cca -> color channel arrays
cca = {}
cca[R] = ia[:,R] #.flatten() I don't think flatten does any additional
cca[G] = ia[:,G] #.flatten() work here
cca[B] = ia[:,B] #.flatten()
#
# compute pieces
#
### FIXME consider replacing **2 pow(,2) with multiplication
### there is a sum of squares function in scipy.stats
#means = dict((c, mean(cca[c]) for c in colors)
# can replace mean(cca[c]) with means[c] but check it!
meanDiffs = dict((c, cca[c]-mean(cca[c])) for c in colors)
sqMeanDiffs = dict((c, meanDiffs[c]**2) for c in colors) # ok for float
sumSqMeanDiffs = dict((c, sqMeanDiffs[c].sum()) for c in colors)
sumSqrs = dict((c, mydot(cca[c], cca[c])) for c in colors)
indicator = dict((c, cca[c]>0) for c in colors)
sums = dict((c, cca[c].sum(dtype=float_)) for c in colors) # .sum() is ok
crossDot = {}
for c1, c2 in ((R,G), (R,B), (G,B)):
crossDot[(c1,c2)] = mydot(cca[c1], cca[c2])
result = {}
for c1, c2 in ((R,G), (R,B), (G,B)):
theseCoeffs = {}
# scipy.dot() is ok for float
theseCoeffs["Pearson"] = dot(meanDiffs[c1],meanDiffs[c2]) / \
math.sqrt(dot(sumSqMeanDiffs[c1],
sumSqMeanDiffs[c2]))
theseCoeffs["Manders"] = crossDot[(c1,c2)] / \
math.sqrt(sumSqrs[c1]*sumSqrs[c2])
theseCoeffs["Coloc(m)1"] = cca[c1][indicator[c2]].sum() / sums[c1]
theseCoeffs["Coloc(m)2"] = cca[c2][indicator[c1]].sum() / sums[c2]
theseCoeffs["Overlap(k)1"] = crossDot[(c1,c2)] / sumSqrs[c1]
theseCoeffs["Overlap(k)2"] = crossDot[(c1,c2)] / sumSqrs[c2]
result[(c1,c2)] = theseCoeffs
return result
def toyingCoeffs(ia, **auxmsrs):
# in flat form, 2nd dimension holds channel;
# cca -> color channel arrays
cca = {}
cca[R] = ia[:,R]
cca[G] = ia[:,G]
cca[B] = ia[:,B]
# mean gives float64 back
means = dict((c, mean(cca[c])) for c in colors)
indicator = dict((c, cca[c]>0) for c in colors)
indicatorSum = dict((c, indicator[c].sum(dtype=float_)) for c in colors)
selfSum = dict((c, cca[c].sum(dtype=float_)) for c in colors)
bigN = ia.shape[0]
# crossDot = {}
# for c1, c2 in ((R,G), (R,B), (G,B)):
# crossDot[(c1,c2)] = mydot(cca[c1], cca[c2])
results = {}
for c in colors:
myCoeffs = {}
myCoeffs["Mean"] = means[c]
myCoeffs["Sum"] = log10(selfSum[c])
myCoeffs["NumOn"] = log10(indicatorSum[c])
myCoeffs["SumToOn"] = selfSum[c] / indicatorSum[c]
myCoeffs["SumToBigN"] = selfSum[c] / bigN
myCoeffs["OnToBigN"] = indicatorSum[c] / bigN
myCoeffs["OnToMasterN"] = indicatorSum[c] / auxmsrs["masterN"]
results[c] = myCoeffs
return results
# good for products of uint8s: convert to 16s
def safedot(a,b):
return (a.astype(uint16) * b.astype(uint16)).sum().astype(float_)
# it appears that dot uses the incoming datatype as the accumulator
# type as well (unlike "arr.sum()" which will bump up to machine
# int size)
#def safedot(a,b):
# return dot(a.astype(uint16), b.astype(uint16)).astype(float_)
def cccOnFlatArray64bit(ia):
# in flat form, 2nd dimension holds channel;
# cca -> color channel arrays
cca = {}
cca[R] = ia[:,R] # should come in as uint8
cca[G] = ia[:,G]
cca[B] = ia[:,B]
#promoted = {}
#for c in colors:
# promoted[c] = uint16(cca[c]) # .astype(uint64)
# accumulating to a uint64 should be ok (approx 2^27 values with max
# value of 256=2^8 gives max sum of 2^35)
# mean() products a float64
#means = dict((c, mean(cca[c]) for c in colors)
meanDiffs = dict((c, cca[c]-mean(cca[c])) for c in colors)
sqMeanDiffs = dict((c, meanDiffs[c]**2) for c in colors)
sumSqMeanDiffs = dict((c, sqMeanDiffs[c].sum()) for c in colors)
sumSqrs = dict((c, safedot(cca[c], cca[c]))
for c in colors)
indicator = dict((c, cca[c]>0) for c in colors)
sums = dict((c, float_(cca[c].sum())) for c in colors)
crossDot = {}
for c1, c2 in ((R,G), (R,B), (G,B)):
crossDot[(c1,c2)] = safedot(cca[c1], cca[c2])
result = {}
for c1, c2 in ((R,G), (R,B), (G,B)):
theseCoeffs = {}
theseCoeffs["Pearson"] = dot(meanDiffs[c1],meanDiffs[c2]) / \
math.sqrt(dot(sumSqMeanDiffs[c1],
sumSqMeanDiffs[c2]))
theseCoeffs["Manders"] = crossDot[(c1,c2)] / \
math.sqrt(sumSqrs[c1]*sumSqrs[c2])
theseCoeffs["Coloc(m)1"] = cca[c1][indicator[c2]].sum() / sums[c1]
theseCoeffs["Coloc(m)2"] = cca[c2][indicator[c1]].sum() / sums[c2]
theseCoeffs["Overlap(k)1"] = crossDot[(c1,c2)] / sumSqrs[c1]
theseCoeffs["Overlap(k)2"] = crossDot[(c1,c2)] / sumSqrs[c2]
result[(c1,c2)] = theseCoeffs
return result
# def cccOnFlatZeroSensitive(ia):
# # in flat form, 2nd dimension holds channel;
# # cca -> color channel arrays
# cca = {}
# cca[R] = ia[:,R] # should come in as uint8
# cca[G] = ia[:,G]
# cca[B] = ia[:,B]
# meanDiffs = dict((c, cca[c]-mean(cca[c])) for c in colors)
# sqMeanDiffs = dict((c, meanDiffs[c]**2) for c in colors)
# sumSqMeanDiffs = dict((c, sqMeanDiffs[c].sum()) for c in colors)
# result = {}
# for c1, c2 in ((R,G), (R,B), (G,B)):
# theseCoeffs = {}
# theseCoeffs["Pearson"] = dot(meanDiffs[c1],meanDiffs[c2]) / \
# math.sqrt(dot(sumSqMeanDiffs[c1],
# sumSqMeanDiffs[c2]))
# result[(c1,c2)] = theseCoeffs
# return result
#
# safe for 64-bit; unsafe for 32-bit
#
# def cccOnFlatZeroInsensitive(ina):
# # in flat form, 2nd dimension holds channel;
# # cca -> color channel arrays
# cca = {}
# # should come in as uint8
# ia = ina[np_any(ina, 1)]
# cca[R] = ia[:,R]; cca[G] = ia[:,G]; cca[B] = ia[:,B]
# # accumulating to a uint64 should be ok (approx 2^27 values with max
# # value of 256=2^8 gives max sum of 2^35)
# sumSqrs = dict((c, safedot(cca[c], cca[c]))
# for c in colors)
# indicator = dict((c, cca[c]>0) for c in colors)
# sums = dict((c, float_(cca[c].sum())) for c in colors)
# crossDot = {}
# for c1, c2 in ((R,G), (R,B), (G,B)):
# crossDot[(c1,c2)] = safedot(cca[c1], cca[c2])
# result = {}
# for c1, c2 in ((R,G), (R,B), (G,B)):
# theseCoeffs = {}
# theseCoeffs["Manders"] = crossDot[(c1,c2)] / \
# math.sqrt(sumSqrs[c1]*sumSqrs[c2])
# theseCoeffs["Coloc(m)1"] = cca[c1][indicator[c2]].sum() / sums[c1]
# theseCoeffs["Coloc(m)2"] = cca[c2][indicator[c1]].sum() / sums[c2]
# theseCoeffs["Overlap(k)1"] = crossDot[(c1,c2)] / sumSqrs[c1]
# theseCoeffs["Overlap(k)2"] = crossDot[(c1,c2)] / sumSqrs[c2]
# result[(c1,c2)] = theseCoeffs
# return result