Python OpenCV第十二课:图片按位操作

可以对图片按位操作,包括和AND、与OR、非NOT和异或XOR。当要提取图像的任何部分、定义和使用非矩形ROI等时,它们将非常有用。下面我们将看到一个如何更改图片特定区域的示例。看完整代码:

import cv2
import numpy as np

img1 = np.zeros((300, 300), dtype="uint8")
cv2.rectangle(img1, (100, 100), (250, 250), 255, -1)
cv2.imshow("Image 1", img1)

img2 = np.zeros((300, 300), dtype="uint8")
cv2.circle(img2, (150, 150), 90, 255, -1)
cv2.imshow("Image 2", img2)

rect_and_circle = cv2.bitwise_and(img1, img2)
cv2.imshow("AND operation", rect_and_circle)

rect_or_circle = cv2.bitwise_or(img1, img2)
cv2.imshow("OR operation", rect_or_circle)

rect_xor_circle = cv2.bitwise_xor(img1, img2)
cv2.imshow("XOR Operation", rect_xor_circle)

rect_xor_circle2 = cv2.bitwise_xor(img1, img2)
cv2.imshow("XOR Operation", rect_xor_circle2)

cv2.waitKey(0)
cv2.destroyAllWindows()
Image 1是正方形,Image 2是圆形。其它几个图片是执行 AND, OR, NOT 和 XOR 的结果,看效果:

Python OpenCV第十二课:图片按位操作_第1张图片

你可能感兴趣的:(#,OpenCV,opencv)