import cv2
import numpy as np
x = np.uint8([250])
y = np.uint8([10])
# OpenCV加法
print(cv2.add(x, y)) # 250+10 = 260 => 255
# Numpy加法
print(x+y) # 250+10 = 260 % 256 = 4
对图像赋予不同的权重,以使其具有融合或透明的感觉。
图像融合:dst = α*img1 + β*img2 + γ
cv2.addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None)
import cv2
from matplotlib import pyplot as plt
# 读取图像
img1 = cv2.imread('lena.jpg') # (512, 512, 3)
img2 = cv2.imread('opencv.jpg') # (536, 536, 3)
print(img1.shape)
print(img2.shape)
# 改变尺寸
img2 = cv2.resize(img2, (512, 512))
# 图像融合:dst = α*img1 + β*img2 + γ
# α:img1的权重,β:img2的权重
dst = cv2.addWeighted(img1, 0.8, img2, 0.2, 0)
# 显示多张图像
plt.subplot(131), plt.title("1. img1"), plt.axis('off')
plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)) # 显示 img1(RGB)
plt.subplot(132), plt.title("2. img2"), plt.axis('off')
plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)) # 显示 img2(RGB)
plt.subplot(133), plt.title("dst. img3"), plt.axis('off')
plt.imshow(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)) # 显示 img2(RGB)
plt.show()
这包括按位 AND
、 OR
、NOT
和 XOR
操作。
cv2.bitwise_and(src1, src2, dst=None, mask=None)
计算两个数组或数组的逐元素按位连进行与运算
cv2.threshold(src, thresh, maxval, type, dst=None)
图像阈值(见OpenCV学习(9))
import cv2
from matplotlib import pyplot as plt
# 读取图像
img1 = cv2.imread('lena.jpg') # (512, 512, 3)
img2 = cv2.imread('opencv.jpg') # (536, 536, 3)
# 提取OpenCV标志,将其缩小后放在img1的左上角
img2 = cv2.resize(img2, (200, 200))
rows, cols, channels = img2.shape
# 创建roi区域
roi = img1[0:rows, 0:cols]
# 现在创建logo的掩码,并同时创建其相反掩码
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 200, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
# 现在将ROI中logo的区域涂黑,即像素值为0
img1_bg = cv2.bitwise_and(roi, roi, mask=mask)
# 仅从logo图像中提取logo区域
img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv)
# 将logo放入ROI并修改主图像
dst = cv2.add(img1_bg, img2_fg)
img1[0:rows, 0:cols] = dst
# 显示多张图像
plt.subplot(231), plt.title("mask. img1"), plt.axis('off')
plt.imshow(cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)) # 显示 img1(RGB)
plt.subplot(232), plt.title("mask_inv. img2"), plt.axis('off')
plt.imshow(cv2.cvtColor(mask_inv, cv2.COLOR_GRAY2RGB)) # 显示 img2(RGB)
plt.subplot(233), plt.title("img1_bg. img3"), plt.axis('off')
plt.imshow(cv2.cvtColor(img1_bg, cv2.COLOR_BGR2RGB)) # 显示 img2(RGB)
plt.subplot(234), plt.title("img2_fg. img4"), plt.axis('off')
plt.imshow(cv2.cvtColor(img2_fg, cv2.COLOR_BGR2RGB)) # 显示 img2(RGB)
plt.subplot(235), plt.title("dst. img5"), plt.axis('off')
plt.imshow(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)) # 显示 img2(RGB)
plt.subplot(236), plt.title("img1. img6"), plt.axis('off')
plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)) # 显示 img2(RGB)
plt.show()