-
Notifications
You must be signed in to change notification settings - Fork 0
/
glcm.py
45 lines (35 loc) · 1.32 KB
/
glcm.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
from skimage.feature import graycomatrix, graycoprops
import numpy as np
def calculate_glcm_from_single_image(
image, distances, angles, levels, symmetric=True, normed=True
):
return graycomatrix(
image, distances, angles, levels, symmetric=symmetric, normed=normed
)
def calculate_glcm_from_many_images(
images, distances, angles, levels, symmetric=True, normed=True
):
return [
calculate_glcm_from_single_image(
img, distances, angles, levels, symmetric, normed
)
for img in images
]
def calculate_glcm_matrix_for_each_category(
categories, images, distances, angles, levels, symmetric=True, normed=True
):
return {
category: calculate_glcm_from_many_images(
images[category], distances, angles, levels, symmetric, normed
)
for category in categories
}
def extract_glcm_features(glcm, props):
return np.array([graycoprops(glcm, prop).flatten() for prop in props]).flatten()
def extract_glcm_features_from_many_images(glcm_list, props):
return [extract_glcm_features(glcm, props) for glcm in glcm_list]
def extract_glcm_features_for_each_category(categories, glcm_list, props):
return {
category: extract_glcm_features_from_many_images(glcm_list[category], props)
for category in categories
}