UserExamples/SIRACalibration: analyze_sira.py

File analyze_sira.py, 3.5 kB (added by andreas, 4 years ago)
Line 
1 # calibrate image from SIRA standard
2 # jrminter at rochester dot rr dot com
3 # version 2 - graphical version
4 # with help from Andreas Schenk's 'extract sub-image' tutorial
5 # execute with 'giplt analyze_sira'
6 from ex.gui.inputdialog import *
7 from gui import Procedure
8 from math import sqrt
9 from ex import Lattice,ReciprocalUnitCell,SpatialUnitCell
10 from ex.gui import LatticeOverlay,UnitCellOverlay
11 from alg import FFT,Conjugate,PeakSearch,HighPassFilter
12 from iplt import PeakList
13 from ex.alg import LatticeFilter, GaussianLatticeFilter, LatticeSearch, PredictLatticePoints, LatticeGaussianExtract, RefineLattice
14
15 def load_image():
16         global image,viewer,size_fft,space,xc,yc,strFileName,vwr_ps,ps_crp,img_crp,vwr_crp
17         load_dialog=InputDialog("Load image")
18         load_dialog.AddPath("Filename:","open")
19         if load_dialog.ShowModal():
20                 strFileName=load_dialog.GetData()[0]
21                 image=LoadImage(strFileName)
22                 extent=image.GetExtent()
23                 width=extent.GetSize()[0]
24                 xc=int(width/2)
25                 height=extent.GetSize()[1]
26                 yc=int(height/2)
27                 min_val=min(width,height)
28                 i=2
29                 size_fft=32
30                 for i in range(10):
31                         val = 2 * size_fft
32                         if val <= min_val:
33                                 size_fft = val
34                                
35                 viewer=Viewer(image)
36                 return True
37         return False
38                
39 def get_spacing():
40         global image,viewer,size_fft,space,xc,yc,strFileName,vwr_ps,ps_crp,img_crp,vwr_crp
41         space=1000000./2160.
42         strSpace='%.3lf' % space
43         space=float(strSpace)
44         spacing_dialog=InputDialog("Lattice Spacing:")
45         spacing_dialog.AddFloat("nm/px: ", space)
46         if spacing_dialog.ShowModal():
47                 data=spacing_dialog.GetData()
48                 space = data[0]
49                 return True
50         return False
51
52 def analyze_image():
53         global image,viewer,size_fft,space,xc,yc,strFileName,vwr_ps,ps_crp,img_crp,vwr_crp
54         ori_float=Vec2(float(0), float(0))
55         half_size=size_fft/2
56         x0=xc-half_size
57         x1=xc+half_size-1
58         y0=yc-half_size
59         y1=yc+half_size-1
60         img_crp=image.Extract(Extent(Point(x0,y0),Point(x1,y1)))
61         vwr_crp=Viewer(img_crp)
62         #
63         # compute the power spectrum
64         #
65         ps_crp=img_crp.Apply(alg.PowerSpectrum())
66         vwr_ps=Viewer(ps_crp)
67         #
68         # search for the lattice
69         #
70         lattice_search = LatticeSearch()
71         # lattice_search.AddPeakSearchExclusion(Extent(Size(5,5),Point(0,0)))
72         ps_crp.Apply(lattice_search)
73         vi = lattice_search.GetVectorImage();
74         lat=lattice_search.GetLattice()
75         lat=Lattice(lat.GetSecond(),-lat.GetFirst(),lat.GetOffset())
76         comp=lat.CalcComponents(ori_float)
77         ori=lat.CalcPosition(Vec2(round(comp[0]),round(comp[1])))
78         lat.SetOffset(ori_float)
79         ov=LatticeOverlay(lat)
80         vwr_ps.AddOverlay(ov)
81         #
82         # get the X and Y RL basis vectors
83         # and compute their lenght
84         #
85         x=lat.GetFirst()
86         y=lat.GetSecond()
87         x1=x[0]
88         x2=x[1]
89         xl=sqrt(x1*x1+x2*x2)
90         y1=y[0]
91         y2=y[1]
92         yl=sqrt(y1*y1+y2*y2)
93         strXl = 'X-axis RL length: %9.4f [px]' % xl
94         strYl = 'Y-axis RL length: %9.4f [px]' % yl
95         print strXl
96         print strYl
97         fXs=xl*space/float(size_fft)
98         fYs=yl*space/float(size_fft)
99         strXs = 'X-axis calibration: %9.4f [nm/px]' % fXs
100         strYs = 'Y-axis calibration: %9.4f [nm/px]' % fYs
101         strAr = '      Aspect ratio: %9.4f        ' % (fXs/fYs)
102         print strXs
103         print strYs
104         print strAr
105         lFile=len(strFileName)
106         strName=strFileName[0:lFile-4]
107         strOutFile=strName+'_ana.txt'
108         #write the results to a file
109         outFile = open(strOutFile, 'w')
110         outFile.write('Analysis of '+strName+'\n')
111         outFile.write(strXl+'\n')
112         outFile.write(strYl+'\n')
113         outFile.write(strXs+'\n')
114         outFile.write(strYs+'\n')
115         outFile.write(strAr+'\n')
116         outFile.close()
117         return False
118
119 ana_proc=Procedure('Image calibration')
120 step_1=ana_proc.AddStep(load_image,'Load image',[])
121 step_2=ana_proc.AddStep(get_spacing,'Get Spacing',[])
122 step_3=ana_proc.AddStep(analyze_image,'Analyze Image',[step_1])
123 Shell.AddProcedure(ana_proc)