File size: 2,962 Bytes
4f6789c
 
 
 
 
 
 
 
 
74f19b4
 
 
3187496
74f19b4
3187496
 
 
 
4f6789c
 
 
 
 
 
74f19b4
 
 
 
4f6789c
 
 
 
 
74f19b4
 
 
 
4f6789c
 
 
 
 
74f19b4
 
 
 
 
4f6789c
 
 
 
 
 
 
3187496
 
 
 
 
4f6789c
3187496
 
4f6789c
 
 
 
3187496
 
4f6789c
 
ebfe618
4f6789c
 
 
 
 
 
 
 
 
 
 
3ffc2f6
a1ddba3
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# "Production code"
import numpy as np
from interactive_pipe import Curve, SingleCurve

# -----------------
# Processing blocks
# -----------------


def gen_color(
    frequency: int = 0,  # discrete slider (int)
    isotropy: float = 0.,  # continuous slider (float)
    context={}  # global parameters (dict)
) -> np.ndarray:
    # context allows sharing information between processing blocks.
    # Let's share the frequency value for further use.
    context["freq"] = frequency
    print(context.get("initialized_param", "Wrongly initialized context!"))
    lin_coord = np.linspace(0, 1., 256)
    X, Y = np.meshgrid(lin_coord, isotropy*lin_coord)
    radius = 0.5+0.5*np.cos(frequency*np.sqrt(X**2 + Y**2))
    return np.stack([np.abs(X), np.abs(Y), radius], axis=-1).clip(0, 1)


def modify_geometry(
    img: np.ndarray,
    effect: str = "flip"  # dropdown menus (str)
) -> np.ndarray:
    img = img[::-1] if "flip" in effect else img
    img = img[:, ::-1] if "mirror" in effect else img
    return img


def change_color(
    img: np.ndarray,
    bnw: bool = True  # checkboxes (bool)
):
    if bnw:
        return np.mean(img, axis=-1, keepdims=True).repeat(3, axis=-1)
    return img


def compare_by_splitting(
    img_1: np.ndarray,
    img_2: np.ndarray,
    ratio: float = 0.5,  # continuous slider (float)
) -> np.ndarray:
    out = np.zeros_like(img_1)
    split = int(ratio*img_1.shape[1])
    out[:, :split] = img_2[:, :split]
    out[:, split+5:] = img_1[:, split+5:]
    return out


def extract_profile(img: np.ndarray, context={}) -> Curve:
    # context allows sharing information between processing blocks
    frequency = context.get('freq', 0)
    init_shared_param = context.get(
        'initialized_param', "Wrongly initialized context!")
    luma = img.mean(axis=-1)
    h_profile = SingleCurve(
        y=luma[0, :], label=f"H profile f={frequency}")
    v_profile = SingleCurve(y=luma[:, 0], label="V profile")
    return Curve(
        [h_profile, v_profile],
        xlabel="Position", ylabel="Luminance",
        grid=True,
        title=f"Luminance profiles\n(global context parameter {init_shared_param})"
    )


# -------------------
# Pipeline definition
# -------------------


def tutorial_pipeline():
    inp = gen_color()
    out_geometry = modify_geometry(inp)
    out_bnw = change_color(inp)
    out_image = compare_by_splitting(out_geometry, out_bnw)
    out_profile = extract_profile(out_image)
    return [[inp, out_geometry], [out_profile, out_image]]

# -----------------------------------------------------------------------------------------
# Other layout you can try! None will create an empty space.
# return [[inp, out_geometry], [out_profile, out_image], [out_bnw, inp]]
# return [out_profile, out_image, out_bnw, inp]
# return out_profile, out_image, out_bnw, inp, None
# return [[inp, out_geometry], [out_profile, out_image], [out_bnw, None], [None, inp]]
# return inp
# return [[out_image], [inp]]