File size: 2,634 Bytes
b762e56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
import numpy as np
from PIL import Image

def trans2_white_bg(img_path):
    img = Image.open(img_path)
    img_arr = 255 - np.array(img)[:, :, 3]
    img_ = Image.fromarray(img_arr)
    img_.save(img_path)
    return img_arr

def affine_shear(seq, dx=-0.3, dy=0.0):
    mask = ~(seq == 0)
    seq_12 = seq.copy()
    seq_12[:,4] -= 12.0
    seq_12[:,5]  = -seq_12[:,5] + 12
    seq_12[:,6] -= 12.0
    seq_12[:,7]  = -seq_12[:,7] + 12
    seq_12[:,8] -= 12.0
    seq_12[:,9]  = -seq_12[:,9] + 12
    
    seq_args = seq_12[:,4:]
    seq_args = np.concatenate([seq_args[:, :2], seq_args[:, 2:4], seq_args[:, 4:6]], 0).transpose(1,0)
    affine_matrix=np.array([[1, dx], 
                            [dy, 1]])
    rotated_args = np.dot(affine_matrix,seq_args)
    rotated_args = rotated_args.transpose(1,0)
    new_args = np.concatenate([rotated_args[:seq.shape[0]], rotated_args[seq.shape[0]:seq.shape[0]*2], rotated_args[seq.shape[0]*2:]],-1)
    new_args[:,0] += 12.0
    new_args[:,1] = -(new_args[:,1] - 12)
    new_args[:,2] += 12.0
    new_args[:,3] = -(new_args[:,3] - 12)
    new_args[:,4] += 12.0
    new_args[:,5] = -(new_args[:,5] - 12)
    new_seq = np.concatenate([seq[:, :4], new_args],1)
    new_seq = new_seq * mask
    return new_seq

def affine_scale(seq, scale=0.8):
    mask = ~(seq==0)
    seq_args = seq[:, 4:] - 12.0
    seq_args *= scale
    seq_args = seq_args + 12.0
    new_seq = np.concatenate([seq[:, :4], seq_args], 1)
    new_seq = new_seq * mask
    return new_seq

def affine_rotate(seq,theta=-5):
    mask = ~(seq==0)
    seq_12 = seq.copy()
    seq_12[:,4] -=12.0
    seq_12[:,5]  = -seq_12[:,5] + 12
    seq_12[:,6] -=12.0
    seq_12[:,7]  = -seq_12[:,7] + 12
    seq_12[:,8] -=12.0
    seq_12[:,9]  = -seq_12[:,9] + 12
    
    seq_args =seq_12[:, 4:] # default as [71,6]
    seq_args = np.concatenate([seq_args[:,:2],seq_args[:,2:4],seq_args[:,4:6]],0).transpose(1,0)# note 2,213
    theta = math.radians(theta)
    affine_matrix=np.array([[np.cos(theta),-np.sin(theta)], [np.sin(theta), np.cos(theta)]])# note 2,2
    rotated_args = np.dot(affine_matrix,seq_args)# note 2,213
    rotated_args = rotated_args.transpose(1,0)# note 213,2
    new_args = np.concatenate([rotated_args[:seq.shape[0]],rotated_args[seq.shape[0]:seq.shape[0]*2],rotated_args[seq.shape[0]*2:]],-1)# note 2,213
    new_args[:,0] +=12.0
    new_args[:,1] = -(new_args[:,1]-12)
    new_args[:,2] +=12.0
    new_args[:,3] = -(new_args[:,3]-12)
    new_args[:,4] +=12.0
    new_args[:,5] = -(new_args[:,5]-12)
    
    new_seq = np.concatenate([seq[:,:4],new_args],1)
    new_seq =new_seq *mask
    return new_seq