Bit-plane layering and reconstruction for image processing

1. Bit plane layering

Pixels are numbers made up of bits. For example, in a 256-level grayscale image, the grayscale of each pixel is composed of 8 bits (one byte). As shown in the figure below, an 8-bit image consists of eight 1-bit planes, where plane 1 contains the lowest-order bits of all pixels in the image, and plane 8 contains the highest-order bits of all pixels in the image.
Bit-plane representation of an 8-bit image

The principle of bit plane layering:

Let the image matrix be
[ 1 2 3 4 ] \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}[1324]
Convert the elements in the matrix to binary one by one, which can be written as:
[ 00000001 00000010 00000011 00000100 ] \begin{bmatrix} 00000001 & 00000010 \\ 00000011 & 00000100 \end{bmatrix}[00000001000000110000001000000100]
The 8 bit plane matrices of this image are:
the 8th bit layer image is:[ 0 0 0 0 ] \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}[0000]
The seventh bit layer image is:[ 0 0 0 0 ] \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}[0000]
The sixth bit layer image is:[ 0 0 0 0 ] \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}[0000]
The fifth bit layer image is:[ 0 0 0 0 ] \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}[0000]
The fourth bit layer image is:[ 0 0 0 0 ] \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}[0000]
The third bit layer image is:[ 0 0 0 1 ] \begin{bmatrix} 0 & 0 \\ 0 & 1 \end{bmatrix}[0001]
The second bit layer image is:[ 0 1 1 0 ] \begin{bmatrix} 0 &1 \\ 1 & 0 \end{bmatrix}[0110]
The first bit layer image is:[ 1 0 1 0 ] \begin{bmatrix} 1 & 0 \\ 1 & 0 \end{bmatrix}[1100]

Code

#分层处理
def  getBitlayer(img):
    h, w = img.shape[0], img.shape[1]
    new_img = np.zeros((h, w, 8))
    for i in range(h):
        for j in range(w):
            n = str(np.binary_repr(img[i, j], 8))
            for k in range(8):
                new_img[i, j, k] = n[k]
    return new_img

Hierarchical visualization

## 通过plt子图形式显示每层bit图
def showBitlayer(new_img):
    # 调整图像大小 实际大小为w*100,h*100 pixel
    plt.rcParams['figure.figsize'] = (10, 3.6)
    fig, a = plt.subplots(2, 4)
    m = 0
    n = 8

    for i in range(2):
        for j in range(4):
            plt.axis('off')
            a[i][j].set_title('Bit plane ' + str(n))
            a[i][j].imshow(new_img[:, :, m], cmap=plt.cm.gray)
            m += 1
            n -= 1
    fig.tight_layout()  # 调整整体空白
    plt.subplots_adjust(wspace=0.5, hspace=-0.2)  # 调整子图间距
    plt.savefig('bitLayer.jpg')
    plt.show()

code result

The original image is:

insert image description here

Bit-plane layered results:
insert image description here

2. Image reconstruction

Refactoring principle

When the image is layered, the data of the nth layer image comes from the nth bit of the binary pixel value. If it is converted back to decimal, its value is
r ∗ 2 ( n − 1 ) r*2^{(n-1) }r2( n 1 ) , then using certain layers of bit layers for reconstruction, convert the pixel values ​​of these layers back to decimal and add them up.

Original image reconstruction code

def rebuildImg(bitImags, build_list, img):
    h, w = img.shape[0], img.shape[1]
    new_img = np.zeros((h, w))
    for i in range(h):
        for j in range(w):
            for m in build_list:
                new_img[i, j] += bitImags[i, j, 7-m] * (2 ** (m))
    return new_img

Refactor image visualization

def showRebuildimgimg(rebuildImag,img):
    plt.figure(figsize=(100, 20))
    plt.subplot(121)
    plt.axis('off')
    plt.imshow(rebuildImag, cmap='gray')
    plt.subplot(122)
    plt.axis('off')
    plt.imshow(img, cmap='gray')
    plt.savefig('rebuiltImag.jpg')
    plt.show()

The reconstructed image results are as follows:
insert image description here
the reconstruction results of layers 6, 7 and 8 are on the left, and the original image is on the right.

overall code

import cv2
import numpy as np
import matplotlib.pyplot as plt


#分层处理过程
def  getBitlayer(img):
    h, w = img.shape[0], img.shape[1]
    new_img = np.zeros((h, w, 8))
    for i in range(h):
        for j in range(w):
            n = str(np.binary_repr(img[i, j], 8))
            for k in range(8):
                new_img[i, j, k] = n[k]
    return new_img


## 通过plt子图形式显示每层bit图
def showBitlayer(new_img):
    # 调整图像大小 实际大小为w*100,h*100 pixel
    plt.rcParams['figure.figsize'] = (10, 3.6)
    fig, a = plt.subplots(2, 4)
    m = 0
    n = 8

    for i in range(2):
        for j in range(4):
            plt.axis('off')
            a[i][j].set_title('Bit plane ' + str(n))
            a[i][j].imshow(new_img[:, :, m], cmap=plt.cm.gray)
            m += 1
            n -= 1
    fig.tight_layout()  # 调整整体空白
    plt.subplots_adjust(wspace=0.5, hspace=-0.2)  # 调整子图间距
    plt.savefig('bitLayer.jpg')
    plt.show()


## bite图像重构
def rebuildImg(bitImags, build_list, img):
    h, w = img.shape[0], img.shape[1]
    new_img = np.zeros((h, w))
    for i in range(h):
        for j in range(w):
            for m in build_list:
                new_img[i, j] += bitImags[i, j, 7-m] * (2 ** (m))
    return new_img

def showRebuildimgimg(rebuildImag,img):
    plt.figure(figsize=(100, 20))
    plt.subplot(121)
    plt.axis('off')
    plt.imshow(rebuildImag, cmap='gray')
    plt.subplot(122)
    plt.axis('off')
    plt.imshow(img, cmap='gray')
    plt.savefig('rebuiltImag.jpg')
    plt.show()

if __name__ == '__main__':
    img = cv2.imread(r'dollars.tif', 0)
    bit_imgs=getBitlayer(img)
    showBitlayer(bit_imgs)
    rebuildImg=rebuildImg(bit_imgs,[5,6,7],img)
    showRebuildimgimg(rebuildImg,img)


dollars.tifClick here to download.

reference blog

https://blog.csdn.net/qq_41398808/article/details/103109111?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-103109111-blog-86769995.235%5Ev38%5Epc_relevant_sort_base2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-103109111-blog-86769995.235%5Ev38%5Epc_relevant_sort_base2&utm_relevant_index=11
https://blog.csdn.net/qq_42505705/article/details/86769995

Guess you like

Origin blog.csdn.net/weixin_42491648/article/details/131617869