Statistiques
| Révision :

root / bin / image2geometry / measure_sepal.py @ 1

Historique | Voir | Annoter | Télécharger (3,73 ko)

1
#!/usr/bin/env python
2

    
3
'''
4
Usage:
5
measure_sepal.py filename output.csv
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
outputfile=outdir+"/"+outdir+".csv"
36

    
37

    
38
print(argv)
39

    
40
imname = filename.split('/')[-1]
41
imnameroot = imname.split('.')[0]
42

    
43
outputdir = imnameroot+'_sections/'
44
projected2Dimage=outdir+"/"+imnameroot+"2D.png"
45
projected2Dimage_hight=outdir+"/hight_"+imnameroot+"2D.png"
46

    
47
os.system("mkdir -p "+outputdir)
48

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

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

    
61
print("Projecting on 2D...")
62

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

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

    
77

    
78
sepale2D=project2D(sepale)
79
imsave2D(projected2Dimage, sepale2D)
80

    
81
'''
82
sepale2D=project2D_hightmap(sepale)
83
imsave2D(projected2Dimage_hight, sepale2D)
84
'''
85

    
86
print('Extracting principal section curves...')
87

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

    
96
write_curve(xup, x_pixelsize, xup_filename)
97

    
98

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

    
107
write_curve(yup, y_pixelsize, yup_filename)
108

    
109

    
110
# 
111
# ----------------------------------------
112
print('Analyzing principal section curves...')
113

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

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

    
122
outfile=outroot+'_up_longitudinal.csv'
123
graph_name= outputdir+imnameroot+'_up_longitudinal.png'
124
length_x, flatlength_x, height_x, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, xup, x_pixelsize, outfile, graph_name)
125

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

    
128
outfile=outroot+'_up_transversal.csv'
129
graph_name= outputdir+imnameroot+'_up_transversal.png'
130
length_y, flatlength_y, height_y, R, R21, R22, R41, R423, R44=analyze_curve1(imnameroot, yup, y_pixelsize, outfile, graph_name)
131

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