IOLink Python 1.11.0
Loading...
Searching...
No Matches
volumeFromStack.py

This code demonstrates how to create a volume from a series of 2D-images given in user-memory.

This code demonstrates how to create a volume from a series of 2D-images given in user-memory. Many 2D images are in user-memory and represent a volume if stacked together. This code shows how to create ImageView from these 2D images, and then create a Volume from these ImageViews.

1import iolink
2from array import array
3
4
8
9# here we have arrays of values giving the data of each slice used to represent a volume
10slice_0 = array('B', [
11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
12 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
13 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
14 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
15 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
16 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
17 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
18 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
19 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
20 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
21])
22slice_1 = array('B', [
23 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
24 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
25 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
26 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
27 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
28 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
29 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
30 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
31 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
32 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
33])
34slice_2 = array('B', [
35 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
36 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
37 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
38 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
39 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
40 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
41 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
42 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
43 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
44 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
45])
46slice_3 = array('B', [
47 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
48 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
49 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
50 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
52 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
53 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
54 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
55 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
56 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
57])
58slice_4 = array('B', [
59 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
60 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
61 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
62 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
63 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
64 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
65 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
66 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
67 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
68 13, 14, 15, 16, 17, 18, 19, 20, 21, 22
69])
70slice_5 = array('B', [
71 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
72 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
73 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
74 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
75 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
76 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
77 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
78 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
79 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
80 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
81])
82slice_6 = array('B', [
83 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
84 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
85 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
86 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
87 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
88 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
89 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
90 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
91 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
92 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
93])
94slice_7 = array('B', [
95 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
96 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
97 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
98 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
99 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
100 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
101 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
102 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
103 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
104 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
105])
106slice_8 = array('B', [
107 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
108 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
109 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
110 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
111 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
112 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
113 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
114 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
115 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
116 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
117])
118slice_9 = array('B', [
119 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
120 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
121 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
122 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
123 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
124 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
125 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
126 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
127 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
128 18, 19, 20, 21, 22, 23, 24, 25, 26, 27
129])
130
131listSlices = [slice_0, slice_1, slice_2, slice_3, slice_4, slice_5, slice_6, slice_7, slice_8, slice_9]
132
133# create a ImageView for the slice whose data, shape and datatype are given in parameters.
134def create_slice_from_raw_data(sliceData, sliceShape: iolink.VectorXu64, dtype: iolink.DataType) -> iolink.ImageView:
135 fullRegionSlice = iolink.RegionXu64.create_full_region(sliceShape)
136
137 # create an image view
138 slice = iolink.ImageViewFactory.allocate(sliceShape, dtype)
139
140 # write data
141 slice.write_region(fullRegionSlice, sliceData)
142
143 # update properties of the slice
144 slice.has_alpha = False
145 slice.axes_interpretation = iolink.AxesInterpretationId.IMAGE
146 slice.image_interpretation = iolink.ImageInterpretation.GRAYSCALE
147
148 return slice
149
150# create the volume from slices
151def create_volume_from_slices(slices) -> iolink.ImageView :
152 # create a MultiImageView from all ImageViews
153 sliceContainer = iolink.MultiImageViewFactory.create()
154
155 shapeSlice = iolink.VectorXu64(10, 10)
156 dtype = iolink.DataTypeId.UINT8
157
158 # iterate over all slices
159 for slice in slices:
160 # create an ImageView from the slice data
161 new_slice = create_slice_from_raw_data(slice, shapeSlice, dtype)
162
163 # add the slice to the MultiImageView
164 sliceContainer.add_frame(new_slice)
165
166 # create the volume by indicating the new dimension as SLICE
167 return iolink.ImageViewFactory.stack(sliceContainer, iolink.ImageDimension.SLICE)
168
169
170# create the volume using the list of slices in user-memory
171volume = create_volume_from_slices(listSlices)
172
173print(volume)
174print(volume.properties)