back to iatexture

Function textpipe

Synopse

The textpipe function computes a set of texture descriptors given an image, roi and a quantization rate

  • att = textpipe(img,roi,rate=8,comp = 1)
    • Output
      • att: texture attributes. [0-11] Histogram attrs, [12-44] GLCM attrs, [45-49] RL attrs, [50-54] Gradient attrs.
    • Input
      • img: input image
      • roi: binary ROI
      • rate: quantization rate
      • comp: =1 to compile the attributes by computing the mean of different orientation attributes; comp = 0 to raw texture attributes

Description

The textpipe function computes a set of texture descriptors given an image, roi and a quantization rate. The output can be the raw texture attributes or a compilation version, in which it is computed the mean of different orientations.

Function Code

 1 from iatexture import *
 2 import ia870
 3 import numpy as np
 4 from numpy import *
 5 
 6 import multiprocessing as mp
 7 import psutil
 8 
 9 def textComp2(desc,img_dim=2):
10 
11     comp_desc = array([])
12     d = 1 # number os distances uses
13     if img_dim==3:
14         q = 13
15     else:
16         q = 4 # orientations
17 
18     nhist = 12
19     nglcm = 11*d*q
20     nrl = 5*q
21     ngrad = 5
22 
23     ## histogram ##
24     comp_desc = desc[:nhist]
25 
26     ## GLCM ##
27     glcmdesc = desc[nhist:nhist+nglcm]# 11 descriptors to each distance and each orientation
28     for i in xrange(d): # distances
29         glcmd = glcmdesc[i*11*q:(i+1)*11*q] # all glcm descriptors of distance i
30         aux = 0
31         for j in xrange(q):
32             aux += (glcmd[j*11:(j+1)*11])/q # mean of all orientations on distance i
33         comp_desc = concatenate((comp_desc,aux))
34     ## RL ##
35     rldesc = desc[nhist+nglcm:nhist+nglcm+nrl] # rl corresponds to the descriptors after glcm + 5 descriptors*number_of_orientations
36     aux = 0
37     for j in xrange(q):
38         aux += (rldesc[j*5:(j+1)*5])/q # mean of all orientations
39     comp_desc = concatenate((comp_desc,aux))
40 
41     ## Gradient ##
42     comp_desc = concatenate((comp_desc,desc[-ngrad:]))
43     return comp_desc
44 
45 def textpipe2(f,roi=[],rate=8,comp=1,bb_use=1):
46 
47     desc = np.array([])
48 
49     d = [1] # distances
50     q = [0,np.pi/4,np.pi/2,3*np.pi/4] # theta
51     img = f.copy()# do not process the original image
52     ## parameters ##
53 
54     o = np.array([[0,1],[1,1],[1,0],[1,-1]],dtype=np.int32) # offsets
55     p = [2] # phi is not used on 3D cases (this value can be any value different of zero)
56 
57     ## quantization ##
58     img = quantimg(img,rate).astype('uint8') # image quantization
59 
60     ## histogram ##
61     _,desc = histstat(img,roi)
62     ## GLCM ##
63     for d_i in d:
64         for o_i in o:
65             _,glcmDesc = glcmdesc(img,offset = o_i*d_i,mask=roi)
66             desc = np.concatenate((desc,glcmDesc))
67     ## RL ##
68     for q_i in q:
69         for p_i in p:
70             if p_i != 0 or p_i==0 and q_i==0:#if theta=0, phi must be zero. There is no (45,0),(90,0),(135,0)
71                 _,rlDesc = rldesc(img,theta=q_i,phi=p_i,mask=roi)
72                 desc = np.concatenate((desc,rlDesc))
73     ## gradient ##
74     _,gradDesc = gradstat(img,mask=roi)
75     desc = np.concatenate((desc,gradDesc))
76 
77     if comp ==1:
78         return textComp2(desc,img_dim=len(img.shape)) # texture attributes compilation
79     else:
80         return desc

2D Example

 1 from iatexture import *
 2 import ia870
 3 from textpipe2 import textpipe2
 4 import time
 5 
 6 img = adreadgray('p/LesionMRI/EM/Agnaldo Jose Costa - 808572-1 - 38A/22.png')
 7 roi = adreadgray('p/LesionMRI/EM/Agnaldo Jose Costa - 808572-1 - 38A/Peri_22_1.png')>0
 8 
 9 aux = ia870.ialabel(roi)
10 aux = ia870.iablob(aux,'area')
11 roi2 = aux>aux.max()-1 # ensure that it is only one lesion, use the biggest blob, avoid manual segmentation mistakes.
12 
13 rate = 8
14 
15 adshow(img,'img')
16 adshow(ia870.iagshow(img,roi),'roi')
17 print img[roi].mean()
18 print 'texture attributes = '
19 desc = textpipe2(img,roi,rate,comp=1)
20 print desc
92.2777777778
texture attributes = 
[  9.22777778e+01   1.88654784e+03  -1.05601415e+00   1.64170412e-01
   2.00000000e+00   4.00000000e+00   1.05000000e+02   1.32000000e+02
   1.56000000e+02   1.79258011e+00   2.00000000e+00   1.05000000e+02
   9.36060901e-03   2.12785961e+02   8.42725687e-01   8.76605160e+02
   9.67905022e-02   2.15432718e+02   3.28035832e+03   1.84819974e+00
   2.03362636e+00   8.15496400e+01   1.39833910e+00   1.32983577e+02
   2.66427489e+00   1.08055809e+00   9.79860476e-01   9.73958333e-01
   2.53484745e+01   1.66903118e+02   5.37502239e-01   2.33356852e-01
   1.00000000e+00]

img

roi

1 import homemariana.att_lib as att_lib
2 
3 # compare with previous library result - difference (we add 3 new histogram attributes)
4 desc2 = att_lib.textureAtt(img,roi)
5 desc2 = att_lib.textComp(desc2)
6 print desc2
7 #print desc2-desc[]
[  9.32777778e+01   1.88654784e+03  -1.05601415e+00   1.64170412e-01
   3.00000000e+00   5.00000000e+00   1.06000000e+02   1.33000000e+02
   1.57000000e+02   9.36060901e-03   2.12785961e+02   8.42725687e-01
   8.76605160e+02   9.67905022e-02   2.15432718e+02   3.28035832e+03
   1.84819974e+00   2.03362636e+00   8.15496400e+01   1.39833910e+00
   1.09951181e-02   4.88204221e+02   5.99658908e-01   7.29528019e+02
   6.13381291e-02   2.21161461e+02   2.40482852e+03   1.77616698e+00
   1.96260854e+00   1.75319481e+02   1.52215987e+00   1.35759615e-02
   7.24066280e+02   3.99139590e-01   6.90440481e+02   2.82647311e-02
   2.23041520e+02   1.93056811e+03   1.69214204e+00   1.87579541e+00
   2.27372278e+02   1.51810298e+00   1.76805220e-02   9.07233714e+02
   2.81980397e-01   6.91957235e+02   3.64073875e-02   2.22437166e+02
   1.71163589e+03   1.63575637e+00   1.77287402e+00   2.70970412e+02
   1.50938182e+00   2.58030990e-02   1.06791322e+03   3.03462646e-01
   6.85654281e+02   2.89199569e-02   2.20518887e+02   1.61760978e+03
   1.53256119e+00   1.64217947e+00   3.09931355e+02   1.43956474e+00
   1.32983577e+02   2.66427489e+00   1.08055809e+00   9.79860476e-01
   9.73958333e-01   2.53484745e+01   1.66903118e+02   5.37502239e-01
   2.33356852e-01   1.00000000e+00]