Content from Opencv-python tutorials own translation finishing
Target
Image addition, subtraction, bitwise operation
Learning function Cv2.add (), cv2.addweighted ()
addition:
Add two images using Cv2.add (), which can be implemented using matrix addition in NumPy. But in OpenCV the addition is saturated operation, that is, the upper bound value, NumPy will take the result modulo.
In a comprehensive way, the use of OPENCV is more effective
Img1=cv2.imread (' 1.jpg ')
img2=cv2.imread (' 2.jpg ')
res = Cv2.add (IMG1,IMG2)
Original image
The result after addition
image Blending
It's actually addition, it's just a proportional mix, with different weights.
The formula is as follows
G (x) = (1−α) f0 (x) +αf1 (x)
Now the weight of the first image is 0.7, the weight of the second image is 0.3, and the cv2.addweighted () function is used to mix
Img1=cv2.imread (' 1.jpg ')
img2=cv2.imread (' 2.jpg ')
dst=cv2.addweighted (img1,0.7,img2,0.3,0)
The result of mixing
Bitwise Operations
Bitwise operations Have and, or, not, XOR. Bit operations are useful when extracting part of the image to select non-regional ROI. The following example is a specific area that changes a picture.
Put the OPENCV logo on another image, if you use addition, the color will change, if the use of mixing, will become transparent, but we do not want to transparent effect, if the region is held, you can use the ROI method, but not held, the following use bit operation implementation.
Import cv2
import numpy as NP
# Load image
IMG1 = cv2.imread (' 2.jpg ')
img2 = Cv2.imread (' 1.jpg ')
rows, Cols,channels = img2.shape
roi = img1[0:rows, 0:cols]
Img2gray = Cv2.cvtcolor (img2,cv2. Color_bgr2gray)
ret, mask = cv2.threshold (Img2gray, 175, 255, Cv2. Thresh_binary) #ret是阈值 (175) Mask is a two-valued image
Mask_inv = Cv2.bitwise_not (mask) #获取把logo的区域取反
IMG1_BG = Cv2.bitwise_ and (Roi,roi,mask = mask) #在img1上面, the logo area and mask are taken with the value
of 0 # to the value of the pixel corresponding to the Non-zero value in Mask_inv in Roi, with the other value 0.
# put the logo in the picture
IMG2_FG = Cv2.bitwise_and (Img2,img2,mask = MASK_INV) #获取logo的像素信息
DST = Cv2.add (IMG1_BG, IMG2_FG) #相加即可
img1[0:rows, 0:cols] = DST
cv2.imshow (' res ', IMG2_FG)
cv2.waitkey (0)
Cv2.destroyallwindows ()
Examples inside the idea is more ingenious, logo background are black, easy to extract out
First set the logo threshold and binary, to get the scope of the logo area
To place the pixel information of the logo area in the background picture, use the bit operator to set 0
Add the pixel information of the logo and the pixel of the background picture
And in the end, that's the effect
Practice
Make a slide-like picture smooth transition to another picture (similar to the one that Fengjie becomes Yifei)
Import cv2
import NumPy as NP
def nothing (x):
pass
img1 = Cv2.imread (' 1.jpg ')
img2 = Cv2.imread (' 2. JPG ')
# Create a black background window
img = Np.zeros ((500,500,3), np.uint8)
Cv2.namedwindow (' image ')
Cv2.createtrackbar (' A ', ' image ', 0,100,nothing) while
(1):
cv2.imshow (' Image ', img)
k = Cv2.waitkey (1 ) & 0xFF
If k = =:
break
r = Cv2.gettrackbarpos (' A ', ' image ')
r=float (R)/100.0
img= Cv2.addweighted (img1,r,img2,1.0-r,0)
cv2.destroyallwindows ()
Terrible ~