目录
1 图像的算术运算 2 图像的逻辑运算
参数含义:
cv2.add(src1,src2,dst,mask,dtype)
输出两个图像加法:dst = src1 + src2
cv2.subtract(src1,src2,dst,mask,dtype)
输出两个图像减法:dst = src1 - src2
cv2.multiply(src1,src2,dst,scale,dtype)
输出两个图像乘法:dst = scale * src1 * src2
cv2.divide(src1,src2,dst,scale,dtype)
举个例子
import cv2
import numpy as np
from matplotlib import pyplot as plt
x = np.uint8([250])
y = np.uint8([10])
print(cv2.add(x, y))
print(x+y)
img1 = cv2.imread('test5.jpg') # 图片1
img2 = cv2.imread('test6.jpg') # 图片2
add = cv2.add(img1, img2) # 两个图像相加
subtract = cv2.subtract(img1, img2) # 两个图像相减
multiply = cv2.multiply(img1, img2) # 两个图像相乘
divide = cv2.divide(img1, img2) # 两个图像相除
plt.subplot(231), plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB), 'gray'), plt.title('img1')
plt.subplot(232), plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB), 'gray'), plt.title('img2')
plt.subplot(233), plt.imshow(cv2.cvtColor(add, cv2.COLOR_BGR2RGB), 'gray'), plt.title('add')
plt.subplot(234), plt.imshow(cv2.cvtColor(subtract, cv2.COLOR_BGR2RGB), 'gray'), plt.title('subtract')
plt.subplot(235), plt.imshow(cv2.cvtColor(multiply, cv2.COLOR_BGR2RGB), 'gray'), plt.title('multiply')
plt.subplot(236), plt.imshow(cv2.cvtColor(divide, cv2.COLOR_BGR2RGB), 'gray'), plt.title('divide')
plt.show()
结果如下:
输出两个图像除法:dst = scale * src1 / src2
cv2.addWeighted(src1,alpha,src2,beta,gamma,dst,dtype)
输出两个图像线性相加:dst = alpha * src1 + beta * src2 + gamma
例如:
import cv2
import numpy as np
from matplotlib import pyplot as plt
def nothing(x): # 滑动条的回调函数
pass
top = cv2.imread('test5.jpg') # 图片1
bottom = cv2.imread('test6.jpg') # 图片2
WindowName = 'example' # 窗口名
cv2.namedWindow(WindowName, cv2.WINDOW_AUTOSIZE) # 建立空窗口
cv2.createTrackbar('proportion', WindowName, 0, 100, nothing) # 两张图片间转换
img1 = cv2.addWeighted(bottom, 0, top, 1, 0)
img2 = cv2.addWeighted(bottom, 0.3, top, 0.7, 0)
img3 = cv2.addWeighted(bottom, 0.5, top, 0.5, 0)
img4 = cv2.addWeighted(bottom, 0.7, top, 0.3, 0)
img5 = cv2.addWeighted(bottom, 1, top, 0, 0)
plt.subplot(231), plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB), 'gray'), plt.title('0:10')
plt.subplot(232), plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB), 'gray'), plt.title('3:7')
plt.subplot(233), plt.imshow(cv2.cvtColor(img3, cv2.COLOR_BGR2RGB), 'gray'), plt.title('5:5')
plt.subplot(234), plt.imshow(cv2.cvtColor(img4, cv2.COLOR_BGR2RGB), 'gray'), plt.title('7:3')
plt.subplot(235), plt.imshow(cv2.cvtColor(img5, cv2.COLOR_BGR2RGB), 'gray'), plt.title('10:0')
while(1):
a1 = cv2.getTrackbarPos('proportion', WindowName) # 获取a1滑动条值
overlap = cv2.addWeighted(bottom, 1-a1/100, top, a1/100, 0) # 将两张图片相加
cv2.imshow(WindowName, overlap)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
elif k == ord('m'): # 按m输出matplotlib绘图
plt.show()
cv2.destroyAllWindows()
结果如下:
按m输出matplotlib绘图
cv2.bitwise_and(src1,src2,dst,mask)
输出两个图像的交集:dst = src1 & src2
cv2.bitwise_or(src1,src2,dst,mask)
输出两个图像的并集:dst = src1 | src2
cv2.bitwise_not(src,dst,mask)
输出图像的非:dst = - src
cv2.bitwise_xor(src1,src2,dst,mask)
输出两个图像的异或:dst = src1 ^ src2
例如:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img1 = cv2.imread('test8.jpg') # 图片1
img2 = cv2.imread('test9.jpg') # 图片2
band = cv2.bitwise_and(img1, img2) # 两个图像求交
bor = cv2.bitwise_or(img1, img2) # 两个图像求并
bnot = cv2.bitwise_not(img1) # 图像求非
bxor = cv2.bitwise_xor(img1, img2) # 两个图像求异或
plt.subplot(231), plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB), 'gray'), plt.title('img1')
plt.subplot(232), plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB), 'gray'), plt.title('img2')
plt.subplot(233), plt.imshow(cv2.cvtColor(band, cv2.COLOR_BGR2RGB), 'gray'), plt.title('and')
plt.subplot(234), plt.imshow(cv2.cvtColor(bor, cv2.COLOR_BGR2RGB), 'gray'), plt.title('or')
plt.subplot(235), plt.imshow(cv2.cvtColor(bnot, cv2.COLOR_BGR2RGB), 'gray'), plt.title('not')
plt.subplot(236), plt.imshow(cv2.cvtColor(bxor, cv2.COLOR_BGR2RGB), 'gray'), plt.title('xor')
plt.show()
结果如下:
如果用普通图片效果如下:
最后讲一下掩膜 ,数字图像处理中,图像掩模主要用于:
举一个例子:
import cv2
import numpy as np
img1 = cv2.imread('test5.jpg')
img2 = cv2.imread('logo.jpg')
rows, cols, channels = img2.shape # 获取图像2的属性
roi = img1[125:125+rows, 125:125+cols] # 选择roi范围
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 转换为灰度图像
ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY) # 设置阈值
mask_inv = cv2.bitwise_not(mask) # 非运算
img1_bg = cv2.bitwise_and(roi, roi, mask=mask)
img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv)
dst = cv2.add(img1_bg, img2_fg)
img1[125:125+rows, 125:125+cols] = dst
cv2.imshow('res', img1)
cv2.imshow('mask', mask)
cv2.imshow('mask_imv', mask_inv)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果如下:
原图
根据阈值限制的得到的掩膜
掩膜求非
最后的效果