比特平面分层(一些基本的灰度变换函数)基本原理及Python实现
作者:互联网
1. 基本原理
在灰度图中,像素值的范围为[0, 255]
,即共有256级灰度。在计算机中,我们使用8比特数来表示每一个像素值。因此可以提取出不同比特层面的灰度图。比特层面分层可用于图片压缩:只储存较高比特层(为什么使用较高层,而不是较低层?通过二进制转换,我们知道较高层在数值中的贡献更大);如使用高四位比特层表示原有的八层比特平面。
2. 测试结果
图源自skimage
3. 代码
1 def extract_bit_layer(input_image, layer_num): 2 ''' 3 提取比特层 4 :param input_image: 原图像 5 :param layer_num: 提取层 6 :return: 提取到的比特层 7 ''' 8 input_image_cp = np.copy(input_image) # 输入图片的副本 9 10 if layer_num == 1: 11 input_image_cp = np.where((input_image_cp >= 0) & (input_image_cp < 2), 255, 0) 12 elif layer_num == 2: 13 input_image_cp = np.where((input_image_cp >= 2) & (input_image_cp < 4), 255, 0) 14 elif layer_num == 3: 15 input_image_cp = np.where((input_image_cp >= 4) & (input_image_cp < 8), 255, 0) 16 elif layer_num == 4: 17 input_image_cp = np.where((input_image_cp >= 8) & (input_image_cp < 16), 255, 0) 18 elif layer_num == 5: 19 input_image_cp = np.where((input_image_cp >= 16) & (input_image_cp < 32), 255, 0) 20 elif layer_num == 6: 21 input_image_cp = np.where((input_image_cp >= 32) & (input_image_cp < 64), 255, 0) 22 elif layer_num == 7: 23 input_image_cp = np.where((input_image_cp >= 64) & (input_image_cp < 128), 255, 0) 24 elif layer_num == 8: 25 input_image_cp = np.where((input_image_cp >= 128) & (input_image_cp < 256), 255, 0) 26 else: 27 print("please enter the number of bit layers from 1 to 8") 28 29 output_image = input_image_cp 30 31 return output_image
标签:layer,比特,灰度变换,image,Python,num,input,cp,255 来源: https://www.cnblogs.com/iwuqing/p/11297280.html