Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
import matplotlib.pyplot as plt
def hard_composite(A, B):
alpha_A = A[:, :, 3:4]
alpha_B = B[:, :, 3:4]
# A over B
final_composite = A * alpha_A + B * alpha_B * (1 - alpha_A)
return final_composite
def soft_composite(A, B, z_A, z_B):
# gradients can be propagated through z_A, z_B
# how z_A, z_B change decides the visibility of the elments in overlapping regions
alpha_A = A[:, :, 3:4]
alpha_B = B[:, :, 3:4]
inv_mask = (1 - alpha_A) * (1 - alpha_B)
e_z_A = np.exp(z_A)
e_z_B = np.exp(z_B)
sum_alpha = alpha_A * e_z_A + alpha_B * e_z_B + inv_mask
new_alpha_A = alpha_A * e_z_A/sum_alpha
new_alpha_B = alpha_B * e_z_B/sum_alpha
final_composite = A * new_alpha_A + B * new_alpha_B
return final_composite
img_1 = plt.imread('1.png')
img_2 = plt.imread('2.png')
hard_1_over_2 = hard_composite(img_1, img_2)
hard_2_over_1 = hard_composite(img_2, img_1)
soft_zA5_zB3 = soft_composite(img_1, img_2, 5, 3)
soft_zA5_zB5 = soft_composite(img_1, img_2, 5, 5)
soft_zA5_zB7 = soft_composite(img_1, img_2, 5, 7)
soft_zA5_zB10 = soft_composite(img_1, img_2, 5, 10)
plt.imsave('hard_1_over_2.png', hard_1_over_2)
plt.imsave('hard_2_over_1.png', hard_2_over_1)
plt.imsave('soft_zA5_zB3.png', soft_zA5_zB3)
plt.imsave('soft_zA5_zB5.png', soft_zA5_zB5)
plt.imsave('soft_zA5_zB7.png', soft_zA5_zB7)
plt.imsave('soft_zA5_zB10.png', soft_zA5_zB10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment