Untitled



In [1]:
import numpy as np
import skimage
import skimage.io
import skimage.morphology
import numba


def to_uint8(img):
    return np.clip(img, 0, 255).astype(np.uint8)


def laplacian(img):
    return (np.roll(img, 1, 0) + np.roll(img, -1, 0) +
            np.roll(img, 1, 1) + np.roll(img, -1, 1) -
            4 * img)


def laplacian_absmax(img1, img2):
    def absmax(a, b):
        return np.where(np.abs(a) > np.abs(b), a, b)
    
    res = np.zeros_like(img1)
    for axis in [0, 1]:
        for delta in [-1, 1]:
            res += absmax(np.roll(img1, delta, axis) - img1,
                          np.roll(img2, delta, axis) - img2)
    return res


@numba.jit
def poisson1(mask, sol, rhs):
    assert sol.shape[:2] == mask.shape[:2] == rhs.shape[:2], 'Dimensions should be equal'
    for i in range(1, sol.shape[0] - 1):
        for j in range(1, sol.shape[1] - 1):
            if mask[i, j]:
                for c in range(3):
                    sol[i, j, c] = (sol[i - 1, j, c] + sol[i + 1, j, c] +
                                        sol[i, j - 1, c] + sol[i, j + 1, c] -
                                        rhs[i, j, c]) / 4
                    
                    
def poisson(n, mask, sol, rhs):
    for i in range(n):
        poisson1(mask, sol, rhs)
    return sol

In [94]:
back = skimage.io.imread('back_4.png').astype(float)[:, :, :3]
fore = skimage.io.imread('fore_4.png').astype(float)[:, :, :3]
mask = skimage.morphology.binary_erosion((fore != 0).any(axis=2), np.ones((3, 3)))

clone = back.copy()
clone[mask] = fore[mask]
skimage.io.imsave('clone.png', to_uint8(clone))

laplace = poisson(100, mask, back.copy(), np.zeros_like(back))
skimage.io.imsave('laplace.png', to_uint8(laplace))

imported = poisson(100, mask, back.copy(), laplacian(fore))
skimage.io.imsave('import.png', to_uint8(imported))

mixed = poisson(100, mask, back.copy(), laplacian_absmax(fore, back))
skimage.io.imsave('mixed.png', to_uint8(mixed))

grayscale_back = skimage.color.gray2rgb(skimage.color.rgb2gray(back)).copy()
grayscale_back[mask] = back[mask]
grayscale = poisson(100, mask, grayscale_back , laplacian(fore))
skimage.io.imsave('gray.png', to_uint8(grayscale))

In [2]:
import numpy as np
import skimage
import skimage.io
import skimage.morphology
import numba

def grad(x):
    return [np.roll(x, 1, 0) - x ,
          np.roll(x, 1, 1) - x]

def laplacian_from_grad(g):
    return(g[0] - np.roll(g[0], -1, 0) +
          g[1] - np.roll(g[1], -1, 1))

In [96]:
import matplotlib.pyplot as plt
%matplotlib inline

In [4]:
#local illum changes
back = skimage.io.imread('back_6.png').astype(float)[:, :, :3]
fore = skimage.io.imread('fore_6.png').astype(float)[:, :, :3]
mask = skimage.morphology.binary_erosion((fore != 0).any(axis=2), np.ones((3, 3)))

eps = 1e-6
img = np.log(back + eps)

img_grad = grad(img)
grad_norms = np.sqrt(np.sum(np.square(img_grad[0]) + np.square(img_grad[1]), axis=2))[:, :,np.newaxis]
#grad_norms = np.sum(np.abs(img_grad[0]) + np.abs(img_grad[1]), axis=2)[:, :,np.newaxis]

avg_grad_norm = np.mean(grad_norms[mask])

beta = 0.2
alpha = 0.2 * avg_grad_norm
grad_target = [alpha**beta * np.abs(grad_norms + eps)**(-beta) * img_grad[0],
               alpha**beta * np.abs(grad_norms + eps)**(-beta) * img_grad[1]]

laplacian_target = laplacian_from_grad(grad_target)
res = img.copy()
 
log_illuminated = poisson(100, mask, res, laplacian_target)
skimage.io.imsave('back_6_illuminated.png', to_uint8(np.exp(log_illuminated) - eps))

In [85]:
for beta in np.arange(0.05, 1, 0.05):
    for a in np.arange(0.05, 1, 0.07):
        alpha = a * avg_grad_norm
        grad_target = [alpha**beta * np.abs(grad_norms + eps)**(-beta) * img_grad[0],
               alpha**beta * np.abs(grad_norms + eps)**(-beta) * img_grad[1]]
        laplacian_target = laplacian_from_grad(grad_target)

        log_illuminated = poisson(100, mask, img.copy(), laplacian_target)
        skimage.io.imsave('zilluminated ' + str(int(100 * a)) + " " + str(int(100 * beta)) + '.png',
                                                                            to_uint8(np.exp(log_illuminated) - eps))

In [19]:
x = np.ones((10,3))
print(x)
print(x * np.ones(10)[:, np.newaxis])


[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]
[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]