-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreProcess.py
executable file
·50 lines (40 loc) · 1.35 KB
/
PreProcess.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
from numpy import unique, bincount, shape, min, max, sum, array, uint32, where, uint8, round
import nrrd
import sys
import os
from matplotlib.pyplot import imshow, show, figure
def AutoBalance(data, threshold=0.00035, background=0):
bins = unique(data)
binc = bincount(data.flat)
histogram = binc[binc > 0]
del binc
if background in bins:
i = where(bins == background)
v = bins[i][0]
c = histogram[i][0]
th = int(((sum(histogram) - histogram[i][0]) / shape(data)[2]) * threshold)
else:
th = int((sum(histogram) / shape(data)[2]) * threshold)
m = min(bins)
M = max(bins)
for x in range(1, shape(bins)[0] - 1):
if sum(histogram[:x]) > th:
m = x - 1
break
for x in range(shape(bins)[0] - 1, 0, -1):
if sum(histogram[x:]) > th:
M = x
break
data[data > M] = M
data[data < m] = m
dataA = round((data - m) * (255.0 / (M - m)))
return (dataA, array([m, M], dtype=uint32), array([bins, histogram], dtype=uint32))
if len(sys.argv) < 2:
print('e.g. python PreProcess.py image.nrrd output.nrrd')
else:
fileName, fileExtension = os.path.splitext(sys.argv[1])
if fileExtension == '.nrrd':
data1, header1 = nrrd.read(sys.argv[1])
elif fileExtension == '.lsm':
# TBA using pylsm
pass