opencv入門(mén)4:圖像算術(shù)-image arithmetic

RGB的像素值都在[0,255],如果我們想給一個(gè)250的像素再加十個(gè)像素會(huì)怎么樣?

NumPy將執(zhí)行模算術(shù)和“環(huán)繞”。比如250 再加10像素,會(huì)繞回到4,
OpenCV 將執(zhí)行剪切并確保像素值永遠(yuǎn)不會(huì)超出范圍[0,255]

NumPy will perform modulo arithmetic and “wrap around”.
OpenCV, on the other hand, will perform clipping and ensure pixel values never fall outside the range [0, 255].

from __future__ import print_function
import numpy as np 
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required =True, help="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original",image)

print("max of 255:{}".format(cv2.add(np.uint8([200]),np.uint8([100]))))
print("min of 0:{}".format(cv2.subtract(np.uint8([50]),np.uint8([100]))))

print("wrap around :{}".format(np.uint8([200])+np.uint8([100])))
print("wrap around :{}".format(np.uint8([50])-np.uint8([100])))

運(yùn)行結(jié)果如下:

M =np.ones(image.shape, dtype="uint8")*100
# defines a NumPy array of ones, with the same
# size as our image.
#為了用100的值而不是1來(lái)填充我們的矩陣,我們簡(jiǎn)單地把1的矩陣乘以100。
added = cv2.add(image,M)
cv2.imshow("Added",added)

M = np.ones(image.shape,dtype ="uint8")*50
subtracted = cv2.subtract(image,M)
cv2.imshow("Subtraced",subtracted)
cv2.waitKey(0)
圖·2

一件事情的畢業(yè),永遠(yuǎn)是另一件事情的開(kāi)啟.
更多文章請(qǐng)關(guān)注我的博客:https://harveyyeung.github.io

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容