@TOC

What is bitplane decomposition

A binary image is obtained by combining the binary pixel values in the uniform bit in the gray image, which is called a bit plane of the gray image, and this process is called bit plane decomposition. For example, a “least significant bit” bitplane can be formed by combining the values of the lowest bits in the binary of all pixels in a grayscale image.

In an 8-bit grayscale image, each pixel is represented by an 8-bit binary, and its value ranges from [0,255], in bits from low to high:

00000001 00000010 00000100 0000100000010000 00100000 01000000 10000000

After taking these values, we can obtain all bit-plane decompositions by bitwise and operation. As for the use? A smart friend would know. For example, if we need to add a watermark to an image, is it better to add it in the high plane or the least significant plane?

Of course, it is the low level plane, because it has the least information, after adding the watermark, it is easier to remove the watermark.

Grayscale bitwise plane decomposition

Now that we know about gray-level bitwise plane decomposition and all that we know about it. Next, let’s take a drawing image and extract all its bitmaps.

The specific code is as follows:

import cv2
import numpy as np

a = cv2.imread("4.jpg".0)
r, c = a.shape
b = np.zeros((r, c, 8), dtype=np.uint8)
for i in range(8):
    b[:, :, i] = 2 ** i
for i in range(8):
    temp = cv2.bitwise_and(a, b[:, :, i])
    cv2.imshow(str(i),temp)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, we get the 8-bit plan of gray image:

The original image looks like this:

Color bitwise plane decomposition

Since gray image has its bit plane, color image can also be extracted. However, since the matrix of color image is a 3-dimensional matrix, we need to carry out bitwise and operation for each color value of RGB.

The specific code is as follows:

import cv2
import numpy as np

a = cv2.imread("4.jpg", -1)
x, y, z = a.shape
b = np.zeros((x, y, 8), dtype=np.uint8)
for i in range(8):
    b[:, :, i] = 2 ** i
temp = np.zeros((x, y, 3), dtype=np.uint8)
for i in range(8):
    temp[:, :, 0] = cv2.bitwise_and(a[:, :, 0], b[:, :, i])
    temp[:, :, 1] = cv2.bitwise_and(a[:, :, 1], b[:, :, i])
    temp[:, :, 2] = cv2.bitwise_and(a[:, :, 2], b[:, :, i])
    cv2.imshow(str(i), temp)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, we get the following 8 bit plans:

The threshold value

I don’t know if the reader has noticed that the 0-4 bitmap details of both grayscale and color images are almost invisible and all are black. How do you make the details stand out?

The answer is to change the thresholds, which are definitely very small, so let’s just change them to 255, and all but zero will make them more detailed. Now let’s change the color bitmap extraction code, the code is as follows:

import cv2
import numpy as np

a = cv2.imread("4.jpg", -1)
x, y, z = a.shape
b = np.zeros((x, y, 8), dtype=np.uint8)
for i in range(8):
    b[:, :, i] = 2 ** i
temp = np.zeros((x, y, 3), dtype=np.uint8)
for i in range(8):
    temp[:, :, 0] = cv2.bitwise_and(a[:, :, 0], b[:, :, i])
    temp[:, :, 1] = cv2.bitwise_and(a[:, :, 1], b[:, :, i])
    temp[:, :, 2] = cv2.bitwise_and(a[:, :, 2], b[:, :, i])
    m = temp[:, :] > 0
    temp[m] = 255
    cv2.imshow(str(i), temp)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result should look like this:

Color bitmap synthesis

If there is decomposition, there must be synthesis. So we also need to know how to combine all the bitplanes into one graph. The code is as follows:

import cv2
import numpy as np

a = cv2.imread("4.jpg", -1)
x, y, z = a.shape
b = np.zeros((x, y, 8), dtype=np.uint8)
for i in range(8):
    b[:, :, i] = 2 ** i
bit_img = np.zeros((x, y, 3), dtype=np.uint8)
temp = np.zeros((x, y, 3), 'uint8')
for i in range(8):
    bit_img[:, :, 0] = cv2.bitwise_and(a[:, :, 0], b[:, :, i])
    bit_img[:, :, 1] = cv2.bitwise_and(a[:, :, 1], b[:, :, i])
    bit_img[:, :, 2] = cv2.bitwise_and(a[:, :, 2], b[:, :, i])

    temp[:, :, 0] = cv2.bitwise_or(temp[:, :, 0], bit_img[:, :, 0])
    temp[:, :, 1] = cv2.bitwise_or(temp[:, :, 1], bit_img[:, :, 1])
    temp[:, :, 2] = cv2.bitwise_or(temp[:, :, 2], bit_img[:, :, 2])

    m = bit_img[:, :] > 0
    bit_img[m] = 255
    #cv2.imshow(str(i)+".bmp",bit_img)
cv2.imshow('00000000', temp)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

Here we combine bitwise plans by bitwise or bitwise_or functions. After running, temp is the original image and bit_img is the bitmap. In this way, in the actual process, we can operate on bit_img in the middle and merge it into a map.

Gray level plane plan synthesis

Color is much more complex than gray scale, gray level plan merge is relatively simple. Without further ado, let’s go straight to the code:

import cv2
import numpy as np

a = cv2.imread("4.jpg".0)
r, c = a.shape
b = np.zeros((r, c, 8), dtype=np.uint8)
for i in range(8):
    b[:, :, i] = 2 ** i
c=np.zeros((r,c),dtype=np.uint8)
for i in range(8):
    temp = cv2.bitwise_and(a, b[:, :, i])
    c=cv2.bitwise_or(c,temp)
cv2.imshow("111",c)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code