opencv入门5:按位操作-bitwise operation
2018-01-29 本文已影响26人
HaveyYeung
现在我们将回顾四个按位运算:AND,OR,XOR和NOT。这四个操作虽然非常基础和低级,但对于图像处理至关重要,特别是当我们在6节开始使用蒙版时。
按位操作以二进制方式操作,并以灰度图像表示:
import numpy as np
import cv2
rectangle = np.zeros((300,300),dtype="uint8")
cv2.rectangle(rectangle,(25,25),(275,275),255,-1)
cv2.imshow("Rectangle",rectangle)
circle = np.zeros((300,300),dtype="uint8")
cv2.circle(circle,(150,150),150,255,-1)
cv2.imshow("Circle",circle)
cv2.waitKey(0)
bitwiseAnd = cv2.bitwise_and(rectangle,circle)
cv2.imshow("And",bitwiseAnd)
cv2.waitKey(0)
bitwiseOr = cv2.bitwise_or(rectangle,circle)
cv2.imshow("OR",bitwiseOr)
cv2.waitKey(0)
bitwiseXor = cv2.bitwise_xor(rectangle,circle)
cv2.imshow("XOR",bitwiseXor)
cv2.waitKey(0)
bitwiseNot = cv2.bitwise_not(rectangle)
cv2.imshow("Not",bitwiseNot)
cv2.waitKey(0)
1. AND: A bitwise AND is true if and only if both pixels
are greater than zero.
2. OR: A bitwise OR is true if either of the two pixels
are greater than zero.
3. XOR: A bitwise XOR is true if and only if either of the
two pixels are greater than zero, but not both.
4. NOT: A bitwise NOT inverts the “on” and “off” pixels
in an image.
运行结果如下:





如果一个给定的像素的值大于零,那么这个像素会被打开,如果它的值为零,它就会被关闭。按位功能在这些二进制条件下运行。
1. AND:当且仅当两个像素都大于零时,按位AND才为真。
2. OR:如果两个像素中的任何一个大于零,则按位“或”为真。
3. XOR 异或功能:当且仅当两个像素中的任何一个大于零时,按位XOR才为真,但不是两者都是。当且仅当两个像素一个大于0一个小于0时才为真,其他都为false
4. NOT 取反:倒置图像中的“开”和“关”像素。
来是偶然,去是必然,尽其当然,顺其自然。
更多文章请关注我的博客:https://harveyyeung.github.io