-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconncomp.py
executable file
·73 lines (59 loc) · 2.33 KB
/
conncomp.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
#!/usr/bin/env python3
def main():
import numpy as np
import logging
import coloredlogs
import argparse
import tifffile as tiff
import os
from scipy.spatial.qhull import QhullError
from scipy import spatial
spatial.QhullError = QhullError
from skimage.morphology import label
from skimage.measure import regionprops
logger = logging.getLogger(__name__)
logging.basicConfig(format='[%(funcName)s] - %(asctime)s - %(message)s', level=logging.INFO)
coloredlogs.install(level='DEBUG', logger=logger)
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', help="image folder path", metavar='PATH')
parser.add_argument('-o', '--output', help="output base path", metavar='PATH')
parser.add_argument('-t', '--threshold', help="binarization threshold", type=float, default=0.01)
parser.add_argument('-b', '--batch', help="batch size", type=int, default=100)
args = parser.parse_args()
lista = os.listdir(args.input)
test = tiff.imread(os.path.join(args.input, lista[0]))
a = np.zeros((args.batch, test.shape[1], test.shape[2]))
limit = int(len(lista) / args.batch) * args.batch
rem = len(lista) % args.batch
aree = []
coords = []
for n in np.arange(0, limit, args.batch):
logger.info('processing batch #%d', n)
for m in np.arange(0, args.batch):
a[m, ...] = tiff.imread(os.path.join(args.input, lista[m+n]))
b = a > args.threshold
c = label(b)
props = regionprops(c)
for prop in props:
aree.append(prop.area)
centro = list(prop.centroid)
centro[0] += n
coords.append(tuple(centro))
a1 = np.zeros((rem, test.shape[1], test.shape[2]))
logger.info('processing last batch')
for m in np.arange(0, rem):
a1[m, ...] = tiff.imread(os.path.join(args.input, lista[limit + m]))
b1 = a1 > args.threshold
c1 = label(b1)
props = regionprops(c1)
for prop in props:
aree.append(prop.area)
centro = list(prop.centroid)
centro[0] += limit
coords.append(tuple(centro))
aree2 = np.array(aree)
coords2 = np.array(coords)
np.savetxt(os.path.join(args.output, 'areas.csv'), aree2)
np.savetxt(os.path.join(args.output, 'coordinates.csv'), coords2)
if __name__ == "__main__":
main()