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