RGB的像素值都在[0,255],如果我們想給一個(gè)250的像素再加十個(gè)像素會怎么樣涩哟?
NumPy將執(zhí)行模算術(shù)和“環(huán)繞”叠赦。比如250 再加10像素,會繞回到4奄抽,
OpenCV 將執(zhí)行剪切并確保像素值永遠(yuǎn)不會超出范圍[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])))
M =np.ones(image.shape, dtype="uint8")*100
# defines a NumPy array of ones, with the same
# size as our image.
#為了用100的值而不是1來填充我們的矩陣看蚜,我們簡單地把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)是另一件事情的開啟.
更多文章請關(guān)注我的博客:https://harveyyeung.github.io