opencv入门5:按位操作-bitwise operation

作者: HaveyYeung | 来源:发表于2018-01-29 15:32 被阅读26次

现在我们将回顾四个按位运算: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.

运行结果如下:


image image image image image

如果一个给定的像素的值大于零,那么这个像素会被打开,如果它的值为零,它就会被关闭。按位功能在这些二进制条件下运行。
1. AND:当且仅当两个像素都大于零时,按位AND才为真。
2. OR:如果两个像素中的任何一个大于零,则按位“或”为真。
3. XOR 异或功能:当且仅当两个像素中的任何一个大于零时,按位XOR才为真,但不是两者都是。当且仅当两个像素一个大于0一个小于0时才为真,其他都为false
4. NOT 取反:倒置图像中的“开”和“关”像素。

来是偶然,去是必然,尽其当然,顺其自然。
更多文章请关注我的博客:https://harveyyeung.github.io

相关文章

  • opencv入门5:按位操作-bitwise operation

    现在我们将回顾四个按位运算:AND,OR,XOR和NOT。这四个操作虽然非常基础和低级,但对于图像处理至关重要,特...

  • 按位与运算获取图像重要的部分---OpenCV-Python开发

    常见的按位逻辑运算 在OpenCV内,常见的按位运算函数如下表所示: 函数名含义bitwise_and()按位与b...

  • 按位操作符

    按位操作符(Bitwise operators)将其操作数(operands)当作 32 位的比特序列(由 0 和...

  • 位运算

    概述 按位操作符(Bitwise operators) 将其操作数(operands)当作32位的比特序列(由0和...

  • JS按位操作符

    按位操作符(Bitwise operators) 将其操作数(operands)当作32位的比特序列(由0和1组成...

  • Bitwise Operation

    Bitwise operator in C/C++ 歡迎來到二進位的世界。電腦資料都是以二進位儲存,想當然程式語言...

  • [JS] 有符号整数的位操作

    1. 32位有符号整数 按位操作符(Bitwise operators)会使用内置函数,7.1.5 ToInt32...

  • 位运算详解

    位运算 Bitwise operation 前言 日常提出疑问,然后引出今天的下文: 如何在代码里不用 “+” 、...

  • C语言-按位逻辑运算符和位移运算符

    按位逻辑运算符 按位与运算符(bitwise AND operator) a & b 按位计算a和b的逻辑与; 按...

  • LeetCode 201-210

    201. 数字范围按位与[https://leetcode-cn.com/problems/bitwise-and...

网友评论

    本文标题:opencv入门5:按位操作-bitwise operation

    本文链接:https://www.haomeiwen.com/subject/ulgxzxtx.html