-
Notifications
You must be signed in to change notification settings - Fork 9
/
slidingwindow.py
46 lines (40 loc) · 2.34 KB
/
slidingwindow.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
import numpy as np
def slidingWindow(image_size, init_size=(64,64), x_overlap=0.5, y_step=0.05,
x_range=(0, 1), y_range=(0, 1), scale=1.5):
"""
Run a sliding window across an input image and return a list of the
coordinates of each window.
Window travels the width of the image (in the +x direction) at a range of
heights (toward the bottom of the image in the +y direction). At each
successive y, the size of the window is increased by a factor equal to
@param scale. The horizontal search area is limited by @param x_range
and the vertical search area by @param y_range.
@param image_size (int, int): Size of the image (width, height) in pixels.
@param init_size (int, int): Initial size of of the window (width, height)
in pixels at the initial y, given by @param y_range[0].
@param x_overlap (float): Overlap between adjacent windows at a given y
as a float in the interval [0, 1), where 0 represents no overlap
and 1 represents 100% overlap.
@param y_step (float): Distance between successive heights y as a
fraction between (0, 1) of the total height of the image.
@param x_range (float, float): (min, max) bounds of the horizontal search
area as a fraction of the total width of the image.
@param y_range (float, float) (min, max) bounds of the vertical search
area as a fraction of the total height of the image.
@param scale (float): Factor by which to scale up window size at each y.
@return windows: List of tuples, where each tuple represents the
coordinates of a window in the following order: (upper left corner
x coord, upper left corner y coord, lower right corner x coord,
lower right corner y coord).
"""
windows = []
h, w = image_size[1], image_size[0]
for y in range(int(y_range[0] * h), int(y_range[1] * h), int(y_step * h)):
win_width = int(init_size[0] + (scale * (y - (y_range[0] * h))))
win_height = int(init_size[1] + (scale * (y - (y_range[0] * h))))
if y + win_height > int(y_range[1] * h) or win_width > w:
break
x_step = int((1 - x_overlap) * win_width)
for x in range(int(x_range[0] * w), int(x_range[1] * w), x_step):
windows.append((x, y, x + win_width, y + win_height))
return windows