Statistiques
| Révision :

root / bin / image2geometry / measure_sepal.py @ 6

Historique | Voir | Annoter | Télécharger (4,43 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
print(argv)
42

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

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

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

    
51
outname = outputfile.split('/')[-1]
52
outroot = outname.split('.')[0]
53

    
54
print("----------------------")
55
print("reading the file ", filename)
56
print("----------------------")
57
sepale=imread(filename)
58
voxelsize = sepale.voxelsize
59
print("voxelsize=",voxelsize)
60
s = sepale.shape
61
print("shape=",s)
62

    
63
print("Computing the volume and volumic asymmetry...")
64
vol=len(np.where(sepale)[0])
65

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

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

    
80
vol=vol*voxelsize[0]*voxelsize[1]*voxelsize[2]
81

    
82
print("Projecting on 2D...")
83

    
84
def project2D(im3):
85
        s2=(s[1],s[0])
86
        im2=np.zeros(s2, dtype='uint8')
87
        for i in range(s[0]):
88
                im2[:,i]=np.array([max(im3[i,j,:]) for j in range(s[1])])
89
        return im2
90

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

    
98

    
99
sepale2D=project2D(sepale)
100
imsave2D(projected2Dimage, sepale2D)
101

    
102
'''
103
sepale2D=project2D_hightmap(sepale)
104
imsave2D(projected2Dimage_hight, sepale2D)
105
'''
106

    
107

    
108
print('Extracting principal section curves...')
109

    
110
# longitudinal section
111
x_section = sepale[int(s[0]/2), :, :]
112
xup_filename = outputdir+imnameroot+'_section_xup.txt'
113
xdown_filename = outputdir+imnameroot+'_section_xdown.txt'
114
x_filename = outputdir+imnameroot+'_section_x.txt'
115
x_pixelsize = (voxelsize[1],voxelsize[2])
116
xup, xdown = extract_curves(x_section)
117

    
118
write_curve(xup, x_pixelsize, xup_filename)
119

    
120

    
121
# transversal section
122
y_section = sepale[:,int(s[1]/2), :]
123
yup_filename = outputdir+imnameroot+'_section_yup.txt'
124
ydown_filename = outputdir+imnameroot+'_section_ydown.txt'
125
y_filename = outputdir+imnameroot+'_section_y.txt'
126
y_pixelsize = (voxelsize[0],voxelsize[2])
127
yup, ydown = extract_curves(y_section)
128

    
129
write_curve(yup, y_pixelsize, yup_filename)
130

    
131

    
132
# 
133
# ----------------------------------------
134
print('Analyzing principal section curves...')
135

    
136
if not(os.path.isfile(outputfile)):
137
        FILE = open(outputfile,"w")
138
        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')
139
        FILE.close()
140

    
141
FILE = open(outputfile,"a")
142
FILE.write(imnameroot+';')
143

    
144
outfile=outroot+'_up_longitudinal.csv'
145
graph_name= outputdir+imnameroot+'_up_longitudinal.png'
146
length_x, flatlength_x, height_x, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, xup, x_pixelsize, outfile, graph_name)
147
os.system("rm "+outfile)
148

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

    
151
outfile=outroot+'_up_transversal.csv'
152
graph_name= outputdir+imnameroot+'_up_transversal.png'
153
length_y, flatlength_y, height_y, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, yup, y_pixelsize, outfile, graph_name)
154
os.system("rm "+outfile)
155

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

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