Statistiques
| Révision :

root / bin / image2geometry / orient_sepal.py @ 1

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

1
#!/usr/bin/env python
2

    
3
'''
4
Usage:
5
orient_object.py filename output.txt
6

7
- filename = path and name of the stack file containing the object (background=0)
8

9
Output:
10
the image with the object reoriented:
11
- CM in the middle of the image
12
- first principal axis along y
13
- second principal axis along x
14

15
'''
16

    
17
from sys import argv
18
import os
19
import numpy as np
20
from scipy.ndimage.morphology import binary_closing, binary_opening
21

    
22
from titk_tools.io import imread, imsave, SpatialImage
23
from titk_tools.rw.registration import isotropic_resampling_seg
24

    
25
from bib_sepals import *
26

    
27

    
28

    
29
print('============== orient_sepal.py =================')
30

    
31
filename = argv[1]
32

    
33
print(argv)
34

    
35
imname = filename.split('/')[-1]
36
imnameroot = imname.split('.')[0]
37

    
38
outputdir = imnameroot+'_oriented/'
39
os.system("mkdir -p "+outputdir)
40

    
41
isodir = imnameroot+'_iso/'
42
os.system("mkdir -p "+isodir)
43

    
44
#outname = outputfile.split('/')[-1]
45
#outroot = outname.split('.')[0]
46

    
47
inrname = outputdir+imnameroot+'.inr.gz'
48
outfilename = outputdir+imnameroot+'_oriented.inr.gz'
49
isoname = isodir+imnameroot+'_iso.inr.gz'
50
sname = isodir+imnameroot+'_iso_closed.inr.gz'
51

    
52
# resample isotropically
53
print("----------------------")
54
print("isotropic resampling of : filename=", filename)
55
print("----------------------")
56
sepale=imread(filename)
57
voxelsize = sepale.voxelsize
58
print("voxelsize=",voxelsize)
59
s = sepale.shape
60
print("shape=",s)
61

    
62
radius=int(voxelsize[2]/voxelsize[0])
63
isovs=(voxelsize[0]*voxelsize[1]*voxelsize[2])**(1.0/3)
64
#isovs=voxelsize[0]
65
print("new voxelsize=",isovs)
66

    
67
sepale=SpatialImage(sepale>0, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
68
imsave(inrname, sepale)
69
isotropic_resampling_seg(inrname, isoname, isovs)
70

    
71
sepale=imread(isoname)
72
s = sepale.shape
73
voxelsize = sepale.voxelsize
74
print('new voxelsize read=', voxelsize)
75
print('new shape', s)
76

    
77
# alongate the target image so that a sepal originally along the diagonal fits into the target image
78
print("----------------------")
79
print("elongating field : filename=", filename)
80
print("----------------------")
81
s_addy=int((np.sqrt(s[0]*s[1]*2)-np.sqrt(s[0]*s[1]))/2)
82
#s_addy=int(np.sqrt(s[0]*s[1])(1.-np.sqrt(2.))/2. * 1.05)
83
s_addz=int(s[2]*0.20)
84
sepale_long=add_side_slices(sepale, 0, 0, s_addy, s_addy, s_addz, s_addz)
85
sepale=SpatialImage(sepale_long, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
86
imsave(isoname, sepale)
87
print('--------------------')
88

    
89
# smooth it with opening
90
sepale=imread(isoname)
91
s = sepale.shape
92
voxelsize = sepale.voxelsize
93
struct_el=struct_element_sphere(radius)
94
sepale = binary_closing(sepale, structure=struct_el, iterations=int(radius))
95
struct_el=struct_element_sphere(radius/2)
96
sepale = binary_closing(sepale, structure=struct_el, iterations=int(radius/2))
97
sepale=SpatialImage(sepale*255, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
98
imsave(sname, sepale)
99

    
100

    
101
# put the center of mass in the center of the image
102
sepale_centered=center_on_cm(sepale)
103
voxelsize = sepale_centered.resolution
104
sepale_centered_filename = outputdir+imnameroot+'_centered.inr.gz'
105
imsave(sepale_centered_filename, sepale_centered)
106

    
107

    
108
# compute principal directions
109
pv_flo=principal_directions(sepale_centered)
110
print(pv_flo)
111

    
112
# sepals originally are oriented with top in more-or less "up" direction on the images
113
# while the eigenvectors' orientation is arbitrary
114
# --> do not change the original rough orientation
115

    
116
# check logitudinal direction:
117
if pv_flo[0][1]<0 :
118
        pv_flo[0] = -pv_flo[0]
119

    
120
# check the transversal direction
121
if pv_flo[1][0]<0 :
122
        pv_flo[1] = -pv_flo[1]
123

    
124
print("New pv_flo",pv_flo)
125

    
126
'''
127
pv_ref = [np.array([0,-1,0]),
128
np.array([-1,0,0])]
129

130
'''
131
pv_ref = [np.array([0,1,0]),
132
np.array([1,0,0])]
133

    
134

    
135
# compute superposing transformation
136
trsf_file=outputdir+imnameroot+'_rigid.txt'
137
write_superposing_transfo(pv_flo, pv_ref, sepale_centered, trsf_file)
138

    
139
# apply transformation
140
sepale_filename = outputdir+imnameroot+'_oriented.inr.gz'
141
apply_transfo_on_seg1(trsf_file, sepale_centered_filename, sepale_filename)
142

    
143
# smooth the transformed image
144
sepale=imread(sepale_filename)
145
sepale = binary_closing(sepale, structure=struct_el, iterations=radius)
146

    
147
# and save the oriented sepal
148
sepale=SpatialImage(sepale*255, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
149
imsave(sepale_filename, sepale)
150