OpenCV study notes 03--image operation--image addition operation, image bit operation, image mask

content

(1), image addition operation

(2), bit logic operation

(3) Mask

(4) Other bit logic operations

(5) Bitmap decomposition in image processing


Introduction: In digital image processing, changing the pixels of an image involves some operational problems. The focus of this article is on bit logic operations. Through bit logic operations, we can find some magic. Using bit operations, we can Realize the addition of watermarks, the encryption and decryption of images, and the partial images of the images we are interested in can also be extracted by logical operations. Enter the topic below.

(1), image addition operation

The addition operation of the image can be operated with the '+' sign or the cv2.add() function in the opencv library, but the operation results of the images obtained by these two methods are different. In addition, let's introduce the usage of the cv2.addWeighted() function.

Suppose we operate on an 8-bit image. That is, the value of each pixel is between [0, 255].

If the '+' sign is used to perform the addition operation of image pixels, if the result after addition is greater than 255, the calculation is restarted from 0, that is, the remainder operation is performed on 255.

If the cv2.add() function is used for the addition operation, if the result after the addition is greater than 255, the uniform assignment is 255.

Let's take a look through the program.

import cv2
import numpy as np
# 声明两个随机数组,大小为4*4
img1 = np.random.randint(0, 255, (4, 4), dtype=np.uint8)
img2 = np.random.randint(0, 255, (4, 4), dtype=np.uint8)

# 打印一下生成的随机数组
print('img1=\n', img1)
print('img2=\n', img2)

# 利用运算符+进行像素的加法运算
img3 = img1 + img2
print('img1 + img2 = \n', img3)
img4 = cv2.add(img1, img2)
print('cv2.add(img1, img2)=\n', img4)

The operation and results are shown below. If there are pixels and greater than 255, the results obtained by the two methods are different:

 Suggestion: It is best to use the cv2.add() function, because if the '+' sign is used, if the pixel sum is 256, the result is 1, which changes from the brightest to the darkest, which we don't want to get. With cv2.add(), if it exceeds 255, it defaults to the maximum value of 255. There is no going from the brightest pixel to the darkest pixel.

The addition operation in front of us is to add the pixels directly and rudely. The cv2.addWeighted() function can be implemented by multiplying the pixel value of the image by a number, that is, participating in the addition operation at a certain ratio. The specific usage is as follows

cv2.addWeighted(img1,  x1,  img2,  x2,  b)

img1 and img2 represent two image arrays, x1 is the number to be multiplied by each pixel of the image img1, x2 is the number to be multiplied by each pixel of the image img2, b is the result of the addition of img1 and img2 plus the value of b.

Let's look at it by example, first declare two arrays, one full of 10, one full of 100, multiplied by different coefficients 0.6, 0.4, plus a b=1.

import numpy as np
import cv2
# 利用加权值对像素进行处理cv2.addWeighted(x, a, y, b, c)
img5 = np.ones((4, 4), dtype=np.uint8) * 10
img6 = np.ones((4, 4), dtype=np.uint8) * 100
img7 = cv2.addWeighted(img5, 0.6, img6, 0.4, 1)
print('img5=\n', img5)
print('img6=\n', img6)
print('cv2.addWeighted(img5, 0.6, img6, 0.4)=\n', img7)

The results are as follows:

 (2), bit logic operation

We have learned binary in mathematics and computer introduction. Bit logic operation is an operation based on binary. The main operations are AND, OR, NOT, and XOR. These bit operations have their advantages, and their corresponding opencv functions are shown in the following table.

Function name basic meaning
cv2.bitwise_and() bit and
cv2.bitwise_or() bit or
cv2.bitwise_xor() XOR
cv2.bitwise_not() negate (not)

cv2.bitwise_and(img1, img2, mask=)

The parameters of the above four functions are the same except for bit negation. Here we will explain with one of them.

img1 and img2: These two are array matrices, which can be the array matrix corresponding to the image or the array matrix defined by yourself.

mask: Indicates a mask. It is an optional parameter, not a mandatory parameter. It can be set according to the requirements. The detailed explanation of the mask will be given later.

Next, let's take a look at the theoretical provisions of these bit operations.

Bit AND: 1 if all 1s, 0 otherwise.

Bit OR: 0 if all 0s, 1 otherwise.

Bit XOR: 0 if they are the same, 1 if they are not.

Bit not: is to take the opposite. If it is 0, take 1, if it is 1, take 0.

As shown below:

 Application of bit AND operation

We mentioned the principle of bit AND operation before, if the same is 1, it is 1, otherwise it is zero. Then let's think about it, if there is a pixel value of 6 now, its corresponding binary value is 0000 0110, we now set another pixel value of 255, the corresponding binary value is: 1111 1111, and the binary of 6 and 255 is bit-anded The result is: 0000 0110. That is, the result is still 6. You can try the binary AND operation with other values ​​x and 255, and the result is still x.

I can draw the following conclusions:

We perform a binary bit operation on any number x with the number 255, and the result is the bit itself.

Likewise, if we perform a binary operation on any value x with the value 0, the result is always 0.

Using the above conclusions, we can extract the part of the image that we are interested in. For example, we have an image and want to extract face information. First read a gray image, then we generate an array matrix of the same size as the image, and then we set some values ​​​​to 255 to get the original image information, and some values ​​​​are set to 0 to display the unwanted places as black. Implement it with the following code.

import numpy as np
import cv2
# 读取一幅图像
filename = r'C:\Users\LBS\Desktop\lena.jpg'
f = cv2.imread(filename, 0)
cv2.imshow('01', f)
# 用位与操作,获取我们感兴趣的部分,利用全1则为1,否则为0的特点
Mat = np.zeros(np.shape(f), dtype=np.uint8)
Mat[100:400, 100:400] = 255
Mat[400: 500, 100:200] = 255
# 将定义的矩阵Mat和图像f进行位与操作
img8 = cv2.bitwise_and(f, Mat)
cv2.imshow('02', Mat)
cv2.imshow('03', img8)
cv2.waitKey()
cv2.destroyAllWindows()

The running result is as follows: the pixel position of the image corresponding to the matrix pixel value of 255 is displayed as the original image content, and the other places are displayed as black.

(3) Mask

A mask is also an array of matrices, consisting of a sequence of values. It can limit the area of ​​bit operation operation, that is, operate at the position where the value of the mask is not equal to 0, and the position where the mask is equal to zero, then assign all the positions where the mask corresponding to 0 of the image to be operated is assigned to 0, 0 i.e. black. For example, suppose we now construct two matrix arrays Mat1, Mat2, and then construct a mask Mat3, and then perform an addition operation on the two matrix arrays (the same is true for bit operations, addition is used here to show the use of masks), then addition The operation area is performed at the position where Mat3 is not equal to 0, and other positions are assigned 0.

code show as below:

import numpy as np
import cv2
# 掩膜,控制计算的范围,只在掩膜不等于1的位置进行操作,其余位置置为0。
Mat1 = np.ones((4, 4), dtype=np.uint8) * 6
Mat2 = np.ones((4, 4), dtype=np.uint8) * 4
# 定义一个掩膜,操作只会在掩膜值为非空的像素点上进行,将其它的像素点的值置为0
Mat3 = np.zeros((4, 4), dtype=np.uint8)
Mat3[2:4, 2:4] = 1
print('Mat1=\n', Mat1)
print('Mat2=\n', Mat2)
print('Mat3=\n', Mat3)
#在掩膜规定的非0位置处执行加法操作,也适用执行位逻辑操作。
Mat4 = cv2.add(Mat1, Mat2, mask=Mat3)
print('Mat4=\n', Mat4)

The result is as follows:

 The mask is applied in the image as follows:

import numpy as np
import cv2
# 读取彩色图像
filename = r'C:\Users\LBS\Desktop\lena.jpg'
f1 = cv2.imread(filename, 1)
cv2.imshow('f1', f1)
# 这里需要注意的是要提取出彩色图像的长宽,来构造掩膜的大小
w, h, c = np.shape(f1)
# 定义一个掩膜mask,操作只会在掩膜值为非空的像素点上进行,将其它的像素点的值置为0
mask = np.zeros((w, h), dtype=np.uint8)
mask[100:400, 100:400] = 255
mask[400:500, 100:200] = 255
# 执行位与运算
#f1 和 f1进行运算的结果还是f1,只不过是在mask指定的非0的区域上进行的操作
f2 = cv2.bitwise_and(f1, f1, mask=mask)
cv2.imshow('f2', f2)
cv2.waitKey()
cv2.destroyAllWindows()

The result of running is as follows:

(4) Other bit logic operations

Bit OR operation and bit XOR operation, we have given its calculation rules at the beginning, its operation and bit AND operation are the same, and its bit XOR operation can realize the encryption and decryption of images. Bit AND operation can also achieve bit-level watermarking and extraction. Due to the limited space, the following articles will implement and explain these in detail .

(5) Bitmap decomposition in image processing

 Here we mainly take the gray image for illustration. If we have an image pixel with 8 bits, it means that we now have a pixel value of 6, which can be represented by binary. This binary has 8 bits, which is 0000 0110. Then let's think about whether it is possible to split these eight bits, then an image consists of several pixel values, and then each pixel value can be converted into 8-bit binary, then we convert each bit of the binary of each pixel Is it possible to extract 8 images by extracting them separately? For example, I regard the current image as an array composed of binary. For the existing image, I extract the 7th bit of the binary of each pixel, then the value of the seventh bit corresponding to each pixel is extracted to form a image. Subsequent articles will also summarize notes on bitmap decomposition in detail.

It is not easy to write, please indicate the source when reprinting.

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/124025061