程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[opencv numpy] saturation operation in Python opencv and modular operation in numpy (unsaturated operation)

編輯:Python

One 、 Preface

stay OpenCV Medium “ Image operation ” Official guide document in , There is such a note

There is a difference between OpenCV addition and Numpy addition. OpenCV addition is a saturated operation while Numpy addition is a modulo operation.

OpenCV and Numpy The addition operation of is different ,OpenCV The addition operation of is saturation operation , and Numpy The addition operation of is modulo operation

Two 、 So what is saturation operation 、 Modular operation ? 

Everyone must have seen something similar sat(a+b) The expression of , In fact, this is saturation operation ; And modular operation is generally used directly a+b To represent the .

The biggest feature of saturation operation is not paying attention to overflow , The execution result has little to do with the bottom ; Suppose the type of variable is 8 Bit unsigned integer , Then the maximum number is 255, If the result of adding is greater than 255, Then the result of saturation operation is 255.

Modulo operations consider overflow bits , The execution results need to be explained to the underlying principles of the computer —— Thinking about binary ; Suppose the type of variable is 8 Bit unsigned integer , Then the maximum number is 255( Corresponding binary 11111111), If the result of adding is greater than 255, Then you have an overflow , Only binary bits within the scope of the container are reserved .

3、 ... and 、 Test code

Why? np.add() The return value of 0? because 255+1=256, Corresponding binary bit 1[00000000], The first superfluous 1 Belong to overflow position , After removal, it is [00000000], Corresponding to decimal integers 0.

import cv2 as cv
import numpy as np
a = np.array([[255]], dtype=np.uint8)
print(cv.add(a, 1))
print(np.add(a, 1))
# [[255]]
# [[0]]


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved