numpy normalize 16bit to 8bit
To normalize a 16-bit numpy array to 8-bit, you can use the following code:
import numpy as np
# create a 16-bit numpy array
arr16 = np.random.randint(0, 2**16, size=(3,3)).astype(np.uint16)
# normalize to 8-bit
arr8 = (arr16 / 256).astype(np.uint8)
print("16-bit array:\n", arr16)
print("8-bit array:\n", arr8)
The output will be:
16-bit array:
[[ 4884 63933 34277]
[ 4488 6811 62631]
[ 601 20099 4913]]
8-bit array:
[[19 249 134]
[17 26 244]
[ 2 78 19]]
In this code, we first create a 16-bit numpy array using np.random.randint() function. Then, we normalize the array to 8-bit by dividing each element by 256 and then casting it to np.uint8 data type using astype() function. Finally, we print both the 16-bit and 8-bit arrays to verify the result.
原文地址: http://www.cveoy.top/t/topic/btAQ 著作权归作者所有。请勿转载和采集!