1. 相同点
都可以实现正常的加法操作。
import numpy as np
import cv2
a = np.array([1], np.uint8)
b = np.array([2], np.uint8)
cv2.add(a, b)
array([[3]], dtype=uint8)
a + b
array([3], dtype=uint8)
2. 不同点
(1)超过当前数据类型所能表示的范围
官方原话是OpenCV addition is a saturated operation while Numpy addition is a modulo operation
即当超出范围时,opencv加法会进行饱和操作,numpy加法会进行取模操作。具体如下。
c = np.array([250], np.uint8)
d = np.array([10], np.uint8)
cv2.add(c, d)
array([[255]], dtype=uint8) # 250+10 = 260 => 255
c + d
array([4], dtype=uint8) # 250+10 = 260 % 256 = 4 (取模就是取余数)
所以做加法时,opencv加法会相对更接近真实值,最好使用opencv加法。或者使用更大数据类型比如uint16, uint32, uint64,这样两种加法都行。
(2)opencv加法功能更强
因为它是一个函数,通过设置其他参数,实现更多功能。重要是mask参数。
. @param src1 数组或者标量.
. @param src2 数组或者标量.
. @param dst output array that has the same size and number of channels as the input array(s); the
. depth is defined by dtype or src1/src2.
. @param mask optional operation mask - 8-bit single channel array, that specifies elements of the
. output array to be changed. 八位单通道数组,指定区域相加,其他区域值为0.
. @param dtype optional depth of the output array (see the discussion below).
通过mask参数,即八位单通道数组,进行指定区域相加,其他区域值为0。