-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
138 lines (119 loc) · 5.04 KB
/
main.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
from datetime import datetime
import numpy as np
from scipy.ndimage import rotate
from tifffile import imread
from labeling.Labeling import Labeling
#from line_profiler import LineProfiler
result = np.zeros((512, 512))
resolution = (512, 512)
def read_img():
files = []
for i in range(1, 6):
for j in range(1, 5):
files.append("C:/mpicbg/metaseg_data/Euclidean_squared_distance/000/diverse" + str(i) + "/sweep_" + str(
j) + "_mask.tif")
images = imread(files)
return images
def test1():
# setting up the images for second example
example2_images = [np.invert(imread("tutorial/up_big.tif"))]
example2_images[0][example2_images[0] > 0] = 130
example2_images.append(
rotate(np.transpose(np.flip(example2_images[0]).copy()), angle=45, reshape=False, mode="constant", cval=0))
example2_images[1][example2_images[1] > 0] = 131
example2_images.append(np.transpose(np.flip(example2_images[0]).copy()))
example2_images[2][example2_images[2] > 0] = 132
example2_images.append(rotate(np.flip(example2_images[0]).copy(), angle=45, reshape=False, mode="constant", cval=0))
example2_images[3][example2_images[3] > 0] = 133
example2_images.append(np.flip(example2_images[0]).copy())
example2_images[4][example2_images[4] > 0] = 134
example2_images.append(
rotate(np.transpose(example2_images[0]).copy(), angle=45, reshape=False, mode="constant", cval=0))
example2_images[5][example2_images[5] > 0] = 135
example2_images.append(np.transpose(example2_images[0]).copy())
example2_images[6][example2_images[6] > 0] = 136
example2_images.append(rotate(example2_images[0], angle=45, reshape=False, mode="constant", cval=0))
example2_images[7][example2_images[7] > 0] = 137
print(np.argwhere( example2_images[0]))
merger = Labeling.fromValues(np.zeros((512, 512), np.int32))
merger.iterate_over_images(example2_images, [str(int) for int in list(range(1, len(example2_images) + 1))])
meta = {
"date": "2021-06-28",
"revision": 1,
"author": "Tom Burke"
}
#merger.add_metadata(meta)
print(merger.metadata)
img, labeling2 = merger.save_result("example2")
print(merger.metadata)
print(labeling2.metadata)
def test4():
global result, img
a = np.zeros((4, 4), np.int32)
a[:2] = 1
example1_images = []
example1_images.append(a)
#example1_images.append(a.copy())
b = a.copy()
example1_images.append(np.flip(b.transpose()))
c = a.copy()
example1_images.append(np.flip(c))
d = a.copy()
example1_images.append(d.transpose())
e = np.zeros((4, 4), np.int32)
e[1:3, 1:3] = 1
example1_images.append(e)
# Initialize the merger with the first image. This can also be an empty image of zeros in the correct shape
merger = Labeling.fromValues(first_image=np.zeros((4,4), dtype="int32"))
merger2 = Labeling.fromValues(first_image=np.zeros((4, 4), dtype="int32"))
result = merger2.add_segments(example1_images[0], (0, 0), source_id=str(0))
result = merger2.add_segments(example1_images[1], (0, 0), source_id=str(1))
result = merger2.add_segments(example1_images[2], (0, 0), source_id=str(2))
result = merger2.add_segments(example1_images[3], (0, 0), source_id=str(3))
result = merger2.add_segments(example1_images[4], (0, 0), source_id=str(4))
merger.iterate_over_images(example1_images,['a','b','c','d','e'])
img, labeling = merger.save_result("example1_1", True)
img, labeling = merger2.save_result("example1", True)
print(img)
print(vars(labeling))
# add_anything(data, (x,y,z):Tuple, merge:dict=None)
# returns dict of actual labels dif
def test3():
# init
images = read_img()
patch_size = 64
start = datetime.now()
# initialize the merger
merger = Labeling(images[0].shape, np.int32)
# iterate over all images
i = 0
for image in images:
merger.add_segments(patch=image, position=(0, 0), source_id=str(i))
i += 1
img, labeling = merger.save_result("big_img", True)
print(datetime.now() - start)
#print(vars(labeling))
# start = datetime.now()
# # initialize the merger
# merger = lb.Labeling(images[0].shape, np.int32)
# # iterate over all images
# i = 0
# for image in images:
# merger.add_segments2(patch=image, position=(0, 0), source_id=str(i))
# i += 1
# img, labeling = merger.save_result("big_img", True)
# print(datetime.now() - start)
#print(vars(labeling))
if __name__ == '__main__':
#profiler = LineProfiler()
#profiler.add_function(lb.Labeling.add_segments)
#profiler.add_function(lb.Labeling.iterate_over_images)
#profiler.add_function(lb.Labeling.save_result)
#profiler.add_function(lb.Labeling.add_image)
#test1()
#profiler.runcall(test1)
test1()
#profiler.runcall(test3)
test4()
#profiler.print_stats()
#profiler.dump_stats("profiling.lprof")