-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfingerprint.py
43 lines (35 loc) · 1.67 KB
/
fingerprint.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
__author__ = 'liorsabag'
import numpy as np
import cv2
def fp(img, mask=None):
mask = mask or np.ones((img.shape[0], img.shape[1]))
# i checked this , it doesnt seem to do what we want , e.g. do a=np.array([0,1]) ; b=np.ones((2)); a or b and u get an error
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# OpenCV uses H: 0 - 180, S: 0 - 255, V: 0 - 255
# histograms
bins = 25
n_pixels = cv2.countNonZero(mask)
hist_hue = cv2.calcHist([hsv], [0], mask, [bins], [0, 180])
hist_hue = [item for sublist in hist_hue for item in sublist] # flatten nested
hist_hue = np.divide(hist_hue, n_pixels)
hist_sat = cv2.calcHist([hsv], [1], mask, [bins], [0, 255])
hist_sat = [item for sublist in hist_sat for item in sublist]
hist_sat = np.divide(hist_sat, n_pixels)
hist_int = cv2.calcHist([hsv], [2], mask, [bins], [0, 255])
hist_int = [item for sublist in hist_int for item in sublist] # flatten nested list
hist_int = np.divide(hist_int, n_pixels)
# Uniformity t(5)=sum(p.^ 2);
hue_uniformity = np.dot(hist_hue, hist_hue)
sat_uniformity = np.dot(hist_sat, hist_sat)
int_uniformity = np.dot(hist_int, hist_int)
# Entropy t(6)=-sum(p. *(log2(p+ eps)));
eps = 1e-15
l_hue = np.log2(hist_hue + eps)
hue_entropy = -1*np.dot(hist_hue, l_hue)
l_sat = np.log2(hist_sat + eps)
sat_entropy = -1*np.dot(hist_sat, l_sat)
l_int = np.log2(hist_int + eps)
int_entropy = -1*np.dot(hist_int, l_int)
result_vector = [hue_uniformity, sat_uniformity, int_uniformity, hue_entropy, sat_entropy, int_entropy]
result_vector = np.concatenate((result_vector, hist_hue, hist_sat), axis=0)
return result_vector