File size: 1,775 Bytes
a103028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy as np

# --------------------------------------------------------------------------------
def scale_depth(depth, img):
    img_sc = np.zeros(img.shape).astype(img.dtype)
    for i in range(img.shape[-1]):
        img_sc[:,:,i] = depth * img[:,:,i]

    return img_sc
# --------------------------------------------------------------------------------


# ---------------------------------------------------------------------------------------------------------------
def blend_fg_bg(bg_img, fg_img, bg_depth, fg_depth):
    # Blends subject and background using lightwraping technique
    depth = bg_depth

    SEG_THRESH = 200
    # ksize = 5
    # kernel = cv2.getStructuringElement(cv2.MORPH_ERODE, (ksize, ksize))
    # depth_erode = cv2.erode(fg_depth.copy(), kernel, iterations=1)

    # Define background border around subject
    # border = fg_depth - depth_erode

    # bg_border = bg_img.copy()
    # bg_border[border < 50] = 0

    # bg_border = cv2.GaussianBlur(bg_border, (5, 5), 1)

    # Add weighted border to to subject
    # added_fg_img = cv2.addWeighted(bg_border, 0.3, fg_img, 0.7, 0)

    fg_matte = fg_depth.astype(np.float64) / 255.
    inv_fg_matte = 1 - fg_matte

    # Scale foreground and background by depth values and then add.
    blended_fg = scale_depth(fg_matte, fg_img)
    blended_bg = scale_depth(inv_fg_matte, bg_img)

    blended = blended_bg + blended_fg
    blended_img = blended.astype(np.uint8)

    # Get total depth by combining both foreground and background depth/seg maps.
    if depth is not None:
        depth[fg_depth > SEG_THRESH] = fg_depth[fg_depth > SEG_THRESH]

    return blended_img, depth
# ---------------------------------------------------------------------------------------------------------------