Statistiques
| Révision :

root / bin / measure_sepal.py @ 18

Historique | Voir | Annoter | Télécharger (4,54 ko)

1
#!/usr/bin/env python
2

    
3
'''
4
Usage:
5
measure_sepal.py filename outputdir
6

7
- filename = path and name of the stack file containing the object (background=0)
8
the object is supposed already oriented as follows:
9
- output = output directory
10
---------------------------------------------------------------------------------
11
- CM in the middle of the image
12
- first principal axis along y
13
- second principal axis along x
14

15
Output:
16

17
measured data is written into the file output.csv
18
flat projections are written in the output directory
19

20
'''
21

    
22
from sys import argv
23
import os
24
import numpy as np
25
from scipy.ndimage.morphology import binary_closing, binary_opening
26

    
27
from titk_tools.io import imread, imsave
28
from bib_sepals import *
29

    
30

    
31
print('============== measure_sepal.py =================')
32

    
33
filename = argv[1]
34
outdir = argv[2]
35

    
36
outputfile=outdir+"/"+outdir+".csv"
37

    
38
outputdir=outdir+"/sections/"
39
projectiondir=outdir+"/projections"
40

    
41

    
42
print(argv)
43

    
44
imname = filename.split('/')[-1]
45
imnameroot = imname.split('.')[0]
46

    
47
projected2Dimage=projectiondir+"/"+imnameroot+"2D.png"
48
#projected2Dimage_hight=outdir+"/hight_"+imnameroot+"2D.png"
49

    
50
os.system("mkdir -p "+outputdir)
51

    
52

    
53

    
54
outname = outputfile.split('/')[-1]
55
outroot = outname.split('.')[0]
56

    
57
print("----------------------")
58
print("reading the file ", filename)
59
print("----------------------")
60
sepale=imread(filename)
61
voxelsize = sepale.voxelsize
62
print("voxelsize=",voxelsize)
63
s = sepale.shape
64
imtype=sepale.dtype
65
print("shape=",s)
66

    
67
print("Computing the volume and volumic asymmetry...")
68
vol=len(np.where(sepale)[0])
69

    
70
# left-right symmetry
71
sep_left=sepale[:int(s[0]/2), :, :]
72
sep_right=sepale[int(s[0]/2):, :, :]
73
vol_left=len(np.where(sep_left)[0])
74
vol_right=len(np.where(sep_right)[0])
75
Avolx=(vol_left-vol_right+0.0)/(vol+0.0)
76

    
77
# top-base symmetry
78
sep_left=sepale[:, :int(s[1]/2), :]
79
sep_right=sepale[:, int(s[1]/2):, :]
80
vol_left=len(np.where(sep_left)[0])
81
vol_right=len(np.where(sep_right)[0])
82
Avoly=(vol_left-vol_right+0.0)/(vol+0.0)
83

    
84
vol=vol*voxelsize[0]*voxelsize[1]*voxelsize[2]
85

    
86

    
87

    
88
print("Projecting on 2D...")
89

    
90
def project2D(im3):
91
        s2=(s[1],s[0])
92
        im2=np.zeros(s2, dtype='uint8')
93
        for i in range(s[0]):
94
                im2[:,i]=np.array([max(im3[i,j,:]) for j in range(s[1])])
95
        return im2
96

    
97
def project2D_hightmap(im3):
98
        s2=(s[1],s[0])
99
        im2=np.zeros(s2, dtype='uint8')
100
        for i in range(s[0]):
101
                im2[:,i]=np.array([list(im3[i,j,:]).index(max(im3[i,j,:])) for j in range(s[1])])
102
        return im2
103

    
104

    
105
sepale2D=project2D(sepale)
106
imsave2D(projected2Dimage, sepale2D)
107

    
108
'''
109
sepale2D=project2D_hightmap(sepale)
110
imsave2D(projected2Dimage_hight, sepale2D)
111
'''
112

    
113

    
114
print('Extracting principal section curves...')
115

    
116
# longitudinal section
117
x_section = sepale[int(s[0]/2), :, :]
118
xup_filename = outputdir+imnameroot+'_section_xup.txt'
119
xdown_filename = outputdir+imnameroot+'_section_xdown.txt'
120
x_filename = outputdir+imnameroot+'_section_x.txt'
121
x_pixelsize = (voxelsize[1],voxelsize[2])
122
xup, xdown = extract_curves(x_section)
123

    
124
write_curve(xup, x_pixelsize, xup_filename)
125

    
126

    
127
# transversal section
128
y_section = sepale[:,int(s[1]/2), :]
129
yup_filename = outputdir+imnameroot+'_section_yup.txt'
130
ydown_filename = outputdir+imnameroot+'_section_ydown.txt'
131
y_filename = outputdir+imnameroot+'_section_y.txt'
132
y_pixelsize = (voxelsize[0],voxelsize[2])
133
yup, ydown = extract_curves(y_section)
134

    
135
write_curve(yup, y_pixelsize, yup_filename)
136

    
137

    
138
# 
139
# ----------------------------------------
140
print('Analyzing principal section curves...')
141

    
142
if not(os.path.isfile(outputfile)):
143
        FILE = open(outputfile,"w")
144
        FILE.write('Sepal;CurvedLength;FlatLength;LHeight;LR;LR21;LR22;LR41;LR423;LR44;CurvedWidth;FlatWidth;THeight;TR;TR21;TR22;TR41;TR423;TR44;volume;AsymL;AsymT\n')
145
        FILE.close()
146

    
147
FILE = open(outputfile,"a")
148
FILE.write(imnameroot+';')
149

    
150
print("Longitudinal section:")
151
outfile=outroot+'_up_longitudinal.csv'
152
graph_name= outputdir+imnameroot+'_up_longitudinal.png'
153

    
154
length_x, flatlength_x, height_x, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, xup, x_pixelsize, outfile, "Longitudinal", graph_name)
155
os.system("rm "+outfile)
156

    
157
FILE.write(str(length_x)+';'+str(flatlength_x)+';'+str(height_x)+';'+str(R)+';'+str(R21)+';'+str(R22)+';'+str(R41)+';'+str(R423)+';'+str(R44)+';')
158

    
159
print("Transversal section:")
160
outfile=outroot+'_up_transversal.csv'
161
graph_name= outputdir+imnameroot+'_up_transversal.png'
162
length_y, flatlength_y, height_y, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, yup, y_pixelsize, outfile, "Transversal", graph_name)
163
os.system("rm "+outfile)
164

    
165
FILE.write(str(length_y)+';'+str(flatlength_y)+';'+str(height_y)+';'+str(R)+';'+str(R21)+';'+str(R22)+';'+str(R41)+';'+str(R423)+';'+str(R44)+';')
166

    
167
FILE.write(str(vol)+';'+str(Avoly)+';'+str(Avolx)+'\n')