图像算数运算的部分实例代码:
import cv2 as cv #两张图片相加 def add_demo(m1,m2): dst = cv.add(m1,m2) cv.imshow("add_demo",dst) #两张图片相减 def subtract_demo(m1,m2): dst = cv.subtract(m1,m2) cv.imshow("subtract_demo",dst) #两张图片相除 def divide_demo(m1,m2): dst = cv.divide(m1,m2) cv.imshow("divide_demo",dst) #两张图片相乘 def multiply_demo(m1,m2): dst = cv.multiply(m1, m2) cv.imshow("multiply_demo", dst) #图片各通道均值 def mean_demo(m1,m2): M1 = cv.mean(m1) M2 = cv.mean(m2) print(M1) print(M2) src1 = cv.imread("timg.jpg") src2 = cv.imread("timg2.jpg") cv.imshow("timg",src1) add_demo(src1,src2) subtract_demo(src1,src2) divide_demo(src1,src2) multiply_demo(src1,src2) mean_demo(src1,src2) cv.waitKey(0) cv.destroyAllWindows()
注意:两张图片的大小要相同,否则会cv2会报错!