OpenCV makes Mask image mask

1. Mask (mask)
In some image processing functions, there will be mask parameters in some parameters, that is, this function supports mask operations. First of all, what is a mask and what is its use, as follows:
Mask in digital image processing The concept is borrowed from the process of PCB plate making. In semiconductor manufacturing, many chip process steps use photolithography technology. The graphic "negative" used in these steps is called a mask (also called "mask"), and its function is : Masking of an opaque pattern template in a selected area on the silicon wafer, and then the underlying etch or diffusion will only affect the area outside the selected area.
Image mask is similar to it, using a selected image, figure or object to block the processed image (all or part) to control the area or process of image processing.
In digital image processing, the mask is a two-dimensional matrix array, and sometimes a multi-valued image is used. The image mask is mainly used for:
① extracting the region of interest, and multiplying the pre-made mask of the region of interest with the image to be processed to obtain For the image of the ROI, the values ​​of the images inside the ROI remain unchanged, while the values ​​of the images outside the ROI are all 0.
② Shielding function, use a mask to shield certain areas on the image, so that they do not participate in the processing or calculation of processing parameters, or only process or count the shielded areas.
③ Structural feature extraction, using similarity variables or image matching methods to detect and extract structural features similar to the mask in the image.
④ Production of special shape images.

2. Case
1. Effect picture
The original picture is as follows:
insert image description here
Rectangular mask VS effect picture is as follows: (Using rectangle mask, only extract the specified area in the image, while ignoring other areas)
insert image description here
Circular mask VS effect picture is as follows: (circle The mask is shown on the left and the application of the mask is on the right. Mask images of essentially arbitrary shapes can be used, such as rectangles, circles, lines, polygons, etc. to extract regions from images)

insert image description here
2. Source code

def main():
    '''
    分别使用矩形和圆形遮罩从图像中提取矩形区域和圆形区域。
    '''
    import argparse
    import cv2
    import numpy as np

    images_bg = r'C:\dest\speed_bg.png'
    # 加载原始输入图像,并展示
    image = cv2.imread(images_bg)
    cv2.imshow("Original", image)
    # 掩码和原始图像具有相同的大小,但是只有俩种像素值:0(背景忽略)、255(前景保留)
    mask = np.zeros(image.shape[:2], dtype="uint8")
    cv2.rectangle(mask, (30, 90), (280, 440), 255, -1)
    cv2.imshow("Rectangular Mask", mask)
    # 应用掩码图像
    masked = cv2.bitwise_and(image, image, mask=mask)
    cv2.imshow("Rectangular Mask Applied to Image", masked)
    cv2.waitKey(0)
    # 构造一个圆形掩码(半径为140px,并应用位运算)
    mask = np.zeros(image.shape[:2], dtype="uint8")
    cv2.circle(mask, (210, 210), 160, 255, -1)
    # cv2.circle(mask, (202, 202), 110, 255, -1)
    masked = cv2.bitwise_and(image, image, mask=mask)
    bitwiseNot = cv2.bitwise_not(mask)
    cv2.imshow("bitwiseNot", bitwiseNot)
    # 展示输出图像
    cv2.imshow("Circular Mask", mask)
    cv2.imshow("Circular Mask Applied to Image", masked)
    cv2.waitKey()
    # TACH_PATTERN_PATH = r'C:\dest\new\tach_mask.png'
    # aircv.imwrite(TACH_PATTERN_PATH, bitwiseNot)
    TACH_PATTERN_PATH2 = r'C:\dest\new\speed_mask.png'
    aircv.imwrite(TACH_PATTERN_PATH2, mask)

if __name__ == "__main__":
    main()

Reference materials:
Image Masking with OpenCV
ultra-detailed annotations of OpenCV to make images Mask
OpenCV's bitwise_and, bitwise_not and other image basic operations and masks

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/129039070