-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwindow.py
57 lines (46 loc) · 2.03 KB
/
window.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
class SlidingWindow:
""" An iterator class that acts as a window function over an image.
Estimating the color-line and transmission at every possible image
window is costly and redundant due to overlap. This sliding window
class is used to iterate over a subset of all image windows. This
is achieved by scanning the image in non-overlapping patches. To
avoid large unresolved areas, multiple passes are made with
slightly different window offsets.
Example use:
sliding_window = SlidingWindow(image)
for window in sliding_window:
foo(...)
Args:
image (3D numpy array): The image the window slides over.
patch_size (int): The size of individual patches
scans (int): The number of times the non-overlapping sliding
window passes over the entire image with a small offset.
"""
def __init__(self, image, patch_size=7, scans=4):
self.image = image
self.patch_size = patch_size
self.scans = scans
def __iter__(self):
height, width, _ = self.image.shape
patch_size = self.patch_size
hop_size = self.patch_size
image = self.image
for offset in range(self.scans):
x_start = 0 + offset * (patch_size - 1 // 2)
y_start = 0 + offset * (patch_size - 1 // 2)
x_end = width - patch_size + 1
y_end = height - patch_size + 1
for y in range(y_start, y_end, hop_size):
for x in range(x_start, x_end, hop_size):
patch = image[y:y + patch_size,
x:x + patch_size, :].copy()
window = Window(patch,
y=y + patch_size - 1 // 2,
x=x + patch_size - 1 // 2)
yield(window)
class Window:
""" A class containing an image pixel patch and its coordinates. """
def __init__(self, patch, x, y):
self.patch = patch
self.x = x
self.y = y