Generate transformation matrix.
(center, scale, res, rot=0)
| 959 | |
| 960 | |
| 961 | def get_transform(center, scale, res, rot=0): |
| 962 | """Generate transformation matrix.""" |
| 963 | # res: (height, width), (rows, cols) |
| 964 | crop_aspect_ratio = res[0] / float(res[1]) |
| 965 | h = 200 * scale |
| 966 | w = h / crop_aspect_ratio |
| 967 | t = np.zeros((3, 3)) |
| 968 | t[0, 0] = float(res[1]) / w |
| 969 | t[1, 1] = float(res[0]) / h |
| 970 | t[0, 2] = res[1] * (-float(center[0]) / w + 0.5) |
| 971 | t[1, 2] = res[0] * (-float(center[1]) / h + 0.5) |
| 972 | t[2, 2] = 1 |
| 973 | if not rot == 0: |
| 974 | rot = -rot # To match direction of rotation from cropping |
| 975 | rot_mat = np.zeros((3, 3)) |
| 976 | rot_rad = rot * np.pi / 180 |
| 977 | sn, cs = np.sin(rot_rad), np.cos(rot_rad) |
| 978 | rot_mat[0, :2] = [cs, -sn] |
| 979 | rot_mat[1, :2] = [sn, cs] |
| 980 | rot_mat[2, 2] = 1 |
| 981 | # Need to rotate around center |
| 982 | t_mat = np.eye(3) |
| 983 | t_mat[0, 2] = -res[1] / 2 |
| 984 | t_mat[1, 2] = -res[0] / 2 |
| 985 | t_inv = t_mat.copy() |
| 986 | t_inv[:2, 2] *= -1 |
| 987 | t = np.dot(t_inv, np.dot(rot_mat, np.dot(t_mat, t))) |
| 988 | return t |
| 989 | |
| 990 | |
| 991 | def transform(pt, center, scale, res, invert=0, rot=0): |