-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
34 lines (24 loc) · 994 Bytes
/
tools.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
import numpy as np
from skimage.feature import corner_harris, corner_peaks
def invert_coords(coords):
return np.array([[c[1], c[0]] for c in coords])
def trim_coords_to_shape(shape, coords, padding=32):
height, width = shape
def filter_fn(coord):
x, y = coord
return x >= padding and \
x <= width - padding and \
y >= padding and \
y <= height - padding
filtered = filter(filter_fn, coords)
return np.array(list(filtered))
def get_keypoints(image):
keypoints = corner_peaks(corner_harris(image), min_distance=5)
keypoints = invert_coords(keypoints)
keypoints = trim_coords_to_shape(image.shape, keypoints)
return keypoints
def get_keypoints_random(image, size=150, padding=32):
img_h, img_w = image.shape
widths = np.random.uniform(padding, img_w - padding, size=(size, 1))
heights = np.random.uniform(padding, img_h - padding, size=(size, 1))
return np.hstack((widths, heights))