-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbase_homography_generator.py
177 lines (137 loc) · 4.53 KB
/
base_homography_generator.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import numpy as np
import networkx as nx
import cv2
from typing import List, Tuple
from abc import ABC, abstractmethod
class BaseHomographyGenerator(ABC):
def __init__(self, K: np.ndarray, D: np.ndarray, buffer_size: int=None, undistort: bool=False) -> None:
self._img_graph = nx.Graph()
self._current_id = None
self._prev_id = self._current_id
self._buffer_size = buffer_size
self._ud = undistort
self._K = K
self._D = D
def addImg(self, img: np.ndarray) -> int:
r"""Append image graph by img and undistort if desired.
"""
self._prev_id = self._current_id
self._current_id = len(self._img_graph)
if self._ud:
img, _ = self.undistort(img)
self._img_graph.add_node(self._current_id, data=img)
if self._prev_id is not None:
self._img_graph.add_edge(self._prev_id, self._current_id)
self._img_graph.add_edge(self._current_id, self._prev_id) # bi-directional graph
if self._buffer_size is not None:
if len(self._img_graph) > self._buffer_size:
first = list(nx.topological_sort(self._img_graph))[0]
self._img_graph.remove_node(first)
self._img_graph = nx.relabel_nodes(self._img_graph, lambda x: x - 1)
self._current_id -= 1
return self._current_id
def clearImgGraph(self) -> None:
r"""Clear image buffer.
"""
self._current_id = None
self._prev_id = self._current_id
self._img_graph.clear()
@abstractmethod
def desiredHomography(self) -> np.ndarray:
r"""Compute desired homography based on image buffer.
Return:
G (np.ndarray): projective homography
"""
return
def undistort(self, img: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
r"""Undistord img.
Args:
img (np.ndarray): Image in OpenCV convention of size HxWxC
Return:
(img, K) (Tuple[np.ndarray, np.ndarray]): Tuple containing undistorted image and new camera matrix
"""
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(self._K, self._D, (w,h), 1, (w,h)) # alpha set to 1.
return cv2.undistort(img, self._K, self._D, None, newcameramtx), newcameramtx
@property
def ImgGraph(self) -> nx.Graph:
r"""Image buffer.
"""
return self._img_graph
@ImgGraph.deleter
def ImgGraph(self) -> None:
self.clearImgGraph()
@property
def ID(self) -> int:
r"""Get current id.
"""
return self._current_id
@ID.setter
def ID(self, id: int) -> None:
r"""Set current node.
"""
self._current_id = id
@property
def K(self) -> np.ndarray:
r"""Camera intrinsics.
"""
return self._K
@K.setter
def K(self, value: np.ndarray) -> None:
self._K = value
@property
def D(self) -> np.ndarray:
r"""Camera distortion.
"""
return self._D
@D.setter
def D(self, value: np.ndarray) -> None:
self._D = value
if __name__ == '__main__':
import matplotlib.pyplot as plt
class DummHomographyGenerator(BaseHomographyGenerator):
def desiredHomography():
return np.eye(3)
def visualize(G: nx.DiGraph):
nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
H = nx.read_edgelist(path="grid.edgelist", delimiter=":")
nx.draw(H)
plt.show()
K = np.eye(5)
D = np.zeros(6)
hg = DummHomographyGenerator(K, D)
# add 3 images to graph
img = np.ones((16, 16))
id = hg.addImg(img)
print('Added node: {}'.format(id))
id = hg.addImg(img)
print('Added node: {}'.format(id))
id =hg.addImg(img)
print('Added node: {}'.format(id))
graph = hg.ImgGraph
visualize(graph)
# move id
print('Current id: {}'.format(hg.ID))
id = 1
print('Setting id to: {}'.format(id))
hg.ID = id
print('Current id: {}'.format(hg.ID))
# add new image
id = hg.addImg(img)
print('Added node: {}'.format(id))
graph = hg.ImgGraph
visualize(graph)
# find path
src = hg.ID
dst = 0
path = nx.dijkstra_path(hg.ImgGraph, src, dst)
print('Path from {} to {}: {}'.format(src, dst, path))
# clear graph
hg.clearImgGraph()
# find path
src = hg.ID
dst = 0
try:
path = nx.dijkstra_path(hg.ImgGraph, src, dst)
except:
print('Path could not be found')