Python图像处理---RGB灰度通道比例叠加

函数定义
# 动态调节比例
import cv2
import numpy as np

def reverse_gray(img):
    """灰度反转"""
    max1 = img.max()
    img1 = img* (-1)
    img2 = img1 + max1
    return img2

def RGB_add(RGB_img,img,a,b,c):
    """
    RGB_img:输入的RGB图片
    INR_img:待叠加的灰度图片
        a  :R通道叠加比例,近红外为1-a
        b  :G通道叠加比例,近红外为1-b
        c  :B通道叠加比例,近红外为1-c
    """
    rgb_new = np.zeros(RGB_img.shape,np.uint8)
    rgb_new_r = RGB_img[:,:,0] * a + (1-a)*img 
    rgb_new_r = rgb_new_r.astype(np.uint8)
    rgb_new_g = RGB_img[:,:,1] * b + (1-b)*img
    rgb_new_g = rgb_new_g.astype(np.uint8)
    rgb_new_b = RGB_img[:,:,2] * c + (1-c)*img
    rgb_new_b = rgb_new_b.astype(np.uint8)
    rgb_new[:,:,0] = rgb_new_r
    rgb_new[:,:,1] = rgb_new_g
    rgb_new[:,:,2] = rgb_new_b
    return rgb_new

def Move(img,delta_x,delta_y):
    """图片平移"""
    #delta_x>0左移,delta_x<0右移
    #delta_y>0上移,delta_y<0下移
    shape = img.shape
    rows = shape[0]
    cols = shape[1]
    transform = np.float32([[1,0,delta_x],[0,1,delta_y]])
    rgb_move = cv2.warpAffine(img,transform,(cols,rows))
    return rgb_move

def set_a(x):
    pass

    print(a)
    
def set_b(x):
    pass

    
def set_c(x):
    pass


示例
p1 = './Simulation-Result-on-Lena.png'

# 读取RGB
rgb_im = cv2.imread(p1)
# 读取灰度,这里的RGB和灰度可以是不同的图片,示例里面用的是同一张
img_gray = cv2.imread(p1,0)
# 灰度反转
img_re = reverse_gray(img_gray)

# 初始化窗口
cv2.namedWindow("preview", 0)
cv2.resizeWindow("preview", 960, 720)
# 初始化滑动条,0-10指定调节范围,set_a为回调函数(一旦滑动条改变就会触发执行回调函数)
cv2.createTrackbar('a', 'preview', 0, 10, set_a)
cv2.createTrackbar('b', 'preview', 0, 10, set_b)
cv2.createTrackbar('c', 'preview', 0, 10, set_c)
while True:    
	# 获取a滑动条的数值
    a = cv2.getTrackbarPos('a', 'preview')
    a = a/10
    b = cv2.getTrackbarPos('b', 'preview')
    b = b/10
    c = cv2.getTrackbarPos('c', 'preview')
    c = c/10
    img_blen = RGB_add(rgb_im,img_re,a,b,c)
    cv2.imshow('preview',cv2.cvtColor(img_blen,cv2.COLOR_RGB2BGR))
    key = cv2.waitKey(1)
    if key ==ord("q"):     
        cv2.destroyAllWindows()
        break
print('DONE')
效果图

Python图像处理---RGB灰度通道比例叠加_第1张图片

你可能感兴趣的:(Python,opencv,python,图像处理,开发语言)