0%

OpenCV(五)-算术与位运算

一、加法运算

1
2
3
4
5
6
7
(function) def add(
src1: MatLike,
src2: MatLike,
dst: MatLike | None = ...,
mask: MatLike | None = ...,
dtype: int = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2
import numpy as np

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

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

# print(dog.shape)

img = np.ones((1200, 1920, 3), np.uint8) * 50

cv2.namedWindow('orig', cv2.WINDOW_NORMAL) # 这两行是为了重新设置窗口大小
cv2.resizeWindow('orig', 800, 600)
cv2.imshow('orig', dog) # 没有上面那两行,也可以显示,结果就是图片的大小

result = cv2.add(dog, img)
cv2.namedWindow('result', cv2.WINDOW_NORMAL)
cv2.resizeWindow('result', 800, 600)
cv2.imshow('result', result)
cv2.waitKey(0)

image-20241106133825500

二、减法运算

1
2
3
4
5
6
7
(function) def subtract(
src1: MatLike, # src1 减去 src2,减少亮度
src2: MatLike,
dst: MatLike | None = ...,
mask: MatLike | None = ...,
dtype: int = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2
import numpy as np

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

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

# print(dog.shape)

img = np.ones((1200, 1920, 3), np.uint8) * 50

cv2.namedWindow('orig', cv2.WINDOW_NORMAL)
cv2.resizeWindow('orig', 800, 600)
cv2.imshow('orig', dog)

result = cv2.subtract(dog, img)
cv2.namedWindow('result', cv2.WINDOW_NORMAL)
cv2.resizeWindow('result', 800, 600)
cv2.imshow('result', result)
cv2.waitKey(0)

image-20241106134420249

三、乘法运算

乘除法就是更快的加减,

1
2
3
4
5
6
7
(function) def multiply(
src1: MatLike,
src2: MatLike,
dst: MatLike | None = ...,
scale: float = ...,
dtype: int = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cv2
import numpy as np

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


img = np.ones((1200, 1920, 3), np.uint8) * 50

cv2.namedWindow('orig', cv2.WINDOW_NORMAL)
cv2.resizeWindow('orig', 800, 600)
cv2.imshow('orig', dog)

result = cv2.multiply(dog, img)
cv2.namedWindow('result', cv2.WINDOW_NORMAL)
cv2.resizeWindow('result', 800, 600)
cv2.imshow('result', result)
cv2.waitKey(0)

image-20241106134755836

四、除法运算

1
2
3
4
5
6
7
(function) def divide(
src1: MatLike,
src2: MatLike,
dst: MatLike | None = ...,
scale: float = ...,
dtype: int = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cv2
import numpy as np

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


img = np.ones((1200, 1920, 3), np.uint8) * 50

cv2.namedWindow('orig', cv2.WINDOW_NORMAL)
cv2.resizeWindow('orig', 800, 600)
cv2.imshow('orig', dog)

result = cv2.divide(dog, img)
cv2.namedWindow('result', cv2.WINDOW_NORMAL)
cv2.resizeWindow('result', 800, 600)
cv2.imshow('result', result)
cv2.waitKey(0)

image-20241106134845892

三、图像的溶合

1
2
3
4
5
6
7
8
9
(function) def addWeighted(
src1: MatLike, # 图片1
alpha: float, # 图片1的权重
src2: MatLike, # 图片2
beta: float, # 图片2的权重
gamma: float, # 静态权重
dst: MatLike | None = ...,
dtype: int = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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('result', result)

cv2.waitKey(0)

image-20241106135659757

四、位运算-非操作

讲矩阵中的01互换

1
2
3
4
5
(function) def bitwise_not(
src: MatLike,
dst: MatLike | None = ...,
mask: MatLike | None = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
import cv2
import numpy as np

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

img[50:150, 100: 200] = 255

new_img = cv2.bitwise_not(img)

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

image-20241106140437287

五、位运算-与操作

与操作,同一为一,所以下面只有两个白色的交叉部分为白

1
2
3
4
5
6
(function) def bitwise_and(
src1: MatLike,
src2: MatLike,
dst: MatLike | None = ...,
mask: MatLike | None = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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_and(img, img2)

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

image-20241106141248022

六、为运算-或与异或

1
2
3
4
5
6
7
8
9
10
11
12
13
(function) def bitwise_or(
src1: MatLike,
src2: MatLike,
dst: MatLike | None = ...,
mask: MatLike | None = ...
) -> MatLike

(function) def bitwise_xor(
src1: MatLike,
src2: MatLike,
dst: MatLike | None = ...,
mask: MatLike | None = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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_or(img, img2)
new_img2 = cv2.bitwise_xor(img, img2)

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

image-20241106141723484

七、大作业-为图像添加水印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#1. 引入一幅图片,dog
#2. 要有一个LOGO,需要自己创建
#3. 计算图片在什么地方添加,在添加的地方变成黑色
#4. 利用add,将logo 与 图处叠加到一起

import cv2
import numpy as np

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

#创建LOGO和mask
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
mask[80:180, 80:180] = 255 # 绿色区域的 mask

#对mask按位求反,01互换,与dog的图片进行操作,方块的位置就变为了0
m = cv2.bitwise_not(mask)

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

# 用掩码对原图区域进行与操作,保留原图的背景
tmp = cv2.bitwise_and(roi, roi, mask = m)

# 将 LOGO 添加到原图的对应区域
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)

image-20241106143531430

-------------本文结束感谢您的阅读-------------