import cv2 as cv
import numpy as np
# 数值运算:加减乘除
def shu_image(srcA,srcB):
src = cv.add(srcA,srcB) # 加
cv.imshow('add',src)
src = cv.subtract(srcA,srcB) # 减
cv.imshow('subtract',src)
src = cv.multiply(srcA,srcB) # 乘
cv.imshow('multiply',src)
src = cv.divide(srcA,srcB) # 除
cv.imshow('divide',src)
# 其他运算 求均值、方差
def others(m1,m2):
M1,dev1 = cv.meanStdDev(m1) # M1是均值,dev1是方差
M2,dev2 = cv.meanStdDev(m2)
h,w = m1.shape[:2]
print(M1)
print(M2)
print('----上面是均值;下面是方差')
print(dev1)
print(dev2)
# 逻辑运算:与或非的操作
def luo_image(srcA,srcB):
src = cv.bitwise_and(srcA,srcB) # 与 两张图片同一位置的色素两个值均不为零的才会有输出
cv.imshow('and',src)
src = cv.bitwise_or(srcA,srcB) # 或 两张图片同一位置的色素两个值不全为零的才会有输出
cv.imshow('or',src)
# 粗略的调节对比度和亮度 a是对比度 g是亮度
def contrast_brightness_image(src1,a,g):
h,w,ch = src1.shape # 获取shape的数值,height和width、通道
# 新建全零图片数组src2,将height和width,类型设置为原图片的通道类型(色素全为零,输出为全黑图片)
src2 = np.zeros([h,w,ch],src1.dtype)
dst = cv.addWeighted(src1,a,src2,1-a,g) # addWeighted函数说明如下
cv.imshow("con-bri-demo", dst)
if __name__ == '__mian__':
"""
数值运算shu_image()部分
"""
src1 = cv.imread('pic/bizhi.jpg')
cv.imshow('original1',src1)
# cv.waitKey(0)
# cv.destroyAllWindows()
"""
其他运算others()部分
"""
src2 = cv.imread('pic/bizhi.jpg')
src3 = cv.imread('pic/bizhi.jpg')
others(src2,src3)
'''
逻辑运算luo_image()部分
'''
luo_image(src2,src3)
'''
调节亮度和对比度contrast_brightness_image()
'''
contrast_brightness_image(src1,1,10)
cv.waitKey()
cv.destroyAllWindows()