-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGLCM_for_single_image.py
52 lines (42 loc) · 1.58 KB
/
GLCM_for_single_image.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
52
#importing pillow module for images
from PIL import Image
#importing the GLCM module
from skimage.feature import greycomatrix, greycoprops
#importing numpy and pandas
import numpy as np
import pandas as pd
#function to extract features for a ***single image***
def extract_features(directory, dist, angle):
# make list for each feature and a dictionary to have all features
features = {}
contrasts = []
dissimilarities = []
homogeneties = []
correlations = []
energies = []
# load an image from file
image = Image.open(directory)
# convert the image pixels to a numpy array
img = np.array(image.getdata()).reshape(image.size[0], image.size[1])
#Calulate GLCM Features and Matrix
gcom = greycomatrix(img, [dist], [angle], 256, symmetric=True, normed=True)
contrast = greycoprops(gcom, prop='contrast')
dissimilarity = greycoprops(gcom, prop='dissimilarity')
homogeneity = greycoprops(gcom, prop='homogeneity')
energy = greycoprops(gcom, prop='energy')
correlation = greycoprops(gcom, prop='correlation')
# store feature
contrasts.append(contrast[0][0])
dissimilarities.append(dissimilarity[0][0])
homogeneties.append(homogeneity[0][0])
energies.append(energy[0][0])
correlations.append(correlation[0][0])
#Add features to dictionary of features
features['contrast'] = contrasts
features['dissimilarity'] = dissimilarities
features['homogeneity'] = homogeneties
features['energy'] = energies
features['correlation'] = correlations
#convert dictionary to dataframe
df = pd.DataFrame(features)
return df