-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.py
42 lines (27 loc) · 997 Bytes
/
features.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
import numpy as np
from image_format import *
def get_features(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = crop(image)
image = blur(image)
image = remove_back(image)
return nonzero_pixels(image), mean_brightness(image), center_of_mass(image)
def nonzero_pixels(image):
return len(image[image > 0].flatten()) / len(image.flatten())
def mean_brightness(image):
return np.mean(image[image > 0])
def center_of_mass(image):
image_center_x = image.shape[0] / 2
image_center_y = image.shape[1] / 2
center_x, center_y, amount = 0, 0, 0
for x in range(len(image)):
for y in range(len(image[x])):
if (image[x][y] > 0):
center_x += x
center_y += y
amount += 1
if (amount == 0):
return image.shape[0] / 2, image.shape[1] / 2
center_x /= amount
center_y /= amount
return abs(center_x - image_center_x), abs(center_y - image_center_y)