OpenCV-Python image bit and operation bitwise_and function detailed explanation

☞ ░Go to LaoYuanPython blog https://blog.csdn.net/LaoYuanPython

I. Overview

The AND operation of an image is mainly used to obtain the part of interest in an image. It is the bitwise AND of two image matrix arrays or an array and a scalar. The result is calculated as follows:

  1. When the two image matrix arrays represented by src1 and src2 have the same size, the value of the resulting matrix element is:
    dst(I)=src1(I)∧src2(I) if mask(I)≠0
  2. When src1 is a matrix array and src2 is a scalar, the value of the resulting matrix element is: the
    dst(I)=src1(I)∧src2 if mask(I)≠0
    specific scalar can be a constant or a four-tuple representing BGRA through the old monkey test. It is not confirmed whether there are other forms, but a two-tuple The sum triple is definitely not good. If it is a constant, OpenCV will convert it into a 4-tuple representing BGRA to participate in the operation. The first element of the quadruple is the constant, and the other elements are 0.
  3. When src1 is a scalar and src2 is a matrix array, the value of the resulting matrix element is: the
    dst(I)=src1∧src2(I) if mask(I)≠0
    scalar value is the same as the above scenario.

Regarding the above calculation process, the supplementary explanation is as follows:

  1. In the case of floating-point arrays, bit operations are related to the bit representation method related to the machine environment (usually compatible with IEEE754);
  2. In the case of a multi-channel matrix array, each channel is processed independently;
  3. In the second and third cases above, if the scalar is a constant or a single numeric variable, it is first converted to a quadruple.

2. Grammar description

Function prototype:

bitwise_and(src1, src2, dst=None, mask=None)

Parameter Description:
  • src1, src2: input image or scalar, scalar can be a single value or a quadruple
  • dst: optional output variable, if you need to use non-None, you must first define it, and its size is the same as the input variable
  • mask: Image mask, optional parameter, 8-bit single-channel grayscale image, used to specify the elements of the output image array to be changed, that is, the output image pixels are output only when the corresponding position element of the mask is not 0, otherwise All channel components of the pixel at this position are set to 0

The return value is the result image matrix. If the actual parameter is passed in dst, the return value is the same as the corresponding actual parameter of dst.

3. Case:

The following example loads an image into memory and then multiplies it with a value and a constructed mask matrix. The loaded original image imgs.jpg is a three-channel color image with a size of 720*600. The image is as follows: the
Insert picture description here
case code is as follows:

import numpy as np
import cv2
def main():
    imgSrc = cv2.imread(r'F:\pic\imgs.JPG')
    # 下面2行代码构造输出OpenCV图标的掩膜,255确保所有位都是1
    imgMask = np.zeros(imgSrc.shape,dtype=np.uint8)
    imgMask[20:190,580:715] = 255 
  

    resultImg1 = cv2.bitwise_and(imgSrc, imgMask)
    resultImg2 = cv2.bitwise_and(imgSrc, 255)
    resultImg3 = cv2.bitwise_and(imgSrc, (255,255,255,255))

    cv2.imshow('resultImg1', resultImg1)
    cv2.imshow('resultImg2', resultImg2)
    cv2.imshow('resultImg3', resultImg3)
    cv2.waitKey(0)
 
main()

The three output images are as follows:
Insert picture description here
Insert picture description here
Insert picture description here
you can see that the value of 255 and the image array and time only retain the pixel value of the B channel, and the images are all blue.

Four, summary

This article describes in detail the syntax and calculation method of the OpenCV-Python image bitwise_and function, and illustrates the bitwise AND of images and scalars, and the bitwise AND of constructed mask images and images. It can be seen that bitwise_and can control the selection of the channel of interest (adjust the element value of the quadruple) or area for output. At the same time, bitwise_and can be used to mask certain areas of the image so that they do not participate in the processing, and can also be used for image structural feature extraction, using similarity variables or image matching methods to detect and extract similar structural features in the image.

If you think this article can be helpful to you, please help me like and add a favorite, thank you!

For more information about OpenCV-Python, please refer to the related article in the column " OpenCV-Python Graphics and Image Processing ".

Paid column about the old ape

Lao Yuan’s paid column " Developing Graphical Interface Python Applications Using PyQt " specifically introduces the basic tutorials of PyQt graphical interface development based on Python, and the paid column " Moviepy Audio and Video Development Column " details the related methods and usage of moviepy audio and video editing and synthesis processing Method to process related editing and synthesis scenes. Both columns are suitable for novice readers who have a certain Python foundation but no relevant knowledge.

Paid column article catalog : " Moviepy audio and video development column article directory ", " Use PyQt to develop graphical interface Python application column directory ".

For those who lack Python foundation, you can learn Python from scratch through Lao Yuan’s free column " Column: Python Basic Tutorial Directory ".

If you are interested and willing to support the readers of Old Ape, welcome to buy paid columns.

Learn Python and OpenCV from the old ape!

☞ ░ to the old ape Python Bowen directory

Guess you like

Origin blog.csdn.net/LaoYuanPython/article/details/109148867