This code demonstrates how to generate a grayscale sphere in a 3D volume.
This code demonstrates how to generate a grayscale sphere in a 3D volume. All voxels inside the sphere have value = 255, and others are 0.
1import iolink
2
3import math
4from array import array
5
6
10
11
12 if shape.size() != 3:
13 raise ValueError("Shape must have 3 dimensions ")
14
15
16 dt = iolink.DataTypeId.UINT8
17 image = iolink.ImageViewFactory.allocate(shape, dt)
18
19
20 image.axes_interpretation = iolink.AxesInterpretationId.VOLUME
21 image.image_interpretation = iolink.ImageInterpretation.GRAYSCALE
22
23
24
25 center = shape / 2
26 radius = min(*center)
27
28
29 for i in range(0, shape[0]):
30 print("line {}".format(i))
31 for j in range(0, shape[1]):
32 for k in range(0, shape[2]):
33 voxPos = iolink.Vector3d(i, j, k)
34 voxPosInt = iolink.VectorXu64(i, j, k)
35 delta = voxPos - center
36 if delta.length() > float(radius):
37 image.write(voxPosInt, 0)
38 else:
39 image.write(voxPosInt, 255)
40
41 return image
42
43
45
46print("Generated image:", str(image))
47
48
49
50slice = iolink.ImageViewFactory.extract_adjusted_region(image, iolink.RegionXu64((0, 0, 50), (100, 100, 1)))
51
52slice = iolink.ImageViewFactory.copy_in_memory(slice)
53
54print("Slice extracted:", str(slice))
55
56
58
59
60
61
62
64
65
66
67
68print("SUCCESS")
Definition grayscaleSphere.py:1