Statistiques
| Révision :

root / bin / image2geometry / orient_sepal.py @ 16

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

1 1 akiss
#!/usr/bin/env python
2 1 akiss
3 1 akiss
'''
4 1 akiss
Usage:
5 1 akiss
orient_object.py filename output.txt
6 1 akiss

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

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

15 1 akiss
'''
16 1 akiss
17 1 akiss
from sys import argv
18 1 akiss
import os
19 1 akiss
import numpy as np
20 1 akiss
from scipy.ndimage.morphology import binary_closing, binary_opening
21 1 akiss
22 4 akiss
from titk_tools.io import imread, imsave, tfread, SpatialImage
23 10 akiss
from titk_tools.registration import isotropic_resampling_seg, isotropic_resampling
24 10 akiss
from titk_tools.registration import apply_transfo_on_seg1, apply_transfo_on_image
25 1 akiss
26 1 akiss
from bib_sepals import *
27 1 akiss
28 1 akiss
print('============== orient_sepal.py =================')
29 1 akiss
30 1 akiss
filename = argv[1]
31 4 akiss
outputdir=argv[2]+'/'
32 1 akiss
33 10 akiss
segmented=True
34 10 akiss
35 10 akiss
if len(argv)>3:
36 10 akiss
        imagetype=argv[3]
37 10 akiss
        if imagetype=='grey':
38 10 akiss
                segmented=False
39 10 akiss
40 1 akiss
print(argv)
41 10 akiss
print('segmented=',segmented)
42 1 akiss
43 1 akiss
imname = filename.split('/')[-1]
44 1 akiss
imnameroot = imname.split('.')[0]
45 1 akiss
46 1 akiss
outfilename = outputdir+imnameroot+'_oriented.inr.gz'
47 1 akiss
48 4 akiss
# read the image
49 10 akiss
img=imread(filename)
50 1 akiss
# resample isotropically
51 1 akiss
print("----------------------")
52 1 akiss
print("isotropic resampling of : filename=", filename)
53 1 akiss
print("----------------------")
54 10 akiss
voxelsize = img.voxelsize
55 1 akiss
print("voxelsize=",voxelsize)
56 10 akiss
s = img.shape
57 10 akiss
imgtype=img.dtype
58 1 akiss
print("shape=",s)
59 1 akiss
60 1 akiss
radius=int(voxelsize[2]/voxelsize[0])
61 1 akiss
isovs=(voxelsize[0]*voxelsize[1]*voxelsize[2])**(1.0/3)
62 1 akiss
#isovs=voxelsize[0]
63 1 akiss
print("new voxelsize=",isovs)
64 1 akiss
65 10 akiss
sepale=SpatialImage(img>0, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
66 10 akiss
if segmented:
67 10 akiss
        sepale=isotropic_resampling_seg(sepale,isovs)
68 10 akiss
else:
69 10 akiss
        img=isotropic_resampling(img,isovs)
70 10 akiss
        sepale=isotropic_resampling_seg(sepale,isovs)
71 1 akiss
72 1 akiss
s = sepale.shape
73 1 akiss
voxelsize = sepale.voxelsize
74 1 akiss
print('new shape', s)
75 1 akiss
76 4 akiss
77 1 akiss
# alongate the target image so that a sepal originally along the diagonal fits into the target image
78 1 akiss
print("----------------------")
79 1 akiss
print("elongating field : filename=", filename)
80 1 akiss
print("----------------------")
81 1 akiss
s_addy=int((np.sqrt(s[0]*s[1]*2)-np.sqrt(s[0]*s[1]))/2)
82 1 akiss
s_addz=int(s[2]*0.20)
83 4 akiss
sepale=add_side_slices(sepale, 0, 0, s_addy, s_addy, s_addz, s_addz)
84 10 akiss
if not segmented :
85 10 akiss
        img=add_side_slices(img, 0, 0, s_addy, s_addy, s_addz, s_addz)
86 1 akiss
87 4 akiss
'''
88 1 akiss
# smooth it with opening
89 4 akiss
#sepale=imread(isoname)
90 1 akiss
s = sepale.shape
91 1 akiss
voxelsize = sepale.voxelsize
92 1 akiss
struct_el=struct_element_sphere(radius)
93 1 akiss
sepale = binary_closing(sepale, structure=struct_el, iterations=int(radius))
94 1 akiss
struct_el=struct_element_sphere(radius/2)
95 1 akiss
sepale = binary_closing(sepale, structure=struct_el, iterations=int(radius/2))
96 1 akiss
sepale=SpatialImage(sepale*255, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
97 4 akiss
#imsave(sname, sepale)
98 4 akiss
'''
99 1 akiss
100 1 akiss
# put the center of mass in the center of the image
101 10 akiss
sepale_centered=center_on_cm(sepale, volumic=True)
102 10 akiss
if not segmented:
103 10 akiss
        img=center_on_cm(img, volumic=True)
104 1 akiss
105 1 akiss
# compute principal directions
106 10 akiss
pv_flo=principal_directions(sepale_centered, volumic=True)
107 1 akiss
print(pv_flo)
108 1 akiss
109 1 akiss
# sepals originally are oriented with top in more-or less "up" direction on the images
110 1 akiss
# while the eigenvectors' orientation is arbitrary
111 1 akiss
# --> do not change the original rough orientation
112 1 akiss
113 1 akiss
# check logitudinal direction:
114 1 akiss
if pv_flo[0][1]<0 :
115 1 akiss
        pv_flo[0] = -pv_flo[0]
116 1 akiss
117 1 akiss
# check the transversal direction
118 1 akiss
if pv_flo[1][0]<0 :
119 1 akiss
        pv_flo[1] = -pv_flo[1]
120 1 akiss
121 1 akiss
print("New pv_flo",pv_flo)
122 1 akiss
123 1 akiss
'''
124 1 akiss
pv_ref = [np.array([0,-1,0]),
125 1 akiss
np.array([-1,0,0])]
126 1 akiss

127 1 akiss
'''
128 1 akiss
pv_ref = [np.array([0,1,0]),
129 1 akiss
np.array([1,0,0])]
130 1 akiss
131 1 akiss
132 1 akiss
# compute superposing transformation
133 1 akiss
trsf_file=outputdir+imnameroot+'_rigid.txt'
134 1 akiss
write_superposing_transfo(pv_flo, pv_ref, sepale_centered, trsf_file)
135 1 akiss
136 4 akiss
trsf=tfread(trsf_file)
137 4 akiss
os.system("rm "+trsf_file)
138 4 akiss
139 1 akiss
# apply transformation
140 10 akiss
if segmented :
141 10 akiss
        oriented=apply_transfo_on_seg1(trsf, sepale_centered)
142 10 akiss
else :
143 10 akiss
        oriented=apply_transfo_on_image(trsf, img)
144 1 akiss
145 1 akiss
# smooth the transformed image
146 4 akiss
#sepale = binary_closing(oriented, structure=struct_el, iterations=radius)
147 1 akiss
148 1 akiss
# and save the oriented sepal
149 4 akiss
sepale_filename = outputdir+imnameroot+'_oriented.inr.gz'
150 10 akiss
if segmented :
151 10 akiss
        sepale=SpatialImage(oriented*255, dtype='uint8', voxelsize=voxelsize, origin=[0, 0, 0])
152 10 akiss
else:
153 10 akiss
        sepale=SpatialImage(oriented, dtype=imgtype, voxelsize=voxelsize, origin=[0, 0, 0])
154 10 akiss
155 1 akiss
imsave(sepale_filename, sepale)