-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_features.py
81 lines (76 loc) · 3.2 KB
/
extract_features.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
""" Module used to extract the features of a 16x16 px patch of an image """
import warnings
import numpy as np
import scipy
from scipy import signal
import skimage
from skimage.color import rgb2gray
from skimage.feature import greycomatrix, greycoprops
WINDOW=16
sobel = np.array([[1,2,1],[0,0,0],[-1,-2,-1]])
def edgedetect(img):
"""
edge detection of patch. Return gradient of image
Use of scipy.signal library for its convolution function
"""
g1 = scipy.signal.convolve2d(img,sobel,'same')
g2 = scipy.signal.convolve2d(img,sobel.T,'same')
return np.abs(g1+g2)
distances = [1]
orientations = [0,np.pi/4,np.pi/2,3*np.pi/4]
num_glcm_features=5
def texture_feat(img):
"""
Return the texture of a patch using the gray level co-occurence matrices
http://scikit-image.org/docs/dev/auto_examples/features_detection/plot_glcm.html
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
img=skimage.img_as_ubyte(img)
glcm=greycomatrix(img,distances,orientations,256,symmetric=True, normed=True)
diss=greycoprops(glcm,'dissimilarity').flatten()
contrast=greycoprops(glcm,'contrast').flatten()
correl = greycoprops(glcm,'correlation').flatten()
homogen =greycoprops(glcm,'homogeneity').flatten()
energy = greycoprops(glcm,'energy').flatten()
del glcm
feat=np.hstack((contrast,diss,correl,homogen,energy))
return feat
def extract_features_patch(img):
"""
Extract features of a single patch given:
- mean of all channels
- variance of all channels
if gray-level:
- textures
- edge density
"""
feat_m = np.mean(img, axis=(0,1))
feat_v = np.var(img, axis=(0,1))
feat = np.append(feat_m, feat_v)
###### graylevel features #########
if(len(img.shape)<3):
feat_text = texture_feat(img)
feat_ed = np.mean(edgedetect(img),axis=(0,1))
feat=np.hstack((feat,feat_text,feat_ed))
return feat
def extract_features_ngbr(color,gray,index,test=False):
"""
Extract features of neighboring patches, and add them to patch feature
if test, treat as individual image, and not as extended list of images
"""
if(not test):
color=color.reshape((100,int(np.sqrt(color.shape[0]//100)),int(np.sqrt(color.shape[0]//100)))+color.shape[1:])
gray=gray.reshape((100,int(np.sqrt(gray.shape[0]//100)),int(np.sqrt(gray.shape[0]//100)))+gray.shape[1:])
else:
color=color.reshape((1,int(np.sqrt(color.shape[0])),int(np.sqrt(color.shape[0])))+color.shape[1:])
gray=gray.reshape((1,int(np.sqrt(gray.shape[0])),int(np.sqrt(gray.shape[0])))+gray.shape[1:])
k=index//(color.shape[1]*color.shape[2])
row,col=np.unravel_index(index%100,(color.shape[1]-2,color.shape[2]-2))
feat=np.append(extract_features_patch(color[k,row+1,col+1]),extract_features_patch(gray[k,row+1,col+1]))
for i in [-1,1]:
feat=np.append(feat,extract_features_patch(color[k,row+1,col+1+i]))
feat=np.append(feat,extract_features_patch(gray[k,row+1,col+1+i]))
feat=np.append(feat,extract_features_patch(color[k,row+1+i,col+1]))
feat=np.append(feat,extract_features_patch(gray[k,row+1+i,col+1]))
return feat