opencv入门4:图像算术-image arithmetic

RGB的像素值都在[0,255],如果我们想给一个250的像素再加十个像素会怎么样?

NumPy将执行模算术和“环绕”。比如250 再加10像素,会绕回到4,
OpenCV 将执行剪切并确保像素值永远不会超出范围[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].
像素算术arithmetic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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])))

运行结果如下:

像素算术arithmetic.py
1
2
3
4
5
6
7
8
9
10
11
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)
一件事情的毕业,永远是另一件事情的开启.
坚持原创技术分享,您的支持将鼓励我继续创作!