-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmult.py
More file actions
151 lines (125 loc) · 3.53 KB
/
mult.py
File metadata and controls
151 lines (125 loc) · 3.53 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
#!/usr/bin/env python
from glob import glob
from os import makedirs, getcwd, remove
from shutil import rmtree
from subprocess import call
import sys
import zipfile
from readroi import salva_img_crop_rois
curdir = getcwd()
imagejdir = 'your_ImageJ_dir'
roi_count = 0
# monta a macro
try:
limiar = int(sys.argv[1])
str_limiar = 'setThreshold(0, %d);\n' % limiar
except:
#limiar =112
str_limiar = ''
macro = """run("Image Sequence...", "open=[%s/tmp/] increment=1 scale=100 file=[] or=[] sort");
run("8-bit");
"""
macro = macro + str_limiar
macro = macro + """run("Make Binary", "method=Default background=Default thresholded remaining black");
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing display include record add in_situ stack");
saveAs("Results", "Results.xls");
roiManager("Save", "RoiSet.zip");
run("Quit");
"""
def try_int(s):
"Convert to integer if possible."
try: return int(s)
except: return s
def natsort_key(s):
"Used internally to get a tuple by which s is sorted."
import re
return map(try_int, re.findall(r'(\d+|\D+)', s))
def natcmp(a, b):
"Natural string comparison, case sensitive."
return cmp(natsort_key(a), natsort_key(b))
def natcasecmp(a, b):
"Natural string comparison, ignores case."
return natcmp(a.lower(), b.lower())
def natsort(seq, cmp=natcmp):
"In-place natural string sort."
seq.sort(cmp)
def natsorted(seq, cmp=natcmp):
"Returns a copy of seq, sorted by natural string sort."
import copy
temp = copy.copy(seq)
natsort(temp, cmp)
return temp
def executaImageJ(chunk):
global roi_count
# move arquivos para dir temp.
for arq in chunk:
f1 = open(arq, 'r')
f2 = open('tmp/'+arq, 'w')
f2.write(f1.read())
f2.close()
f1.close()
# salva a macro
cmd = macro % (curdir)
fm = open('macro.ijm', 'w')
fm.write(cmd)
fm.close()
# executa ImageJ
sr = ['']
call([imagejdir, '-macro', curdir+'/macro.ijm'])
fr = open('Results.xls', 'r')
sr = fr.readlines()
fr.close()
zf = zipfile.ZipFile('RoiSet.zip', 'r')
# ajusta rois
rois = []
for filename in zf.namelist():
roi_count +=1
campos = filename.split('-')
imagem = chunk[int(campos[0])-1]
data = zf.read(filename)
str_roi = '%s-%04d.roi' % (imagem, roi_count)
rois.append(str_roi)
froi = open('rois/'+str_roi, 'w')
froi.write(data)
froi.close()
#ajusta resultados
for numero_linha, linha in enumerate(sr[1:]):
campos = linha.split('\t')
ind = int(campos[0])-1
campos[0] = rois[ind]
ind = int(campos[27])-1
campos[27] = chunk[ind]
sr[numero_linha+1] = '\t'.join(campos)
return ''.join(sr[1:])
arquivos = glob('*.jpg')
arquivos = natsorted(arquivos)
results = """ Area Mean StdDev Mode Min Max X Y XM YM Perim. BX BY Width Height Major Minor Angle Circ. Feret
"""
try:
rmtree('rois')
except:
pass
makedirs('rois')
try:
rmtree('tmp')
except:
pass
makedirs('tmp')
for chunk in [arquivos[n:n+50] for n in range(0,len(arquivos),50)]:
#executa imageJ
results = results + executaImageJ(chunk)
rmtree('tmp')
makedirs('tmp')
fr = open('Results.xls', 'w')
fr.write(results)
fr.close()
rmtree('tmp')
remove('macro.ijm')
remove('RoiSet.zip')
try:
rmtree('roiimgs')
except:
pass
makedirs('roiimgs')
for image in glob('*.jpg'):
salva_img_crop_rois(image)