-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTamura.py
67 lines (61 loc) · 2.44 KB
/
Tamura.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
"""
Created on 29 Jun 2013
Implements the calculation of the Tamura features.
@author: Bibiana and Adria
"""
from scipy.stats.stats import kurtosis
import numpy as np
from scipy.signal.signaltools import convolve2d
from math import pi
H = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
V = np.array([[-1,-1,-1],[0,0,0], [1,1,1]])
class Tamura:
@staticmethod
def coarseness(img):
(rows, columns, _) = img.shape
A = np.zeros((6,rows,columns))
S = np.zeros((rows,columns))
#Step 1
for row in range(rows):
for column in range(columns):
size = 1
for count in range(6):
beginningi = max(row - (size - 1) / 2, 0)
beginningj = max(column - (size - 1) / 2, 0)
finali = min(beginningi + size, rows)
finalj = min(beginningj + size, columns)
A[count][row][column] = img[beginningi:finali, beginningj:finalj, :].mean()
size = size*2
#Step 2 and 3
for row in range(rows):
for column in range(columns):
size = 1
arrayVal = np.zeros(12)
for count in range(6):
beginningi = max(row - (size - 1) / 2, 0)
beginningj = max(column - (size - 1) / 2, 0)
finali = min(beginningi + size, rows-1)
finalj = min(beginningj + size, columns-1)
arrayVal[2*count] = np.abs(A[count][finali][column] - A[count][beginningi][column])
arrayVal[2*count+1] = np.abs(A[count][row][finalj] - A[count][row][beginningj])
S[row][column] = np.max(arrayVal)
#step 4
return S.mean()
@staticmethod
def contrast(img):
kurt = kurtosis(img,axis=None,fisher=False)
var = img.var()
return var / np.power(kurt, 1. / 4.)
@staticmethod
def directionality(img):
newImg = img + 4
convV = np.zeros(newImg.shape)
convH = np.zeros(newImg.shape)
for i in range(newImg.shape[2]):
convV[:,:,i] = convolve2d(newImg[:,:,i],V,mode='same',fillvalue=1)
convH[:,:,i] = convolve2d(newImg[:,:,i],H,mode='same',fillvalue=1)
convV[convV == 0] = 0.1
convH[convH == 0] = 0.1
theta1 = pi/2. + np.arctan(convV/convH)
theta2 = pi/2 + np.arctan(convH/convV)
return [theta1.var(),theta2.var()]