OpenCV实现图像的运算
图像的加法运算
import cv2
import numpy as np
people = cv2.imread('1.png')
print(people.shape)
img = np.ones((132, 96, 3),np.uint8) * 50
cv2.imshow('people',people)
result = cv2.add(people,img)
cv2.imshow('result',result)
cv2.waitKey(0)
图像的减法、乘法、除法运算
result = cv2.subtract(people,img)
- multiply(A,B)
- divide(A,B)
图像溶合
- addWeighted(A,alpha,B,bate,gamma)
- alpha,bate是权重,占比,总和为1
- gamma是静态权重,将生产图片的所有元素都加上gamma值
- 同时也要求AB两张图的属性一样才能溶合
result = cv2.addWeighted(people,0.7,img,0.3,0)
图像位运算
import cv2
import numpy as np
img = np.zeros((200,200),np.uint8)
img[50:150,50:150] = 255
new_img = cv2.bitwise_not(img)
cv2.imshow('img',img)
cv2.imshow('new_img',new_img)
cv2.waitKey(0)
- 与运算:bitwise_and(img1,img2)
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)
- 或操作:bitwise_or(img1,img2)
- 异或操作:bitwise_xor(img1,img2)
new_img = cv2.bitwise_or(img,img2)
new_img = cv2.bitwise_xor(img,img2)