python + opencv 图像处理(六)——像素运算

对图像进行“加减乘除、平均值方差、逻辑运算、调节对比度亮度”。

# 像素运算
from cv2 import cv2 as cv
import numpy as np 

# 加
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 others(m1,m2):
    # M1 = cv.mean(m1)  #算均值
    # M2 = cv.mean(m2)
    M1,dev1 = cv.meanStdDev(m1)  #算均值和方差
    M2,dev2 = cv.meanStdDev(m2)
    print(M1)
    print(M2)
    print('dev1:',dev1)
    print('dev2:',dev2)

#逻辑运算    
def logic_demo(m1,m2):
    dst = cv.bitwise_not(m1,m2)
    dst = cv.bitwise_or(m1,m2)
    dst = cv.bitwise_and(m1,m2)
    cv.imshow('logic_demo',dst)

def contrast_brightness_demo(image,c,b):  # 图像,对比度,亮度
    h,w,ch = image.shape   # 得到宽和高以及通道
    blank = np.zeros([h,w,ch],image.dtype)
    dst = cv.addWeighted(image,c,blank,1-c,b)
    cv.imshow('con_bri_demo',dst)

filepath1 = "C:\\pictures\\0.jpg"   #两张一样大小的图 #并不是指存储大小,而是长宽通道
filepath2 = "c:\\pictures\\1.png"
img1 = cv.imread(filepath1)
img2 = cv.imread(filepath2)
print(img1.shape)
print(img2.shape)
cv.imshow("input image1",img1)
cv.imshow("input image2",img2)
# video_demo()
'''
add_demo(img1,img2)
subtract_demo(img1,img2)
divide_demo(img1,img2)
multiply_demo(img1,img2)

'''
others(img1,img2)
cv.waitKey(0)
cv.destroyAllWindows()

你可能感兴趣的:(opencv)