函数定义
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):
"""图片平移"""
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_im = cv2.imread(p1)
img_gray = cv2.imread(p1,0)
img_re = reverse_gray(img_gray)
cv2.namedWindow("preview", 0)
cv2.resizeWindow("preview", 960, 720)
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 = 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')
效果图