首先导包:
import numpy as np import cv2 import matplotlib.pyplot as plt def show(image): plt.imshow(image) plt.axis('off') plt.show() def imread(image): image=cv2.imread(image) image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB) return image
rectangle=np.zeros((300,300,3),dtype='uint8') #我们来画一个矩形 white=(255,255,255) cv2.rectangle(rectangle,(25,25),(275,275),white,-1) show(rectangle)
circle=np.zeros((300,300,3),dtype='uint8') cv2.circle(circle,(150,150),150,white,-1) show(circle)
#AND操作,有黑就变黑 image=cv2.bitwise_and(circle,rectangle) show(image)
#or操作,有白就变白 image=cv2.bitwise_or(circle,rectangle) show(image)#貌似按位操作就只有0和1这两种图像的状态,奇怪了
#XOR操作,同种颜色变黑,不同颜色变白 image=cv2.bitwise_xor(circle,rectangle) show(image)#貌似按位操作就只有0和1这两种图像的状态,奇怪了