forked from XiSHEN0220/RANSAC-Flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
129 lines (111 loc) · 4.6 KB
/
dataloader.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import pandas as pd
from torch.utils.data import Dataset
import os.path as osp
import numpy as np
import torch
import cv2
class HPatchesDataset(Dataset):
"""
HPatches dataset (for evaluation)
Args:
csv_file: csv file with ground-truth data
image_path_orig: filepath to the dataset (full resolution)
transforms: image transformations (data preprocessing)
image_size: size (tuple) of the output images
Output:
source_image: source image
target_image: target image
correspondence_map: pixel correspondence map
between source and target views
mask: valid/invalid correspondences
"""
def __init__(self,
csv_file,
image_path_orig,
transforms,
image_size=(240, 240)):
self.df = pd.read_csv(csv_file)
self.image_path_orig = image_path_orig
self.transforms = transforms
self.image_size = image_size
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
data = self.df.iloc[idx]
obj = str(data.obj)
im1_id, im2_id = str(data.im1), str(data.im2)
h_scale, w_scale = self.image_size[0], self.image_size[1]
h_ref_orig, w_ref_orig = data.Him.astype('int'), data.Wim.astype('int')
h_trg_orig, w_trg_orig, _ = \
cv2.imread(osp.join(self.image_path_orig,
obj,
im2_id + '.ppm'), -1).shape
H = data[5:].astype('double').values.reshape((3, 3))
'''
As gt homography is calculated for (h_orig, w_orig) images,
we need to
map it to (h_scale, w_scale)
H_scale = S * H * inv(S)
'''
S1 = np.array([[w_scale / w_ref_orig, 0, 0],
[0, h_scale / h_ref_orig, 0],
[0, 0, 1]])
S2 = np.array([[w_scale / w_trg_orig, 0, 0],
[0, h_scale / h_trg_orig, 0],
[0, 0, 1]])
H_scale = np.dot(np.dot(S2, H), np.linalg.inv(S1))
# inverse homography matrix
Hinv = np.linalg.inv(H_scale)
# estimate the grid
X, Y = np.meshgrid(np.linspace(0, w_scale - 1, w_scale),
np.linspace(0, h_scale - 1, h_scale))
X, Y = X.flatten(), Y.flatten()
# create matrix representation
XYhom = np.stack([X, Y, np.ones_like(X)], axis=1).T
# multiply Hinv to XYhom to find the warped grid
XYwarpHom = np.dot(Hinv, XYhom)
# vector representation
XwarpHom = torch.from_numpy(XYwarpHom[0, :]).float()
YwarpHom = torch.from_numpy(XYwarpHom[1, :]).float()
ZwarpHom = torch.from_numpy(XYwarpHom[2, :]).float()
Xwarp = \
(2 * XwarpHom / (ZwarpHom + 1e-8) / (w_scale - 1) - 1)
Ywarp = \
(2 * YwarpHom / (ZwarpHom + 1e-8) / (h_scale - 1) - 1)
# and now the grid
grid_gt = torch.stack([Xwarp.view(h_scale, w_scale),
Ywarp.view(h_scale, w_scale)], dim=-1)
# mask
mask = grid_gt.ge(-1) & grid_gt.le(1)
mask = mask[:, :, 0] & mask[:, :, 1]
img1 = \
cv2.resize(cv2.imread(osp.join(self.image_path_orig,
obj,
im1_id + '.ppm'), -1),
self.image_size)
img2 = \
cv2.resize(cv2.imread(osp.join(self.image_path_orig,
obj,
im2_id + '.ppm'), -1),
self.image_size)
_, _, ch = img1.shape
if ch == 3:
img1_tmp = cv2.imread(osp.join(self.image_path_orig,
obj,
im1_id + '.ppm'), -1)
img2_tmp = cv2.imread(osp.join(self.image_path_orig,
obj,
im2_id + '.ppm'), -1)
img1 = cv2.cvtColor(cv2.resize(img1_tmp,
self.image_size),
cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(cv2.resize(img2_tmp,
self.image_size),
cv2.COLOR_BGR2RGB)
# global transforms
img1 = self.transforms(img1)
img2 = self.transforms(img2)
return {'source_image': img1,
'target_image': img2,
'correspondence_map': grid_gt,
'mask': mask.long()}