-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.py
More file actions
executable file
·156 lines (122 loc) · 5.28 KB
/
Copy pathstack.py
File metadata and controls
executable file
·156 lines (122 loc) · 5.28 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
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
#!/usr/bin/python
# Copyright (c) 2015 Matthew Earl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Routines for stacking registered images.
"""
__all__ = (
'get_bounding_rect',
'StackedImage',
)
import collections
import cv2
import numpy
class _BoundingRect(collections.namedtuple('_Rect', ('x', 'y', 'w', 'h'))):
"""
A rectangle which bounds a set of points.
"""
@classmethod
def from_points(cls, points):
"""Create a bounding rectangle from a set of points."""
top_left = numpy.min(points, axis=1)
bottom_right = numpy.max(points, axis=1)
return _BoundingRect(top_left[0, 0],
top_left[1, 0],
bottom_right[0, 0] - top_left[0, 0],
bottom_right[1, 0] - top_left[1, 0])
def expand(self, points):
"""Expand the bounding rect with a matrix of points."""
points = numpy.vstack(numpy.matrix(points), self.corners)
return self.from_points(points)
@property
def corners(self):
return numpy.matrix([[self.x, self.y],
[self.x + self.w, self.y],
[self.x, self.y + self.h],
[self.x + self.w, self.y + self.h]]).T
@property
def size(self):
return self.w, self.h
def _translate_matrix(v):
"""Return a translation matrix."""
out = numpy.identity(3)
out[:2, 2:] = v
return out
def get_bounding_rect(ims_and_transforms):
"""
Return a bounding rectangle for a set of (image, transformation) pairs.
Argument:
ims_and_transforms: Sequence of image and transformation matrices. Each
element is an `(im, M)` pair, where `M` converts points in a
reference coordinate frame into the image's coordinate frame.
"""
def im_corners(im):
return numpy.matrix([[0, 0],
[0, im.shape[0]],
[im.shape[1], 0],
[im.shape[1], im.shape[0]]],
dtype=numpy.float64).T
points = numpy.hstack([M.I * numpy.vstack([im_corners(im),
numpy.ones((1, 4))])
for im, M in ims_and_transforms])
rect = _BoundingRect.from_points(points)
return (rect.x, rect.y, rect.w, rect.h)
class StackedImage(object):
"""
Represents an image composed of a set of overlaid images.
Input images are rotated and translated into a reference coordinate system.
The output image bounds are determined by a rectangle, whose coordinates
are in the same reference coordinate system.
`get_bounding_rect` can be used to obtain the bounding rectangle. For
example, to stack a list of (image, transformation) pairs where the output
image bounds all the input images:
stacked = StackedImage(get_bounding_rect(ims_and_transforms))
for im, M in ims_and_transforms:
stacked.add_image(im, M)
# stacked.im is now the stacked image.
To stack of list of (image, transformation) pairs where the output image
bounds just the first image:
stacked = StackedImage(get_bounding_rect(ims_and_transforms[:1]))
for im, M in ims_and_transforms:
stacked.add_image(im, M)
# stacked.im is now the stacked image.
"""
def __init__(self, rect):
"""
Initialize a new StackedImage.
Arguments:
rect: Rectangle defining the bounds of the output image, relative
to the reference coordinate system. The rectangle is a
`(x, y, w, h)` tuple, such as the one returned by
`get_bounding_rect()`.
"""
self._rect = _BoundingRect(*rect)
self._im = numpy.zeros((self._rect.h, self._rect.w),
dtype=numpy.uint8)
@property
def im(self):
return self._im
def add_image(self, im, M):
cv2.warpAffine(im,
(M * _translate_matrix(self._rect.corners[:, 0]))[:2],
tuple(int(x) for x in self._rect.size),
dst=self._im,
borderMode=cv2.BORDER_TRANSPARENT,
flags=cv2.WARP_INVERSE_MAP)