


如图所示
通过使用两张图加上一个权重 进行等差计算权重
而后使用权重 到另一个图上 无法达到理想的效果
比如平移效果
import cv2
import numpy as np
def gen_one_c(mwb, image1_r, image2_r):
x_one = (image1_r.reshape([image1_r.shape[0], image1_r.shape[1], 1]) + np.zeros([image1_r.shape[1]])).transpose(
[0, 2, 1])
y_one = (image2_r.reshape([image2_r.shape[0], image2_r.shape[1], 1]) + np.zeros([image2_r.shape[1]]))
one_w = (mwb * y_one) / (x_one + 0.0000001)
image2_r_gen = np.array([(i@j.T).tolist() for i, j in zip(image1_r, one_w)])
return one_w, image2_r_gen
def image_to_image(image_path1="1.png",image_path2="2.PNG"):
image1 = cv2.imread(image_path1)
image2 = cv2.imread(image_path2)
image1[image1 == 0] = 1
image2[image2 == 0] = 1
image1_r, image1_g, image1_b = cv2.split(image1)
image2_r, image2_g, image2_b = cv2.split(image2)
wb = np.arange(1, image1_r.shape[-1] + 1)
wb = wb / wb.sum()
wb = wb.reshape([-1, 1]) + np.zeros(wb.size)
mwb = (wb.reshape([wb.shape[0], wb.shape[1], 1]) + np.zeros([image1_r.shape[0]])).transpose([2, 1, 0])
# image1_r=np.random.randint(1,255,image1_r.shape)
# image2_r=np.random.randint(1,255,image2_r.shape)
r_w, r_im = gen_one_c(mwb, image1_r, image2_r)
g_w, g_im = gen_one_c(mwb, image1_g, image2_g)
b_w, b_im = gen_one_c(mwb, image1_b, image2_b)
oimage = cv2.merge([b_im.astype("uint8"), g_im.astype("uint8"), r_im.astype("uint8")])
return oimage ,r_w,b_w,g_w
def image_to_image_by_w(image_path,r_w,b_w,g_w):
image1 = cv2.imread(image_path)
image1[image1 == 0] = 1
image1_r, image1_g, image1_b = cv2.split(image1)
r_im = np.array([(i @ j.T).tolist() for i, j in zip(image1_r, r_w)])
b_im= np.array([(i @ j.T).tolist() for i, j in zip(image1_b, b_w)])
g_im = np.array([(i @ j.T).tolist() for i, j in zip(image1_g, g_w)])
oimage = cv2.merge([b_im.astype("uint8"), g_im.astype("uint8"), r_im.astype("uint8")])
return oimage
if __name__ == '__main__':
# 两张图建模得到权重
oimage,r_w,b_w,g_w=image_to_image()
# 使用权重
oimage1=image_to_image_by_w("3.png", r_w, b_w, g_w)
cv2.imshow("1",oimage)
cv2.imshow("2",oimage1)
cv2.waitKey(1000)
print()