cv.add(src1,src2[,dst[,mask[,dtype]]])
import cv2 as cv
import numpy as np
def sums_add():
img1 = cv.imread('./images/butterfly.jpg')
img2 = cv.imread('./images/lena.jpg')
print('img1',img1[0,100])
print('img2',img2[0,100])
print('加法',img2[0,100] + img1[0,100])
print('np.add',np.add(img2[0,100], img1[0,100]))
print('cv.add',cv.add(img2[0,100], img1[0,100]))
if __name__ == "__main__":
sums_add()
cv.subtract(src1,src2[,dst[,mask[,dtype]]])
import cv2 as cv
import numpy as np
def sums_subtract():
img1 = cv.imread('./images/butterfly.jpg')
img2 = cv.imread('./images/lena.jpg')
print('img1',img1[0,100])
print('img2',img2[0,100])
print('减法',img2[0,100] - img1[0,100])
print('np.subtract',np.subtract(img2[0,100], img1[0,100]))
print('cv.subtract',cv.subtract(img2[0,100], img1[0,100]))
if __name__ == "__main__":
sums_subtract()
cv.multiply(src1,src2[,dst[,scale[,dtype]]])
import cv2 as cv
import numpy as np
def sums_multiply():
img1 = cv.imread('./images/butterfly.jpg')
img2 = cv.imread('./images/lena.jpg')
print('img1',img1[0,100])
print('img2',img2[0,100])
print('乘法',img2[0,100] * img1[0,100])
print('np.multiply',np.multiply(img2[0,100], img1[0,100]))
print('cv.multiply',cv.multiply(img2[0,100], img1[0,100]))
if __name__ == "__main__":
sums_multiply()
cv.divide(src1,src2[,dst[,scale[,dtype]]])
import cv2 as cv
import numpy as np
def sums_divide():
img1 = cv.imread('./images/butterfly.jpg')
img2 = cv.imread('./images/lena.jpg')
print('img1',img1[0,100])
print('img2',img2[0,100])
print('除法',img2[0,100] * img1[0,100])
print('np.divide',np.divide(img2[0,100], img1[0,100]))
print('cv.divide',cv.divide(img2[0,100], img1[0,100]))
if __name__ == "__main__":
sums_divide()
注意:
- OpenCV加法和Numpy加法之间有区别。OpenCV加法是饱和运算,而Numpy加法是模运算。
- 保证不越界,cv.add、cv.subtract、cv.multiply、cv.divide取值范围 [0,255]。
import cv2 as cv
import numpy as np
def sums_image():
image1 = cv.imread("./images/opencv-logo-white.png")
image2 = np.zeros_like(image1)
image2[:] = (110,0,250)
cv.imshow('image1', image1)
cv.imshow('image2', image2)
# 图像像素加法运算
add_img = cv.add(image1, image2)
cv.imshow('add_img', add_img)
# 图像像素减法运算
subtract_img = cv.subtract(image1, image2)
cv.imshow('subtract_img', subtract_img)
# 图像像素乘法运算
multiply_img = cv.multiply(image1, image2)
cv.imshow('multiply_img', multiply_img)
# 图像像素除法运算
divide_img = cv.divide(image1, image2)
cv.imshow('divide_img', divide_img)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
sums_image()
import cv2 as cv
import numpy as np
def numpy_image():
image1 = cv.imread("./images/opencv-logo-white.png")
image2 = np.zeros_like(image1)
image2[:] = (110,0,250)
cv.imshow('image1', image1)
cv.imshow('image2', image2)
# 图像像素加法运算
add_img = np.add(image1, image2)
cv.imshow('add_img', add_img)
# 图像像素减法运算
subtract_img = np.subtract(image1, image2)
cv.imshow('subtract_img', subtract_img)
# 图像像素乘法运算
multiply_img = np.multiply(image1, image2)
cv.imshow('multiply_img', multiply_img)
# 图像像素除法运算
divide_img = np.divide(image1, image2)
cv.imshow('divide_img', divide_img)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
numpy_image()
名称 | 说明 |
---|---|
bool_ | 布尔型数据类型(True 或者 False) |
int_ | 默认的整数类型(类似于 C 语言中的 long,int32 或 int64) |
intc | 与 C 的 int 类型一样,一般是 int32 或 int 64 |
intp | 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64) |
int8 | 字节(-128 to 127) |
int16 | 整数(-32768 to 32767) |
int32 | 整数(-2147483648 to 2147483647) |
int64 | 整数(-9223372036854775808 to 9223372036854775807) |
uint8 | 无符号整数(0 to 255) |
uint16 | 无符号整数(0 to 65535) |
uint32 | 无符号整数(0 to 4294967295) |
uint64 | 无符号整数(0 to 18446744073709551615) |
float_ | float64 类型的简写 |
float16 | 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位 |
float32 | 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位 |
float64 | 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位 |
complex_ | complex128 类型的简写,即 128 位复数 |
complex64 | 复数,表示双 32 位浮点数(实数部分和虚数部分) |
complex128 | 复数,表示双 64 位浮点数(实数部分和虚数部分) |