OpenCV-Python Development Guide (4) --- bit plane decomposition and synthesis

What is bit plane decomposition

Combining binary pixel values ​​in a uniform bit in a grayscale image to obtain a binary image, which is called a bit plane of the grayscale image, and this process is called bit plane decomposition. For example, combining the values ​​of the lowest bits in the binary bits on all pixels in a grayscale image can form a "least significant bit" bit plane.

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

00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000

After taking these values, we can get all the bit-plane decomposition diagrams through bitwise AND operations. As for what's the use? Smart friends must know. For example, if we need to add a watermark to an image, should we add it on the high-bit plane or the least significant bit-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 bit plane decomposition

Now that we understand the gray-level bit-plane decomposition and all its knowledge. Next, we will get a drawing image and extract all its bit plane images.

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()

After running, we get 8 bit plane images of grayscale image:

Bit plane
The original picture looks like this:

Original image

Color image bit plane decomposition

Since grayscale images have their bit planes, color images can also be extracted. But because the matrix of the color image is a three-dimensional matrix, we need to perform a 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()

After running, we will get the following 8 bitmaps:
Color bit plane

Threshold

I don’t know, have the reader noticed, whether it’s a grayscale image or a color image, the details of the 0-4 bitmap are almost invisible, and they are all black. How to make its details more prominent at this time?

The answer is to change the threshold. Their value must be very small. We directly change it to 255, and all the details except 0 can be highlighted. Now we change the code for color bitmap extraction, 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()

After running, the effect is as follows:
Change threshold

Color bit plan synthesis

Since there is decomposition, there must be synthesis. So we also need to master how to combine all bit planes into one image. code show as below:

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()

Here, we merge into a bit plane image by bitwise or bitwise_or function. After running, temp is the original image, and bit_img is the bit plane image. In this way, when we are actually processing, we can manipulate the bit_img in the middle and merge it into one image.

Gray-scale bit-planar image synthesis

Color is much more complicated than grayscale, and gray-scale bit-plan merging is relatively simple. Without further ado, let's go directly 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()

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113769212