一、函数简介
1、add—图像矩阵相加
函数原型:cv2.add(src1, src2, dst=None, mask=None, dtype=None)
src1:图像矩阵1
src1:图像矩阵2
dst:默认选项
mask:默认选项
dtype:默认选项
2、subtract—图像矩阵相减
函数原型:cv2.subtract(src1, src2, dst=None, mask=None, dtype=None)
src1:图像矩阵1
src1:图像矩阵2
dst:默认选项
mask:默认选项
dtype:默认选项
3、bitwise_and—图像与运算
函数原型:cv2.bitwise_and(src1, src2, dst=None, mask=None)
src1:图像矩阵1
src1:图像矩阵2
dst:默认选项
mask:默认选项
4、bitwise_or—图像或运算
函数原型:cv2.bitwise_or(src1, src2, dst=None, mask=None)
src1:图像矩阵1
src1:图像矩阵2
dst:默认选项
mask:默认选项
5、bitwise_xor—图像异或运算
函数原型:bitwise_xor(src1, src2, dst=None, mask=None)
src1:图像矩阵1
src1:图像矩阵2
dst:默认选项
mask:默认选项
6、bitwise_not—图像非运算
函数原型:bitwise_not(src1, src2, dst=None, mask=None)
src1:图像矩阵1
src1:图像矩阵2
dst:默认选项
mask:默认选项
二、实例演示
1、原图像每个像素都加100,大于255的按255处理
#原始图像每个像素都加100, 大于255的按255处理 import cv2 import numpy as np img = cv2.imread("test.png") cv2.imshow("Original", img) cv2.waitKey(0) #图像img各像素加100 M = np.ones(img.shape, dtype=‘uint8‘)*100#与img大小一样的全100矩阵 added = cv2.add(img, M)#将图像image与M相加 cv2.imshow("Added", added) cv2.waitKey(0)
效果如下图所示:
原图:
2、原图像每个像素都减去50,小于0的按0处理
#原图像每个像素都减去50, 小于0的按0处理 import cv2 import numpy as np image = cv2.imread(‘test.png‘) cv2.imshow("Orignal", image) cv2.waitKey(0) #图像image各像素减去50 M = np.ones(image.shape, dtype="uint8")*50 subtracted = cv2.subtract(image, M) cv2.imshow("Subtracted", subtracted) cv2.waitKey(0)
效果如图所示:
3、矩形与圆形的交运算
#矩形与圆形的交运算 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) cv2.waitKey(0) #画圆形 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)
效果如下所示:
4、矩形与圆形的或运算
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) cv2.waitKey(0) #画圆形 Circle = np.zeros((300, 300), dtype=‘uint8‘) cv2.circle(Circle, (150, 150), 150, 255, -1) cv2.imshow("Circle", Circle) cv2.waitKey(0) #图形的或 bitwiseor = cv2.bitwise_or(Rectangle, Circle) cv2.imshow("OR", bitwiseor) cv2.waitKey(0)
效果如图所示:
5、矩形与圆形的异或运算
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) cv2.waitKey(0) #画圆形 Circle = np.zeros((300, 300), dtype=‘uint8‘) cv2.circle(Circle, (150, 150), 150, 255, -1) cv2.imshow("Circle", Circle) cv2.waitKey(0) #图像的异或 bitwisexor = cv2.bitwise_xor(Rectangle, Circle) cv2.imshow("XOR", bitwisexor) cv2.waitKey(0)
效果如图所示:
6、圆形的非运算
import numpy as np import cv2 #画圆形 Circle = np.zeros((300, 300), dtype=‘uint8‘) cv2.circle(Circle, (150, 150), 150, 255, -1) cv2.imshow("Circle", Circle) cv2.waitKey(0) #圆形的非运算 bitwisenot = cv2.bitwise_not(Circle) cv2.imshow("NOT", bitwisenot) cv2.waitKey(0)
效果如图所示:
原文地址:https://www.cnblogs.com/leoych/p/12106145.html