numpy convert 16bit data to 8bit data
To convert 16-bit data to 8-bit data in numpy, you can use the following code:
import numpy as np
# assume you have 16-bit data stored in a numpy array called 'data'
# normalize the data to the range [0, 255]
data_norm = (data - np.min(data)) * 255 / (np.max(data) - np.min(data))
# convert the normalized data to 8-bit integers
data_8bit = data_norm.astype(np.uint8)
In this code, we first normalize the 16-bit data to the range [0, 255] using the formula (data - min(data)) * 255 / (max(data) - min(data)).
Then, we convert the normalized data to 8-bit integers using the astype method of numpy arrays, specifying the np.uint8 data type. This will truncate any decimal values and ensure that the resulting array contains only integers between 0 and 255.
原文地址: http://www.cveoy.top/t/topic/btzN 著作权归作者所有。请勿转载和采集!