| IOLink Python 1.11.0

Loading...
Searching...
No Matches
gradientFloatToInt.py

This code demonstrates how to adapt a float ImageView into a 16-bit unsigned integer ImageView.

This code demonstrates how to adapt a float ImageView into a 16-bit unsigned integer ImageView. Data are converted on the fly, and the resulting image has values between 0 and 65535. Both images are displayed to show their content.

1 import iolink
2
3 import numpy as np
4
5
9
10
11# Create a a vertical gradient into a float ImageView
12# using the given input range [min, max].
13 def gradientFloatImage(inputRange: iolink.Vector2f):
14 shape = iolink.VectorXu64(5, 30)
15
16 dt = iolink.DataTypeId.FLOAT
17 image = iolink.ImageViewFactory.allocate(shape, dt)
18
19 image.axes_interpretation = iolink.AxesInterpretationId.IMAGE
20
21 stepLine = (inputRange[1] - inputRange[0]) / shape[1]
22
23 lineSize = iolink.VectorXu64(shape[0], 1)
24 valueInit = inputRange[0]
25 valueToSet = valueInit
26 for j in range(0, shape[1]):
27 lineRegion = iolink.RegionXu64((0, j), lineSize)
28 # create a buffer corresponding to the line size, and
29 # initialize it to the same value
30 lineBuffer = np.full((shape[0], 1), valueToSet, dtype=np.float32)
31 image.write_region(lineRegion, lineBuffer)
32 valueToSet = valueInit + j * stepLine
33
34 return image
35
36data_range = iolink.Vector2f(0.0, 5.0)
37
38floatImage = gradientFloatImage(data_range)
39
40# convert the float ImageView to numpy
41floatNpy = iolink.NumpyInterop.to_numpy_array(floatImage)
42
43# print min/max values
44print("Float image min/max:", np.amin(floatNpy), np.amax(floatNpy))
45
46# convert the float image to a 16-bit integer image
47# an adapter is added to the float image to convert the values on the fly
48# data are not duplicated
49intImage = iolink.ImageViewFactory.adapt_dynamic_range(floatImage, iolink.DataTypeId.UINT16, data_range, iolink.Vector2d(0, 65535))
50
51# imageView must be in-memory to be converted into numpy
52# An in-memory ImageView which has been adapted is not considered as is-memory anymore
53intImage = iolink.ImageViewFactory.copy_in_memory(intImage)
54
55# convert the Integer ImageView to numpy
56intNpy = iolink.NumpyInterop.to_numpy_array(intImage)
57
58# print min/max values
59print("Integer image min/max:", np.amin(intNpy), np.amax(intNpy))
60
61print("SUCCESS")
gradientFloatImage
Definition gradientFloatImage.py:1