gen_image.py 3.33 KB
Newer Older
BobYeah's avatar
BobYeah committed
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
94
95
96
97
98
99
100
101
102
103
104
105
import matplotlib.pyplot as plt
import numpy as np
import torch

def RandomGenSamplesInPupil(conf, n_samples):
    '''
    Random sample n_samples positions in pupil region
    
    Parameters
    --------
    conf      - multi-layers' parameters configuration
    n_samples - number of samples to generate
    
    Returns
    --------
    a n_samples x 2 tensor with 2D sample position in each row
    '''
    samples = torch.empty(n_samples, 2)
    i = 0
    while i < n_samples:
        s = (torch.rand(2) - 0.5) * conf.pupil_size
        if np.linalg.norm(s) > conf.pupil_size / 2.:
            continue
        samples[i, :] = s
        i += 1
    return samples

def GenSamplesInPupil(conf, circles):
    '''
    Sample positions on circles in pupil region
    
    Parameters
    --------
    conf      - multi-layers' parameters configuration
    circles   - number of circles to sample
    
    Returns
    --------
    a n_samples x 2 tensor with 2D sample position in each row
    '''
    samples = torch.tensor([[ 0., 0. ]])
    for i in range(1, circles):
        r = conf.pupil_size / 2. / (circles - 1) * i
        n = 4 * i
        for j in range(0, n):
            angle = 2 * np.pi / n * j
            samples = torch.cat((samples, torch.tensor([[ r * np.cos(angle), r * np.sin(angle)]])),dim=0)
    return samples

def GenRetinal2LayerMappings(conf, df, v, u):
    '''
    Generate the mapping matrix from retinal to layers.
    
    Parameters
    --------
    conf - multi-layers' parameters configuration
    df   - focal distance
    v    - a 1 x 2 tensor stores half viewport
    u    - a M x 2 tensor stores M sample positions on pupil
    
    Returns
    --------
    The mapping matrix
    '''
    H_r = conf.retinal_res[0]
    W_r = conf.retinal_res[1]
    D_r = conf.retinal_res.double()
    N = conf.n_layers
    M = u.size()[0] #41
    Phi = torch.empty(H_r, W_r, N, M, 2, dtype=torch.long)
    p_rx, p_ry = torch.meshgrid(torch.tensor(range(0, H_r)),
                                torch.tensor(range(0, W_r)))
    p_r = torch.stack([p_rx, p_ry], 2).unsqueeze(2).expand(-1, -1, M, -1)
    # print(p_r.shape) #torch.Size([480, 640, 41, 2])
    for i in range(0, N):
        dpi = conf.h_layer[i] / conf.layer_res[0] # 1 / 480
        ci = conf.layer_res / 2 # [240,320]
        di = conf.d_layer[i] # 深度
        pi_r = di * v * (1. / D_r * (p_r + 0.5) - 0.5) / dpi # [480, 640, 41, 2]
        wi = (1 - di / df) / dpi # (1 - 深度/聚焦) / dpi  df = 2.625 di = 1.75
        pi = torch.floor(pi_r + ci + wi * u)
        torch.clamp_(pi[:, :, :, 0], 0, conf.layer_res[0] - 1)
        torch.clamp_(pi[:, :, :, 1], 0, conf.layer_res[1] - 1)
        Phi[:, :, i, :, :] = pi
    return Phi

def GenRetinalFromLayers(layers, Phi):
    # layers:  2, color, height, width 
    # Phi:torch.Size([480, 640, 2, 41, 2])
    M = Phi.size()[3] # 41
    N = Phi.size()[2] # 2
    # print(layers.shape)# torch.Size([2, 3, 480, 640])
    # print(Phi.shape)# torch.Size([480, 640, 2, 41, 2])
    # retinal image: 3channels x retinal_size
    retinal = torch.zeros(3, Phi.size()[0], Phi.size()[1])
    for j in range(0, M):
        retinal_view = torch.zeros(3, Phi.size()[0], Phi.size()[1])
        for i in range(0, N):
            retinal_view.add_(layers[i,:, Phi[:, :, i, j, 0], Phi[:, :, i, j, 1]])
        retinal.add_(retinal_view)
    retinal.div_(M)
    return retinal