Opencv notes—simple operations for picture addition and mixing

Picture addition:

  • Add cv: 255 + 13 = 255
  • np addition: 255 + 13 = 12
  • Note that the picture types should be consistent
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 中文显示配置
plt.rcParams['font.sans-serif']=['SimHei']      # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False        # 用来正常显示负号

# 读取图像
img1 = cv.imread("img/img1.jpeg")
img2 = cv.imread("img/img2.jpeg")
img1 = img1[:1080,:]                            # 为了形状一直

# 图像相加
# cv加法:255+13=255
img3 = cv.add(img1,img2)
# numpy加法:255+13=12
img4 = img1 + img2

# 图像显示
fig,axes = plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img3[:,:,::-1])
axes[0].set_title(u"cv中的加法")
axes[1].imshow(img4[:,:,::-1])
axes[1].set_title("直接相加")
plt.show()

Running result:
Insert picture description herepicture mix:

  • img3 = aimg1 + bimg2 + r
  • Note that the image type should be consistent
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 读取图像
img1 = cv.imread("img/img1.jpeg")
img2 = cv.imread("img/img2.jpeg")
img1 = img1[:1080,:]

print(img2.shape)   # 图像形状
print(img1.shape)   # 图像形状

# 图像混合
img3 = cv.addWeighted(img1,0.7,img2,0.3,0)

# 图像显示
plt.figure(figsize=(8,8))
plt.imshow(img3[:,:,::-1])
plt.show()

Running results:
Insert picture description herecv Xiaobaiwang big guy pointing

Guess you like

Origin blog.csdn.net/weixin_45666249/article/details/114904776