-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_alignment.py
153 lines (123 loc) · 4.89 KB
/
image_alignment.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from RANSAC_Flow.quick_start.coarseAlignFeatMatch import CoarseAlign
import sys
sys.path.append('RANSAC_Flow/utils/')
import outil
sys.path.append('RANSAC_Flow/model/')
import model as model
import PIL.Image as Image
import os
import numpy as np
import torch
from torchvision import transforms
from tqdm import tqdm
import argparse
import warnings
import torch.nn.functional as F
import pickle
import pandas as pd
import kornia.geometry as tgm
# from scipy.misc import imresize
from itertools import product
if not sys.warnoptions:
warnings.simplefilter('ignore')
import matplotlib.pyplot as plt
def get_Avg_Image(Is, It) :
Is_arr, It_arr = np.array(Is) , np.array(It)
Imean = Is_arr * 0.5 + It_arr * 0.5
return Image.fromarray(Imean.astype(np.uint8))
class imageAlign:
def __init__(self):
self._initNetwork()
self._initCoarseModel()
def _initNetwork(self):
resumePth = 'RANSAC_Flow/model/pretrained/MegaDepth_Theta1_Eta001_Grad1_0.774.pth'
kernelSize = 7
self.Transform = outil.Homography
self.nbPoint = 4
network = {
'netFeatCoarse' : model.FeatureExtractor(),
'netCorr' : model.CorrNeigh(kernelSize),
'netFlowCoarse' : model.NetFlowCoarse(kernelSize),
'netMatch' : model.NetMatchability(kernelSize),
}
for key in list(network.keys()):
network[key].cuda()
self.typeData = torch.cuda.FloatTensor
param = torch.load(resumePth)
msg = 'Loading pretrained model from {}'.format(resumePth)
print (msg)
for key in list(param.keys()):
network[key].load_state_dict(param[key])
network[key].eval()
self.network = network
def _initCoarseModel(self):
nbScale = 7
coarseIter = 10000
coarsetolerance = 0.05
minSize = 224
imageNet = True # we can also use MOCO feature here
scaleR = 1.2
self.coarseModel = CoarseAlign(nbScale, coarseIter, coarsetolerance, 'Homography', minSize, 1, True, imageNet, scaleR)
def alignImages(self, sourceImage, targetImage, isPath=False, isTensor=False):
if isPath:
I1 = Image.open(sourceImage).convert('RGB')
I2 = Image.open(targetImage).convert('RGB')
elif isTensor:
trans = transforms.ToPILImage()
I1 = trans(sourceImage.cpu())
I2 = trans(targetImage.cpu())
else:
I1 = sourceImage.convert('RGB')
I2 = targetImage.convert('RGB')
self.coarseModel.setSource(I1)
self.coarseModel.setTarget(I2)
I2w, I2h = self.coarseModel.It.size
featt = F.normalize(self.network['netFeatCoarse'](self.coarseModel.ItTensor))
#### -- grid
gridY = torch.linspace(-1, 1, steps = I2h).view(1, -1, 1, 1).expand(1, I2h, I2w, 1)
gridX = torch.linspace(-1, 1, steps = I2w).view(1, 1, -1, 1).expand(1, I2h, I2w, 1)
grid = torch.cat((gridX, gridY), dim=3).cuda()
warper = tgm.HomographyWarper(I2h, I2w)
bestPara, InlierMask = self.coarseModel.getCoarse(np.zeros((I2h, I2w)))
bestPara = torch.from_numpy(bestPara).unsqueeze(0).cuda()
flowCoarse = warper.warp_grid(bestPara)
I1_coarse = F.grid_sample(self.coarseModel.IsTensor, flowCoarse)
I1_coarse_pil = transforms.ToPILImage()(I1_coarse.cpu().squeeze())
featsSample = F.normalize(self.network['netFeatCoarse'](I1_coarse.cuda()))
corr12 = self.network['netCorr'](featt, featsSample)
flowDown8 = self.network['netFlowCoarse'](corr12, False) ## output is with dimension B, 2, W, H
flowUp = F.interpolate(flowDown8, size=(grid.size()[1], grid.size()[2]), mode='bilinear')
flowUp = flowUp.permute(0, 2, 3, 1)
flowUp = flowUp + grid
flow12 = F.grid_sample(flowCoarse.permute(0, 3, 1, 2), flowUp).permute(0, 2, 3, 1).contiguous()
I1_fine = F.grid_sample(self.coarseModel.IsTensor, flow12)
I1_fine_pil = transforms.ToPILImage()(I1_fine.cpu().squeeze())
res = {
'sourceImage' : I1,
'targetImage' : I2,
'alignedImage': I1_fine_pil,
'alignedTensor':I1_fine,
}
return res
if __name__ == '__main__':
I1_path = 'RANSAC_Flow/img/test_source_cat.png'
I2_path = 'RANSAC_Flow/img/test_target_cat.png'
iAlign = imageAlign()
res = iAlign.alignImages(I1_path, I2_path, isPath=True)
I1_fine_pil = res['alignedImage']
I2 = res['targetImage']
It = iAlign.coarseModel.It
plt.figure(figsize=(20, 10))
plt.subplot(1, 3, 1)
plt.axis('off')
plt.title('Source Image (Fine Alignment)')
plt.imshow(I1_fine_pil)
plt.subplot(1, 3, 2)
plt.axis('off')
plt.title('Target Image')
plt.imshow(I2)
plt.subplot(1, 3, 3)
plt.axis('off')
plt.title('Overlapped Image')
plt.imshow(get_Avg_Image(I1_fine_pil, It))
plt.show()