我有一个强度值从-3000到1000的数组,我已经对所有小于-100到-100的值进行了阈值处理,所有值都大于400到400,之后我将它转换为数据类型np.uint8 ,这使得数组中的所有值的强度值都为0到255,
我感到困惑的是,是否有一个我可以运行的操作,它将使数组从0-255强度范围到前一个-100到400?
任何建议都会有用,提前致谢
答案 0 :(得分:2)
要将[-3000,1000]的相当粗略的阈值([-100,400])作为uint8存储给定范围,首先需要将这些值映射到[0,255]范围之前将它们存储为uint8。我在这里选择了线性映射:
import numpy as np
def calculate_sum_of_errors(array_size):
# random intensity values ranging from -3000 to 1000
intensity_values = np.random.randint(-3000, 1000, array_size)
# threshold to range [-100, 400]
thresholded_values = np.clip(intensity_values, -100, 400)
# lineary map the range [-100, 400] to the range [0, 256] - we choose 256 here instead of
# 255 because only the outer range of our input array will produce this value, where as
# if we choose to use 255 we loose more precision because only the outer range (400)
# will map to 255, instead we need this to be the last 'set' of the range instead
mapped_values_best = np.array([(v + 100) / 500 * 256 for v in thresholded_values])
# after the mapping, threshold to the range [0, 255] and store as uint8
mapped_values = np.clip(mapped_values_best, 0, 255).astype('uint8')
# do the reverse mapping (first convert to float so we can hold the values)
restored_values = np.array([(v / 255 * 500 - 100) for v in mapped_values], 'int16')
return sum(abs(thresholded_values - restored_values))
# just to see the 'error' of our ways, print the sum of the errors divided by the array size
print([calculate_sum_of_errors(s) / s for s in np.linspace(1, 10000, 100, dtype='int')])
答案 1 :(得分:-2)
如果有效,请尝试array_name.encode("uint8").decode("uint8")
,然后您可以使用decode()
方法