-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_calculation.py
More file actions
72 lines (40 loc) · 1.46 KB
/
image_calculation.py
File metadata and controls
72 lines (40 loc) · 1.46 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from skimage import io
import math
import numpy as np
class ImageDigitalExpress(object):
def __init__(self, image_path):
self._image_path = image_path
# calculate an image mean and variance , return a tuple
def image_mean_variance(self):
summary = 0
diff_sum = 0
img = io.imread(self._image_path, as_grey=True)
row = len(img)
column = len(img[0])
for i in range(column):
for n in range(column):
summary += img[i][n]
mean = summary / (row*column)
for a in range(row):
for b in range(column):
diff_sum += (img[a][b] - mean) ** 2
variance = math.sqrt(diff_sum / (row * column))
return mean, variance
# calculate an image information entropy
def image_infoentropy(self):
img = io.imread(self._image_path, as_grey=True)
img_size = img.size
hist = np.histogram(img, bins=256)[0] / img_size
hist_list = list(hist)
num = 0
for i in range(len(hist_list)):
if hist_list[i] == 0:
num += 1
for n in range(num):
hist_list.remove(0)
sum_num = len(hist_list)
add_sum = 0
for k in range(sum_num):
add_sum += hist_list[k] * math.log(hist_list[k], 2)
img_entropy = -add_sum
return img_entropy