OpenCV Quick Start Nine: OpenCV Arithmetic and Bit Operations

One: Arithmetic operations

1. Addition

cv2.add(img1, img2)

img1: Image 1 to be added
img2: Image 2 to be added

Add the two pictures, use 255 count if it is greater than 255.
The size of the two pictures to be synthesized must be the same

Subtraction, multiplication, and division are similar, so I won’t describe them too much
. Example:

import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')

#图的加法运算就是矩阵的加法运算
#因此,加法运算的两张图大小必须是相等的

#print(dog.shape)

img = np.ones((1200, 1920, 3), np.uint8) * 50#像素值变成50

cv2.imshow('orig', dog)

result = cv2.add(dog, img)#加法  感觉曝光一样
cv2.imshow('result', result)

orig_1 = cv2.subtract(result, img)#减法变暗 result-img
#乘multiply(A,B)
#除divide(A,B)
cv2.imshow('org_1', orig_1)

cv2.waitKey(0)

Addition effect demonstration jia
Subtraction effect demonstrationsubtraction

Two: Image Fusion

1. Fusion function

AddWeighted( InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1);

src1: Indicates the first array that needs to be weighted, and often fills in a Mat
alpha: Indicates the weight of the first array
src2: Indicates the second array, which needs to have the same size and number of channels as the first array
beta: Indicates the first Two arrays of weight values
​​gamma: a scalar value to add to the sum of weights. Its meaning will naturally be understood through the formulas listed next
dst: OutputArray type dst, the output array, which has the same size and number of channels as the two input arrays
dtype: optional depth of the output array, with a default value -1. When the two input arrays have the same depth, this parameter is set to -1 (default value), which is equivalent to srcl.depth()

When alpha, beta is 1, and gamma is 0, the effect is the same as add.
Only when the attributes of the two images are the same can fusion be performed.

The output image can be understood as

dst = src1 [I]*alpha+ src2[I]*beta + gamma;

2. Example

import cv2
import numpy as np

back = cv2.imread('./back.jpeg')
smallcat = cv2.imread('./smallcat1.jpeg')

#只有两张图的属性是一样的才可以进行融合
print(back.shape)
print(smallcat.shape)

result = cv2.addWeighted(smallcat, 0.7, back, 0.3, 0)
cv2.imshow('add2', result)
cv2.waitKey(0)

Three: bit operations

1. AND operation

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

src1: image to participate in AND operation
src2: AND operation with src2 and src1
dst: output array with the same size and type as input
mask: optional operation mask, 8-bit single-channel array, which specifies the output array to be changed element

The operation with transportation is 1 & 1 = 1, and the others are 0, which can be understood as a parallel circuit to control small lights

Or, not, different or the same reason, so I won’t make too many descriptions

2. Example

import cv2
import numpy as np

#创建一张图片
img = np.zeros((200,200), np.uint8)
img2 = np.zeros((200,200), np.uint8)

img[20:120, 20:120] = 255
img2[80:180, 80:180] = 255

#new_img = cv2.bitwise_not(img)  #非  黑白颜色反转
#new_img = cv2.bitwise_and(img, img2)  #与--图像交集  全1则1  类似于串联控制灯亮  只有白色相交的地方是白色
#new_img = cv2.bitwise_or(img, img2)   #或--非0都显示
new_img = cv2.bitwise_xor(img, img2)   #异或 --交集0 


cv2.imshow('new_img', new_img)
cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.waitKey(0)

Non: color inversion (only one array can be operated)
yu

AND: similar to intersectionand

or: similar to unionor

XOR: Intersection 0, others unchanged
do

Four: Add watermark

1. The numpy operation learned before is used here

OpenCV Quick Start Six: Graphical Numpy

2,. example

#1. 引入一幅图片,dog
#2. 要有一个LOGO,需要自己创建
#3. 计算图片在什么地方添加,在添加的地方变成黑色
#4. 利用add,将logo 与 图处叠加到一起

import cv2
import numpy as np

#导入图片
dog = cv2.imread('./dog.jpeg')

#创建LOGO和掩码
logo = np.zeros((200, 200, 3), np.uint8)
mask = np.zeros((200, 200), np.uint8)

#绘制LOGO
logo[20:120, 20:120] = [0,0,255]
logo[80:180, 80:180] = [0,255,0]

mask[20:120, 20:120] = 255
mask[80:180, 80:180] = 255

#对mask按位求反
m = cv2.bitwise_not(mask)

#选择dog添加logo的位置
roi = dog[0:200, 0:200]

#与m进行与操作
tmp = cv2.bitwise_and(roi, roi, mask = m)

dst = cv2.add(tmp, logo)

dog[0:200,0:200] = dst

cv2.imshow('dog', dog)
# cv2.imshow('dst', dst)
# cv2.imshow('tmp', tmp)
# cv2.imshow('m', m)
# cv2.imshow('mask', mask)
# cv2.imshow('logo', logo)
cv2.waitKey(0)

output:
gou

Daily "big pie":
Although the youth is not left for the teenagers, no matter how far we go

Guess you like

Origin blog.csdn.net/weixin_52051554/article/details/126368642