Bitwise Operations
Bitwise operations are: And,or,not,xor and so on. When we extract part of an image, these operations are useful when choosing a non-rectangular ROI, often used for logo projection.
How to operate this section:
The image fixed threshold value of two is valued by the threshold function.
Definition: The two value of the image is to set the gray value of the pixel on the image to 0 or 255, that is, the entire image will be visible only black and white visual effects.
An image includes the target object, background and noise, in order to extract the target object directly from the multi-valued digital image, the common method is to set a threshold T, using t to divide the image data into two parts: a group of pixels larger than t and a pixel group less than T. This is the most special method to study the gray-scale transformation, called the image of the two value (binarization).
import cv2import numpy as npimg = cv2.imread(‘1.jpg‘)img2 = cv2.imread(‘2c.jpg‘)rows,cols,channels = img2.shaperoi = img[0:rows, 0:cols]GrayImage=cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) # 中值滤波 GrayImage= cv2.medianBlur(GrayImage,5) # mask_bin 是黑白掩膜ret,mask_bin = cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY) #mask_inv 是反色黑白掩膜mask_inv = cv2.bitwise_not(mask)# 黑白掩膜 和 大图切割区域 取和img1_bg = cv2.bitwise_and(roi,roi,mask = mask_bin)#反色黑白掩膜 和 logo 取和img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv)dst = cv2.add(img1_bg,img2_fg) img[0:rows, 0:cols ] = dstcv2.imshow(‘GrayImage‘,mask_bin)cv2.waitKey(0)cv2.destroyAllWindows()
For ease of understanding, paste the process diagram
Mask_bin:
MASK_INV:
IMG1_BG and IMG2_FG:
Eventually:
"AI Basics" python:opencv--image arithmetic Operations (2): Bitwise operations