-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptor.py
59 lines (39 loc) · 1.75 KB
/
descriptor.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
import numpy as np
from collections import namedtuple
from HuMomentsDescriptor import HuMomentsDescriptor
from ExposureDescriptor import ExposureDescriptor
Descriptor = namedtuple('Descriptor', ['moments', 'exposure'])
def trim_patch(patch, size=(20, 20)):
img_h, img_w = patch.shape
h, w = size
diff_h, diff_w = img_h - h, img_w - w
return patch.copy()[int(diff_h/2):-int(diff_h/2),
int(diff_w/2):-int(diff_w/2)]
def extract(image, keypoints):
hu_moments = HuMomentsDescriptor(patch_size=(24, 24))
exposure = ExposureDescriptor(patch_size=(32, 32))
moments_descriptors = hu_moments.extract(image, keypoints)
exposure_descriptors = exposure.extract(image, keypoints)
assert(len(moments_descriptors) == len(exposure_descriptors))
# Put it into Descriptor struct
descriptors = [Descriptor(m, e) for m, e in zip(
moments_descriptors, exposure_descriptors)]
return descriptors
def extract_for_patch(patch):
hu_moments = HuMomentsDescriptor()
exposure = ExposureDescriptor()
moments_desc = hu_moments.extract_for_patch(trim_patch(patch, size=(24, 24)))
exposure_desc = exposure.extract_for_patch(patch)
return Descriptor(moments_desc, exposure_desc)
def distance(desc1, desc2):
return distance_weighted(desc1, desc2)
def distance_weighted(desc1, desc2, w_moments=0.25, w_exposure=0.75):
assert(w_moments + w_exposure == 1)
hu_moments = HuMomentsDescriptor()
exposure = ExposureDescriptor()
hu_moments_distance = hu_moments.distance_braycurtis(
desc1.moments, desc2.moments)
exposure_distance = exposure.distance(
desc1.exposure, desc2.exposure)
return (w_exposure * exposure_distance) + \
(w_moments * hu_moments_distance)