我尝试用cv2.normalize函数规范化一组灰度图像,其均值和单位方差为零
out_image = np.zeros((32,32),dtype=np.float32)
out_array = np.zeros((len(X),32,32), dtype=np.uint8)
for imageindex in range(0,len(X)):
img = X[imageindex].squeeze()
if proctype == 'MeanSubtraction':
out_image = img.astype(np.float32) - np.mean(img.astype(np.float32))
elif proctype == 'Normalization':
out_image = cv2.normalize(img.astype(np.float32), out_image, alpha=-0.5, beta=0.5,\
norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
elif proctype == 'HistEqualization':
out_image = cv2.equalizeHist(img)
elif proctype == 'CLAHE':
clahe = cv2.createCLAHE(tileGridSize=(6,6),clipLimit = 20.0)
out_image = clahe.apply(img)
out_array[imageindex] = out_image.astype(np.uint8)
return out_array
但是,如果我使用0和1(或0和255)作为参数alpha和beta作为normalize函数,它就可以工作了。但是如果我使用-0.5和+0.5,它会返回一个空图像(全为零)
为什么会这样?在
out_array
的类型是np.uint8
,因此不能准确表示浮点值。因此,当您将包含范围[-0.5, 0.5]
中的浮点值的out_image
转换为使用out_image.astype(np.uint8)
的np.uint8
,所有这些值都将被截断为零。考虑这个例子:如果要在
out_array
中存储浮点值,首先需要将其dtype
更改为np.float32
。或者,可以将范围[-0.5, 0.5]
中的值重新规范化为[0, 255]
范围内的无符号整数。在相关问题 更多 >
编程相关推荐