root / bin / image2geometry / orient_sepal.py @ 4
Historique | Voir | Annoter | Télécharger (3,49 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, tfread, SpatialImage |
23 |
from titk_tools.registration import isotropic_resampling_seg |
24 |
from titk_tools.registration import apply_transfo_on_seg1 |
25 |
|
26 |
from bib_sepals import * |
27 |
|
28 |
print('============== orient_sepal.py =================')
|
29 |
|
30 |
filename = argv[1]
|
31 |
outputdir=argv[2]+'/' |
32 |
|
33 |
print(argv) |
34 |
|
35 |
imname = filename.split('/')[-1] |
36 |
imnameroot = imname.split('.')[0] |
37 |
|
38 |
outfilename = outputdir+imnameroot+'_oriented.inr.gz'
|
39 |
|
40 |
# read the image
|
41 |
sepale=imread(filename) |
42 |
# resample isotropically
|
43 |
print("----------------------")
|
44 |
print("isotropic resampling of : filename=", filename)
|
45 |
print("----------------------")
|
46 |
voxelsize = sepale.voxelsize |
47 |
print("voxelsize=",voxelsize)
|
48 |
s = sepale.shape |
49 |
print("shape=",s)
|
50 |
|
51 |
radius=int(voxelsize[2]/voxelsize[0]) |
52 |
isovs=(voxelsize[0]*voxelsize[1]*voxelsize[2])**(1.0/3) |
53 |
#isovs=voxelsize[0]
|
54 |
print("new voxelsize=",isovs)
|
55 |
|
56 |
sepale=SpatialImage(sepale>0, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0]) |
57 |
sepale=isotropic_resampling_seg(sepale,isovs) |
58 |
|
59 |
s = sepale.shape |
60 |
voxelsize = sepale.voxelsize |
61 |
print('new shape', s)
|
62 |
|
63 |
|
64 |
# alongate the target image so that a sepal originally along the diagonal fits into the target image
|
65 |
print("----------------------")
|
66 |
print("elongating field : filename=", filename)
|
67 |
print("----------------------")
|
68 |
s_addy=int((np.sqrt(s[0]*s[1]*2)-np.sqrt(s[0]*s[1]))/2) |
69 |
s_addz=int(s[2]*0.20) |
70 |
sepale=add_side_slices(sepale, 0, 0, s_addy, s_addy, s_addz, s_addz) |
71 |
|
72 |
'''
|
73 |
# smooth it with opening
|
74 |
#sepale=imread(isoname)
|
75 |
s = sepale.shape
|
76 |
voxelsize = sepale.voxelsize
|
77 |
struct_el=struct_element_sphere(radius)
|
78 |
sepale = binary_closing(sepale, structure=struct_el, iterations=int(radius))
|
79 |
struct_el=struct_element_sphere(radius/2)
|
80 |
sepale = binary_closing(sepale, structure=struct_el, iterations=int(radius/2))
|
81 |
sepale=SpatialImage(sepale*255, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
|
82 |
#imsave(sname, sepale)
|
83 |
'''
|
84 |
|
85 |
# put the center of mass in the center of the image
|
86 |
sepale_centered=center_on_cm(sepale) |
87 |
|
88 |
# compute principal directions
|
89 |
pv_flo=principal_directions(sepale_centered*255)
|
90 |
print(pv_flo) |
91 |
|
92 |
# sepals originally are oriented with top in more-or less "up" direction on the images
|
93 |
# while the eigenvectors' orientation is arbitrary
|
94 |
# --> do not change the original rough orientation
|
95 |
|
96 |
# check logitudinal direction:
|
97 |
if pv_flo[0][1]<0 : |
98 |
pv_flo[0] = -pv_flo[0] |
99 |
|
100 |
# check the transversal direction
|
101 |
if pv_flo[1][0]<0 : |
102 |
pv_flo[1] = -pv_flo[1] |
103 |
|
104 |
print("New pv_flo",pv_flo)
|
105 |
|
106 |
'''
|
107 |
pv_ref = [np.array([0,-1,0]),
|
108 |
np.array([-1,0,0])]
|
109 |
|
110 |
'''
|
111 |
pv_ref = [np.array([0,1,0]), |
112 |
np.array([1,0,0])] |
113 |
|
114 |
|
115 |
# compute superposing transformation
|
116 |
trsf_file=outputdir+imnameroot+'_rigid.txt'
|
117 |
write_superposing_transfo(pv_flo, pv_ref, sepale_centered, trsf_file) |
118 |
|
119 |
trsf=tfread(trsf_file) |
120 |
os.system("rm "+trsf_file)
|
121 |
|
122 |
# apply transformation
|
123 |
oriented=apply_transfo_on_seg1(trsf, sepale_centered) |
124 |
|
125 |
# smooth the transformed image
|
126 |
#sepale = binary_closing(oriented, structure=struct_el, iterations=radius)
|
127 |
|
128 |
# and save the oriented sepal
|
129 |
sepale_filename = outputdir+imnameroot+'_oriented.inr.gz'
|
130 |
sepale=SpatialImage(oriented*255, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0]) |
131 |
imsave(sepale_filename, sepale) |
132 |
|