-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetFeatures.py
More file actions
executable file
·43 lines (32 loc) · 1.2 KB
/
getFeatures.py
File metadata and controls
executable file
·43 lines (32 loc) · 1.2 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 19 14:15:33 2018
@author: edwardwu
"""
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import cv2
def getFeatures(img, bbox):
y, x = [], []
N = -float('inf')
num_of_objs = bbox.shape[0]
for i in range(num_of_objs):
topx, bottomx = bbox[i, 0, 0], bbox[i, 1, 0]
lefty, righty = bbox[i, 0, 1], bbox[i, 2, 1]
obj = img[int(topx):int(bottomx+1), int(lefty):int(righty+1)]
obj = np.float32(obj)
corners = cv2.goodFeaturesToTrack(obj, maxCorners = 100, qualityLevel = 0.001, minDistance = 3)
corners = corners[:,0,:]
corners[:, 0] += topx
corners[:, 1] += lefty
corners = corners[(corners[:, 0] <= bottomx) & (corners[:, 0] >= topx) & (corners[:,1] <= righty) & (corners[:,1] >= lefty)]
N = max(N, corners.shape[0])
y.append(corners[:, 0])
x.append(corners[:, 1])
res_y, res_x = np.zeros([N, num_of_objs]), np.zeros([N, num_of_objs]) - 1
for i in range(num_of_objs):
res_y[:len(y[i]), i] = y[i]
res_x[:len(x[i]), i] = x[i]
return res_y, res_x