root / bin / image2geometry / measure_sepal.py @ 10
Historique | Voir | Annoter | Télécharger (4,48 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 |
os.system("mkdir -p "+symdir)
|
52 |
|
53 |
outname = outputfile.split('/')[-1] |
54 |
outroot = outname.split('.')[0] |
55 |
|
56 |
print("----------------------")
|
57 |
print("reading the file ", filename)
|
58 |
print("----------------------")
|
59 |
sepale=imread(filename) |
60 |
voxelsize = sepale.voxelsize |
61 |
print("voxelsize=",voxelsize)
|
62 |
s = sepale.shape |
63 |
imtype=sepale.dtype |
64 |
print("shape=",s)
|
65 |
|
66 |
print("Computing the volume and volumic asymmetry...")
|
67 |
vol=len(np.where(sepale)[0]) |
68 |
|
69 |
# left-right symmetry
|
70 |
sep_left=sepale[:int(s[0]/2), :, :] |
71 |
sep_right=sepale[int(s[0]/2):, :, :] |
72 |
vol_left=len(np.where(sep_left)[0]) |
73 |
vol_right=len(np.where(sep_right)[0]) |
74 |
Avolx=(vol_left-vol_right+0.0)/(vol+0.0) |
75 |
|
76 |
# top-base symmetry
|
77 |
sep_left=sepale[:, :int(s[1]/2), :] |
78 |
sep_right=sepale[:, int(s[1]/2):, :] |
79 |
vol_left=len(np.where(sep_left)[0]) |
80 |
vol_right=len(np.where(sep_right)[0]) |
81 |
Avoly=(vol_left-vol_right+0.0)/(vol+0.0) |
82 |
|
83 |
vol=vol*voxelsize[0]*voxelsize[1]*voxelsize[2] |
84 |
|
85 |
|
86 |
|
87 |
print("Projecting on 2D...")
|
88 |
|
89 |
def project2D(im3): |
90 |
s2=(s[1],s[0]) |
91 |
im2=np.zeros(s2, dtype='uint8')
|
92 |
for i in range(s[0]): |
93 |
im2[:,i]=np.array([max(im3[i,j,:]) for j in range(s[1])]) |
94 |
return im2
|
95 |
|
96 |
def project2D_hightmap(im3): |
97 |
s2=(s[1],s[0]) |
98 |
im2=np.zeros(s2, dtype='uint8')
|
99 |
for i in range(s[0]): |
100 |
im2[:,i]=np.array([list(im3[i,j,:]).index(max(im3[i,j,:])) for j in range(s[1])]) |
101 |
return im2
|
102 |
|
103 |
|
104 |
sepale2D=project2D(sepale) |
105 |
imsave2D(projected2Dimage, sepale2D) |
106 |
|
107 |
'''
|
108 |
sepale2D=project2D_hightmap(sepale)
|
109 |
imsave2D(projected2Dimage_hight, sepale2D)
|
110 |
'''
|
111 |
|
112 |
|
113 |
print('Extracting principal section curves...')
|
114 |
|
115 |
# longitudinal section
|
116 |
x_section = sepale[int(s[0]/2), :, :] |
117 |
xup_filename = outputdir+imnameroot+'_section_xup.txt'
|
118 |
xdown_filename = outputdir+imnameroot+'_section_xdown.txt'
|
119 |
x_filename = outputdir+imnameroot+'_section_x.txt'
|
120 |
x_pixelsize = (voxelsize[1],voxelsize[2]) |
121 |
xup, xdown = extract_curves(x_section) |
122 |
|
123 |
write_curve(xup, x_pixelsize, xup_filename) |
124 |
|
125 |
|
126 |
# transversal section
|
127 |
y_section = sepale[:,int(s[1]/2), :] |
128 |
yup_filename = outputdir+imnameroot+'_section_yup.txt'
|
129 |
ydown_filename = outputdir+imnameroot+'_section_ydown.txt'
|
130 |
y_filename = outputdir+imnameroot+'_section_y.txt'
|
131 |
y_pixelsize = (voxelsize[0],voxelsize[2]) |
132 |
yup, ydown = extract_curves(y_section) |
133 |
|
134 |
write_curve(yup, y_pixelsize, yup_filename) |
135 |
|
136 |
|
137 |
#
|
138 |
# ----------------------------------------
|
139 |
print('Analyzing principal section curves...')
|
140 |
|
141 |
if not(os.path.isfile(outputfile)): |
142 |
FILE = open(outputfile,"w") |
143 |
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')
|
144 |
FILE.close() |
145 |
|
146 |
FILE = open(outputfile,"a") |
147 |
FILE.write(imnameroot+';')
|
148 |
|
149 |
outfile=outroot+'_up_longitudinal.csv'
|
150 |
graph_name= outputdir+imnameroot+'_up_longitudinal.png'
|
151 |
length_x, flatlength_x, height_x, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, xup, x_pixelsize, outfile, graph_name) |
152 |
os.system("rm "+outfile)
|
153 |
|
154 |
FILE.write(str(length_x)+';'+str(flatlength_x)+';'+str(height_x)+';'+str(R)+';'+str(R21)+';'+str(R22)+';'+str(R41)+';'+str(R423)+';'+str(R44)+';') |
155 |
|
156 |
outfile=outroot+'_up_transversal.csv'
|
157 |
graph_name= outputdir+imnameroot+'_up_transversal.png'
|
158 |
length_y, flatlength_y, height_y, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, yup, y_pixelsize, outfile, graph_name) |
159 |
os.system("rm "+outfile)
|
160 |
|
161 |
FILE.write(str(length_y)+';'+str(flatlength_y)+';'+str(height_y)+';'+str(R)+';'+str(R21)+';'+str(R22)+';'+str(R41)+';'+str(R423)+';'+str(R44)+';') |
162 |
|
163 |
FILE.write(str(vol)+';'+str(Avoly)+';'+str(Avolx)+'\n') |