Chapter 3 - OpenCV Basics - 4 - Mask

basic concept

Mask, also known as mask, mask, template. Image mask refers to the use of specific images/graphics/objects to block the processed image (all or part), so as to control the image processing area. In digital image processing, the mask is a specified array. After all, the image is also array.

The picture on the left is the original picture, and the picture in the middle is the mask (the white area is the transparent area, and the black area is the black occlusion area). After control processing, the result picture on the right is obtained.

Multiplication

The basis of multiplication is that any number multiplied by 0 is 0, and any number multiplied by 1 is itself

The simple operation is as follows:

import cv2 as cv
import numpy as np

lena = cv.imread("lena.jpg")
shape = lena.shape
cv.imshow("lena", lena)
# 生成掩模区域
yanmo = np.zeros([shape[0], shape[1], shape[2]], np.uint8)
# 生成掩模实际作用区域
yanmo[100:300, 200:400] = 1
# 掩模乘法操作
new_lena = lena * yanmo
cv.imshow("new_lena", new_lena)

cv.waitKey()
cv.destroyAllWindows()

Displayed as follows:

 

Notice:

  1. The shape attribute of the image is a tuple containing the length, width, and number of channels of the image
  2. The resulting mask and image size must be exactly the same

logic operation

The basis of logical operation is that the result of bitwise AND operation between any number and 0 is 0, and the bitwise AND operation between any number and 1 is itself, and the value of all 1s in an 8-bit pixel value is 255.

So the above code can be changed to:

import cv2 as cv
import numpy as np

lena = cv.imread("lena.jpg")
shape = lena.shape
print(type(shape))
cv.imshow("lena", lena)
# 生成掩模区域
yanmo = np.zeros([shape[0], shape[1], shape[2]], np.uint8)
# 生成掩模实际作用区域,全部为255
yanmo[100:300, 200:400] = 255
# 掩模按位与操作
new_lena = cv.bitwise_and(lena, yanmo)
cv.imshow("new_lena", new_lena)

cv.waitKey()
cv.destroyAllWindows()

The effect is the same as the picture above.

mask as function argument

We know that the essence of the mask is a specific array, and the mask exists as a parameter in the function.

Example: Calculation result = cv2.add(parameter 1, parameter 2, mask)

When using the mask parameter, the operation will only be performed on the non-empty pixels in the mask array, and the pixel values ​​​​of other pixels will be set to 0

Specifically use the following code:

import cv2 as cv
import numpy as np

lena = cv.imread("lena.jpg")
# 随便截的一张图,大小和lena的一样 512*512
test = cv.imread("test.png")
h, w, c = lena.shape
cv.imshow("lena", lena)
cv.imshow("test", test)
# 生成掩模区域
yanmo = np.zeros([h,w], np.uint8)
# 生成掩模实际作用区域,全部为非0即可,这里随便选了100
yanmo[0:400, 100:400] = 100
# 掩模作为函数参数使用
new_lena = cv.add(lena, test, mask=yanmo)
cv.imshow("mask", new_lena)

cv.waitKey()
cv.destroyAllWindows()

operation result:

 

Notice:

  1. When the mask is implemented as a function parameter, as long as the value of the array position is 0, these positions are set to 0
  2. The value of other positions that are not 0, as long as it is not 0

 

Guess you like

Origin blog.csdn.net/sunguanyong/article/details/129211559