Bit-plane layered (basic gradation conversion function) to achieve the basic principles and Python

1. Basic principles

In the grayscale image, the pixel values of the range [0, 255], i.e., a total of 256 gray levels. In the computer, we use eight bits to represent each pixel value. It is possible to extract different bit levels of grayscale. Bit-level stratification can be used for picture compression : only a bit higher storage layer (Why use a higher level, rather than the lower layer by binary conversion, we know that a greater contribution to a higher level in the value of?); Such as the use of the high four bits layer represents the original eight bit-planes.

2. Test results

FIG derived skimage

3. Code

 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

 

Guess you like

Origin www.cnblogs.com/iwuqing/p/11297280.html