opencv加法和和numpy加法的比较-04

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。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值