-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfield.py
More file actions
82 lines (63 loc) · 1.86 KB
/
Copy pathfield.py
File metadata and controls
82 lines (63 loc) · 1.86 KB
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
"""
Implementation of the ground truth field
manager.
"""
###########
# Imports #
###########
import torch
import torchvision.transforms as transforms
from kornia.geometry.transform import warp_perspective
from PIL import Image
#####################
# General variables #
#####################
# Matrices associated to the ground truth model
CALIBRATION = torch.tensor([
[150, 0, 960],
[0, 150, 540],
[0, 0, 1]
]).float()
HOMOGRAPHY = torch.tensor([
[15, 0, 960],
[0, 15, 540],
[0, 0, 1]
]).float()
###########
# Classes #
###########
def three2four(img):
"""
Convert an image represented by a 3 * h * w tensor
to a 4 * h * w tensor.
"""
if len(img.size()) > 3:
img = img.squeeze(0)
h, w = img.size()[1], img.size()[2]
converted = torch.zeros(4, h, w)
nonnul = torch.sum(img, dim=0)
colors = torch.argmax(img, dim=0)
colors[(nonnul == 0)] = 4
converted[1][(colors == 2).squeeze()] = 1
converted[2][(colors == 1).squeeze()] = 1
converted[3][(colors == 0).squeeze()] = 1
return converted
class Field:
def __init__(self, field, ratio=7.5):
# Open image file
self.four = Image.open(field)
# Transform images to tensors
self.four = transforms.ToTensor()(self.four)
self.four = three2four(self.four)
# Homography to warp (resized) model
kprim = torch.true_divide(CALIBRATION, ratio)
kprim[2, 2] = 1
self.h = torch.matmul(kprim, torch.inverse(CALIBRATION))
self.h = torch.matmul(self.h, HOMOGRAPHY)
def warp_field(self, homography):
hr = torch.matmul(HOMOGRAPHY, torch.inverse(homography.view(3,3)))
hr = torch.inverse(hr)
hr = hr.unsqueeze(0)
# Warp the model
warped_field = warp_perspective(self.four.unsqueeze(0), hr, (144, 256), flags='nearest')
return warped_field