-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexity.py
52 lines (39 loc) · 1.39 KB
/
complexity.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
#Implement by https://github.com/zhaoxuhui/Entropy
import numpy as np
from numba import jit
from collections import Counter
@jit(nopython=True)
def calcIJ(img_patch):
total_p = img_patch.shape[0] * img_patch.shape[1]
if total_p % 2 != 0:
center_p = img_patch[int(img_patch.shape[0] / 2), int(img_patch.shape[1] / 2)]
mean_p = (np.sum(img_patch) - center_p) / (total_p - 1)
return (center_p, mean_p)
else:
pass
def calcEntropy2dSpeedUp(img, win_w=3, win_h=3):
height = img.shape[0]
ext_x = int(win_w / 2)
ext_y = int(win_h / 2)
ext_h_part = np.zeros([height, ext_x], img.dtype)
tem_img = np.hstack((ext_h_part, img, ext_h_part))
ext_v_part = np.zeros([ext_y, tem_img.shape[1]], img.dtype)
final_img = np.vstack((ext_v_part, tem_img, ext_v_part))
new_width = final_img.shape[1]
new_height = final_img.shape[0]
IJ = []
for i in range(ext_x, new_width - ext_x):
for j in range(ext_y, new_height - ext_y):
patch = final_img[j - ext_y:j + ext_y + 1, i - ext_x:i + ext_x + 1]
ij = calcIJ(patch)
IJ.append(ij)
Fij = Counter(IJ).items()
Pij = []
for item in Fij:
Pij.append(item[1] * 1.0 / (new_height * new_width))
H_tem = []
for item in Pij:
h_tem = -item * (np.log(item) / np.log(2))
H_tem.append(h_tem)
H = np.sum(H_tem)
return H