-
Notifications
You must be signed in to change notification settings - Fork 1
/
coordinates_to_image.py
executable file
·48 lines (37 loc) · 1.17 KB
/
coordinates_to_image.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
#!/usr/bin/env python3
from collections import defaultdict
from skimage import measure, io
import matplotlib.pyplot as plt
import numpy
import os
import sys
width = 839
height = 396
for filename in sys.argv[1:]:
name = os.path.splitext( os.path.basename(filename) )[0]
image = numpy.zeros( (height,width) )
with open(filename) as f:
for line in f:
(x,y) = [ int(a) for a in line.split()[0:2] ]
image[y][x] = 1
# Store image
io.imsave( "/tmp/" + name + ".png", image )
labels = measure.label( image, background=0 )
nrows, ncols = labels.shape
components = defaultdict(list)
#
# Separate connected components
#
for y in range(nrows):
for x in range(ncols):
component = labels[y][x]
if component > 0:
components[component].append( (x,y) )
#
# Stored connected components
#
with open("/tmp/" + name + "_components.txt", "w") as f:
for component in sorted( components.keys() ):
for (x,y) in components[component]:
print("%d\t%d" % (x,y), file=f)
print("\n", file=f)