图像加法
import numpy as np
import cv2
img = cv2.imread('jk.jpg')
res = cv2.add(img, img)
cv2.imshow('Mypicture1',img)
cv2.imshow('MyPicture2',res)cv2.waitKey(0)
cv2.destroyAllWindows()
res = cv2.add(src1, src2, dst=None, mask=None, dtype=None)
src1:第一幅图像
src2:第二幅图像
mask:图像掩膜
dtype:图像数据类型
2幅图像必须相同大小才可以相加。图像加法就是图像的像素值相加,如果a+b > 255,则c=255。最大的特色是图像会变得更亮。
import cv2
import numpy as npimg = cv2.imread('jk.jpg')
res1 = cv2.add(img,img)
res2 = img + img
cv2.imshow('MyPicture1',img)
cv2.imshow('MyPicture2',res1)
cv2.imshow('Mypicture3', res2)cv2.waitKey(0)
cv2.destroyAllWindows()
使用数学符号(+)执行图象加法。
c= a+b # 如果a+b<=255
c = mod((a+b), 256) #如果a+b>255,相当于取256的余数
掩膜
import cv2
import numpy as npimg1 = np.ones((4,5), dtype=np.uint8)*8
img2 = np.ones((4,5), dtype=np.uint8)*9
mask = np.zeros((4,5), dtype=np.uint8)
mask[1:3,1:4] = 255
dst = np.random.randint(0,256,(4,5), np.uint8)
print('img1=\n',img1)
print('img2=\n',img2)
print('mask=\n',mask)
print('最初值 dst=\n',dst)
dst = cv2.add(img1,img2,mask=mask)
print('结果值 dst=\n',dst)
重复曝光技术
图像加权和
dst = saturate(src1 X alpha + src2 X beta + gamma)
src1和src2必须是大小相同的图像
dst = addWeighted(src1, alpha, src2, beta, gamma)
import cv2
import numpy as npsrc1 = np.ones((2,3), dtype=np.uint8)*10
src2 = np.ones((2,3), dtype=np.uint8)*50
alpha = 1
beta = 0.5
gamma = 5
print(f'src1=\n{src1}')
print(f'src2=\n{src2}')
dst = cv2.addWeighted(src1,alpha,src2,beta,gamma)
print(f'dst=\n{dst}')
图像的位运算
cv2.bitwise_and():相当于逻辑的and运算
cv2.bitwise_or():相当于逻辑的or运算
cv2.bitwise_not():相当于逻辑的not运算
cv2.bitwise_xor():相当于逻辑的xor运算
dis = cv2.bitwise_and(src1, src2, mask=None)
import cv2
import numpy as np
src1 = np.random.randint(0,255,(3,5),dtype=np.uint8)
src2 = np.zeros((3,5),dtype=np.uint8)
src2[0:2,0:2] = 255
dst = cv2.bitwise_and(src1,src2)
print(f'src1=\n{src1}')
print(f'src2=\n{src2}')
print(f'dst=\n{dst}')
import cv2
import numpy as npsrc1 =cv2.imread('jk.jpg')
src2 = np.zeros(src1.shape, dtype = np.uint8)
src2[30:260,70:260,:] = 255 # 这是三维数组
dst = cv2.bitwise_and(src1,src2)
cv2.imshow('Hung',src1)
cv2.imshow('Mask',src2)
cv2.imshow('Result',dst)cv2.waitKey(0)
cv2.destroyAllWindows()
dst = cv2.bitwise_or(src1, src2, mask=None)
import cv2
import numpy as npsrc1 = cv2.imread('jk.jpg')
src2 = np.zeros(src1.shape,dtype=np.uint8)src2[30:260,70:260,:] = 255
dst = cv2.bitwise_or(src1,src2)
cv2.imshow('Hung',src1)
cv2.imshow('mask',src2)
cv2.imshow('Result',dst)cv2.waitKey(0)
cv2.destroyAllWindows()
dst = cv2.bitwise_not(src, mask=None)
import cv2
import numpy as npsrc = cv2.imread('forest.jpg')
dst = cv2.bitwise_not(src)
cv2.imshow('Forest',src)
cv2.imshow('Result',dst)cv2.waitKey(0)
cv2.destroyAllWindows()
dst = cv2.bitwise_xor(src1,src2,mask=None)
只要不相同则返回1.
任意像素与白色像素值(11111111)执行xor运算时,结果是not运算的结果。
人以像素于黑色像素值(00000000)执行xor运算时,结果是原像素值。
import cv2
import numpy as npsrc1 = cv2.imread('forest.jpg')
src2 = np.zeros(src1.shape, dtype=np.uint8)
src2[:,120:360,:] = 255
dst = cv2.bitwise_xor(src1,src2)
cv2.imshow('Forest',src1)
cv2.imshow('Mask',src2)
cv2.imshow('Forest xor operation', dst)cv2.waitKey(0)
cv2.destroyAllWindows()
图像加密与解密
若C = A xor B
则A = B xor C
将淹没图像B当作一个密钥图像,A于B的xor运算就可以为A图像加密,要解密就让密钥图像与C执行xor运算。
import cv2
import numpy as npsrc = cv2.imread("forest.jpg")
key = np.random.randint(0,255,src.shape,dtype=np.uint8)
print(src.shape)
cv2.imshow('forest',src)
cv2.imshow('key',key)img_encry = cv2.bitwise_xor(src,key)
img_decry = cv2.bitwise_xor(img_encry,key)
cv2.imshow('encrytion', img_encry)
cv2.imshow('decrytion',img_decry)cv2.waitKey(0)
cv2.destroyAllWindows()