Bit plane layering of images

  Bit-Plane Slicing is a technique commonly used in image processing that involves splitting the pixel value representation of an image into different binary bit planes. These planes can represent different features or information in the image respectively, thus providing useful tools in image analysis, enhancement and compression. The following is an explanation of the mathematical principles, meaning and usage scenarios of bit plane layering:
Mathematical principle:
  For a grayscale image, the pixel value of each pixel can be represented by an 8-bit binary number (usually in the range of 0 to 255 ). Bit plane layering is to split these 8-bit binary numbers into 8 independent planes, each plane corresponding to a specific binary bit. Each bit plane only contains information about that bit of the corresponding pixel in the image, and other bits are set to 0. This can be expressed as the following mathematical formula:
  For a pixel value ppp , its 8-bit binary representation isp = b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 p = b_7b_6b_5b_4b_3b_2b_1b_0p=b7b6b5b4b3b2b1b0, where b 7 b_7b7Represents the highest bit, b 0 b_0b0Indicates the lowest bit. Then iii bit plane (i = 0 , 1 , … , 7 i = 0, 1, \ldots, 7i=0,1,,7 ) can be expressed as:
P i (x, y) = bi P_i(x, y) = b_iPi(x,y)=bi
Here (x, y) (x, y)(x,y ) are the pixel coordinates in the image.

Significance:
  The main significance of bit plane layering is to decompose the information of the image so that we can observe and manipulate different details and features in the image. High bit plane ( b 7 b_7b7to b 4 b_4b4) usually contains the global structure and main features of the image, while the low-bit plane ( b 3 b_3b3to b 0 b_0b0) contains finer details. This makes bit plane layering the basis for several operations:

  1. Image enhancement: By enhancing specific bit planes, certain details in an image can be highlighted or suppressed, thereby improving the visual quality of the image.
  2. Image compression: In some cases, discarding lower-bit information can achieve image compression and reduce storage and transmission overhead.
  3. Image Analysis: By looking at different bit planes, you can better understand the structure, texture, and features in an image.

Usage scenarios:
  Bit plane layering is useful in a variety of image processing scenarios, including:

  • Image enhancement: By enhancing high-level planes, the main features in the image can be highlighted and the visualization effect of the image can be improved.
  • Image compression: By discarding the low-bit plane, lossless or lossy image compression can be achieved, reducing storage and transmission overhead.
  • Image Analysis: Analyzing bit planes can help identify features in images such as texture, shape, and edges.
  • Steganography: In steganography, low-level planes can be used to hide secret information without affecting the appearance of the image.
      In summary, bitplane layering is a useful image processing tool that can be used for multiple purposes in different fields, from image enhancement to analyzing image features.
      The code implementation process is as follows:
import cv2                     # 导入OpenCV库用于图像处理
import matplotlib.pyplot as plt  # 导入matplotlib库用于绘图
import numpy as np              # 导入NumPy库用于数值操作

class BIT:
    def __init__(self, input_path):
        self.input_path = input_path  # 初始化类,传入输入图像路径

    def fenceng(self):
        img_gray = cv2.imread(self.input_path, flags=0)  # 以灰度方式读取输入图像
        if img_gray is None:
            print('Unable to load image!')
        else:
            print('Load image successfully!')

        height, width = img_gray.shape[:2]  # 获取图像的高度和宽度

        plt.figure(figsize=(10, 8))  # 设置绘图的图像大小
        for i in range(9, 0, -1):   # 循环从9到1(倒序)
            plt.subplot(3, 3, (9 - i) + 1, xticks=[], yticks=[])  # 在3x3的网格中创建子图

            if i == 9:  # 对于第一个子图(i = 9),显示原始灰度图像
                plt.imshow(img_gray, cmap='gray')
                plt.title('Original')
            else:
                img_bit = np.empty((height, width), dtype=np.uint8)  # 创建一个空的图像数组

                for w in range(width):
                    for h in range(height):
                        x = np.binary_repr(img_gray[h, w], width=8)  # 将像素值转换为8位二进制字符串
                        x = x[::-1]  # 反转二进制字符串
                        a = x[i - 1]  # 从反转的二进制字符串中获取第(9-i)位的值
                        img_bit[h, w] = int(a)  # 将像素值设置为所选位的值

                plt.imshow(img_bit, cmap='gray')  # 显示带有所选位平面的图像
                plt.title(f"{bin((i - 1))}")  # 将标题设置为(i - 1)的二进制表示

        plt.show()  # 显示包含所有子图的完整图像

# 输入图像文件的路径
imgfile = "./Images/stuff.jpg"
bit = BIT(imgfile)  # 使用输入图像路径创建BIT类的实例
bit.fenceng()  # 调用fenceng方法执行位平面分割并可视化操作

Guess you like

Origin blog.csdn.net/qq_50993557/article/details/132154063
Bit
BIT