Statistiques
| Révision :

root / bin / image2geometry / measure_sepal.py @ 4

Historique | Voir | Annoter | Télécharger (3,81 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("Projecting on 2D...")
64

    
65
def project2D(im3):
66
        s2=(s[1],s[0])
67
        im2=np.zeros(s2, dtype='uint8')
68
        for i in range(s[0]):
69
                im2[:,i]=np.array([max(im3[i,j,:]) for j in range(s[1])])
70
        return im2
71

    
72
def project2D_hightmap(im3):
73
        s2=(s[1],s[0])
74
        im2=np.zeros(s2, dtype='uint8')
75
        for i in range(s[0]):
76
                im2[:,i]=np.array([list(im3[i,j,:]).index(max(im3[i,j,:])) for j in range(s[1])])
77
        return im2
78

    
79

    
80
sepale2D=project2D(sepale)
81
imsave2D(projected2Dimage, sepale2D)
82

    
83
'''
84
sepale2D=project2D_hightmap(sepale)
85
imsave2D(projected2Dimage_hight, sepale2D)
86
'''
87

    
88
print('Extracting principal section curves...')
89

    
90
# longitudinal section
91
x_section = sepale[int(s[0]/2), :, :]
92
xup_filename = outputdir+imnameroot+'_section_xup.txt'
93
xdown_filename = outputdir+imnameroot+'_section_xdown.txt'
94
x_filename = outputdir+imnameroot+'_section_x.txt'
95
x_pixelsize = (voxelsize[1],voxelsize[2])
96
xup, xdown = extract_curves(x_section)
97

    
98
write_curve(xup, x_pixelsize, xup_filename)
99

    
100

    
101
# transversal section
102
y_section = sepale[:,int(s[1]/2), :]
103
yup_filename = outputdir+imnameroot+'_section_yup.txt'
104
ydown_filename = outputdir+imnameroot+'_section_ydown.txt'
105
y_filename = outputdir+imnameroot+'_section_y.txt'
106
y_pixelsize = (voxelsize[0],voxelsize[2])
107
yup, ydown = extract_curves(y_section)
108

    
109
write_curve(yup, y_pixelsize, yup_filename)
110

    
111

    
112
# 
113
# ----------------------------------------
114
print('Analyzing principal section curves...')
115

    
116
if not(os.path.isfile(outputfile)):
117
        FILE = open(outputfile,"w")
118
        FILE.write('Sepal;CurvedLength;FlatLength;LHeight;LR;LR21;LR22;LR41;LR423;LR44;CurvedWidth;FlatWidth;THeight;TR;TR21;TR22;TR41;TR423;TR44\n')
119
        FILE.close()
120

    
121
FILE = open(outputfile,"a")
122
FILE.write(imnameroot+';')
123

    
124
outfile=outroot+'_up_longitudinal.csv'
125
graph_name= outputdir+imnameroot+'_up_longitudinal.png'
126
length_x, flatlength_x, height_x, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, xup, x_pixelsize, outfile, graph_name)
127
os.system("rm "+outfile)
128

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

    
131
outfile=outroot+'_up_transversal.csv'
132
graph_name= outputdir+imnameroot+'_up_transversal.png'
133
length_y, flatlength_y, height_y, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, yup, y_pixelsize, outfile, graph_name)
134
os.system("rm "+outfile)
135

    
136
FILE.write(str(length_y)+';'+str(flatlength_y)+';'+str(height_y)+';'+str(R)+';'+str(R21)+';'+str(R22)+';'+str(R41)+';'+str(R423)+';'+str(R44)+'\n')
137