opencv图像运算

import cv2 as cv
import numpy as np


def add(img1,img2):
    '''图片相加'''
    dst = cv.add(img1,img2)
    cv.imshow("add",dst)

def sub(img1,img2):
    '''图片相减'''
    dst = cv.subtract(img1,img2)
    cv.imshow("subtract",dst)

def divide(img1,img2):
    '''图片相除'''
    dst = cv.divide(img1,img2)
    cv.imshow("divide",dst)
def multiply(img1,img2):
    dst = cv.multiply(img1,img2)
    cv.imshow("multiply",dst)

def logic(img1,img2):
    '''逻辑运算'''
    dst = cv.bitwise_or(img1,img2)
    cv.imshow("bitwise_or", dst)
    dst = cv.bitwise_and(img1,img2)
    cv.imshow("bitwise_and", dst)
    dst = cv.bitwise_not(img1, img2)
    cv.imshow("bitwise_not", dst)

def contrast_brightness(img,alpha,beta):
    '''
    亮度和对比度调节
    alpha: 对比度
    beta: 亮度
    '''

    '''
    height = img.shape[0]
    weight = img.shape[1]
    for  col in range (height):
        for row in range (weight):
            b = img[col,row,0]
            g = img[col,row,1]
            r = img[col,row,2]
            #这里需要注意的alpha * b + beta要在0~255,我这里没有注意
            img[col,row,0] = alpha * b + beta
            img[col,row,1] = alpha * g + beta
            img[col,row,2] = alpha * r + beta 
    cv.imshow("contrast_brightness ",img)
    '''
    #获取图片的高、宽、通道
    h,w,ch = img.shape
    #创建一张全黑的图片
    blank = np.zeros([h,w,ch],img.dtype)
    dst = cv.addWeighted(img,alpha,blank,1-alpha,beta)
    cv.imshow("contrast_brightness ", dst)

gakki = cv.imread("/Users/reon/Code/Python/OpenCv/gakki.jpg")
masami = cv.imread("/Users/reon/Code/Python/OpenCv/masami.jpg")
#split 函数分离通道号
gakki_B,gakki_G,gakki_R = cv.split(gakki)
masami_B,masami_G,gakki_R = cv.split(masami)
#将RGB 转换成 GRAY
gakki_gray = cv.cvtColor(gakki,cv.COLOR_RGB2GRAY)
masami_gray = cv.cvtColor(masami,cv.COLOR_RGB2GRAY)
#masami_dst = np.zeros([400,400,3],np.uint8)
#将两张图片设置成shape一样大小

gakki_dst = cv.resize(gakki_gray,(600,600))
masami_dst = cv.resize(masami_gray,(600,600))
cv.namedWindow("pic",cv.WINDOW_AUTOSIZE)
#cv.imshow("gakki",gakki)
cv.imshow("masami",masami)
if gakki_dst.shape[0] == masami_dst.shape[0] and gakki_dst.shape[1] == masami_dst.shape[1] :
    # add(gakki_dst,masami_dst)
    # sub(gakki_dst,masami_dst)
    # divide(gakki_dst,masami_dst)
    # multiply(gakki_dst,masami_dst)
     logic(gakki_dst,masami_dst)
    #logic(gakki,masami) 这个是错误的 两个图片必须shape一摸一一样
contrast_brightness(masami,0.8,30)
cv.waitKey(0)

你可能感兴趣的:(opencv图像运算)