| IOLink Python 1.11.0

Loading...
Searching...
No Matches
grayscaleSphere.py

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.

1 import iolink
2
3 import math
4 from array import array
5
6
9 def grayscaleSphere(shape: iolink.VectorXu64):
10
11 # first we can check if given shape has exactly 3 dimensions
12 if shape.size() != 3:
13 raise ValueError("Shape must have 3 dimensions ")
14
15 # To create a grayscale ImageView, the datatype of the image must only have 1 components
16 dt = iolink.DataTypeId.UINT8
17 image = iolink.ImageViewFactory.allocate(shape, dt)
18
19 # indicate the created image is a VOLUME with Grayscale color interpretation
20 image.axes_interpretation = iolink.AxesInterpretationId.VOLUME
21 image.image_interpretation = iolink.ImageInterpretation.GRAYSCALE
22
23 # determine the radius of the volume
24 # we use the smallest axis length
25 center = shape / 2
26 radius = min(*center)
27
28 # draw the sphere voxel by voxel
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
44image = grayscaleSphere(iolink.VectorXu64([100, 100, 100]))
45
46print("Generated image:", str(image))
47
48# Since it is not easy to visualize a volume, we can at least visualize a slice from it
49# Here we extract the 50th slice on Z axis to obtain a 2D image.
50slice = iolink.ImageViewFactory.extract_adjusted_region(image, iolink.RegionXu64((0, 0, 50), (100, 100, 1)))
51# we put this slice in memory to be able to export it as numpy
52slice = iolink.ImageViewFactory.copy_in_memory(slice)
53
54print("Slice extracted:", str(slice))
55
56
58
59#import numpy as np
60#from matplotlib import pyplot as plt
61
62
64
65#plt.imshow(npy, interpolation='nearest')
66#plt.show()
67
68print("SUCCESS")
grayscaleSphere
Definition grayscaleSphere.py:1