@Gaiussheh
2018-08-23T22:18:32.000000Z
字数 23028
阅读 665
Ronchi
The main part of the past two weeks' work is about the understanding of Ronchi, basically on physical principles and mathematical methods. We would like to make a check on these to see if the physical part is well-understood. If it is so then a proramming work will get started from next week.
My understanding on this topic:
One Ronchi target with grating constant :

Two Ronchi targets ,placed at the nearly same position :

Total Cost Function
Non-integer Shearing

The main steps of this project should be
By next week these works will be carried out
The main isssue if this stage of work is to acquire the phase information at the target plane from the image. We mainly used the method discribed in the report last week. The idea comes from Takeda et al.(1982)

f=np.fft.fft2(Raw) #Raw is the image with maskTransformed=np.fft.fftshift(f)Abs=abs(Transformed)
this is what we get from fft:
the two light spot indicates the main space frequiencies caused the interference, if we put the center of one of two peaks at then the interference will be filtered out and the rest information is all about the phase we want.
for i in range(0,2047):for j in range(0,2047):if (i-1023)**2*(j-1023)**2<4000000\\emperical parameter,may not be universal:Abs[i][j]=0
the result is shown:
Finding the maximun of the left half of the image will give the center if the peak
a=np.empty(shape=[2048, 2048])for i in range(0,1023):for j in range(0,2047):a[i][j]=Abs[i][j]m, n = a.shapeindex = int(a.argmax())x = int(index / n)y = index % n
for i in range(0,2047):for j in range(0,2047):if (i-x)**2+(j-y)**2>10000:Abs[i][j]=0Transformed[i][j]=0
b=f #set up a complex matrix with ffor i in range(0,2047):for j in range(0,2047):b[i][j]=0 #set b as 0for i in range(-100,100):for j in range(-100,100):b[1023+i][1023+j]=Transformed[x+i][y+j]
ff=np.fft.ifftshift(b)Result=np.fft.ifft2(ff)Phase=np.zeros((2048, 2048))for i in range(0,2047):for j in range (0,2047):Phase[i][j]=cmath.phase(cmath.log(Result[i][j]))*Mask[i][j]
This is the final information we acquire

To take a closee look:
for i in range(0,2047):for j in range(0,2046):if Phase[i][j]-Phase[i][j+1]>0.9*2*math.pi:print(i,j)for k in range(j+1,2047):Phase[i][k]+=2*math.pielif Phase[i][j]-Phase[i][j+1]<-0.9*2*math.pi:print(i,j)for k in range(j+1,2047):Phase[i][k]-=2*math.pi
The future work is listed in the order of priority:
The issue of this week is to improve the retrieved phase. The main difference of this week from last week is that a hanning window is applied. Also, The picture is not cut before fft.
window=scipy.signal.hanning(51, sym=True)for i in range(0,2048):for j in range(0,2048):if abs(i-x)>25 or abs(j-y)>25:Abs[i][j]=0Transformed[i][j]=0else:Abs[i][j]*=window[i-(x-25)]*window[j-(y-25)]Transformed[i][j]*=window[i-(x-25)]*window[j-(y-25)]
the result is shown(after centered):

One may find smaller structures within the peak. These structures had already appeared once the fft is done, which is different from the previous work. Here a diagram of fft is shown after DC filtering but before windowing.

The future works are the same with those in Report1. The cost functon is of the most importance.
I. SUMMARY
The main part of the work in the past two week focus on the Cost Function and the derivities. Also the codes are reorganized so that the optimazation can run faster. Still, p255 is used.
II. PROGRESS
The programme is sperated into two parts, the first in the Link Function, which is used to retrieve the phase and link them. In this version all the fransformations are done within a 251( direction)151( direction) square.
Link(start,end) function is to retrieve and link phase for all the pictures with p number from start to end, for example, Link(255,256) gives the linked retrieved phase of 255. This is realized by:
def Link(start,end):for i in range(start,end): # i is p numberfor j in range(1,5): # j is the arealink(15,30,i,j) # 15: the length of the mask, 30: the intensity of the DC Filter
where area range from 1 to 5 indecates the part of the picture.( area==1 indecates upper-left rectangle, etc )
link(length,Intensity,pnumber,area) function is to retrieve and link the phase of one of the 4 rectangle areas in a picture with a certain p number. This is realized by:
def link(length,Intensity,pnumber,area):Phase=getphase(length,Intensity,pnumber,area)for i in range(0,251):for j in range(0,150):if Phase[i][j]-Phase[i][j+1]>0.95*2*math.pi:for k in range(j+1,151):Phase[i][k]+=2*math.pielif Phase[i][j]-Phase[i][j+1]<-0.95*2*math.pi:for k in range(j+1,151):Phase[i][k]-=2*math.pifor j in range(0,151):for i in range(0,250):if Phase[i][j]-Phase[i+1][j]>0.95*2*math.pi:for k in range(i+1,251):Phase[k][j]+=2*math.piif Phase[i][j]-Phase[i+1][j]<-0.95*2*math.pi:for k in range(i+1,251):Phase[k][j]-=2*math.piplt.imshow(Phase)plt.colorbar()plt.axis()plt.show()np.savetxt('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\LinkedRetrivedPhase%d-%d.txt'%(pnumber,area,pnumber,area),Phase)return Phase
The code is long but the very main part is line 2 and line3-18.line 3-18 is to link phase. Line 2 is the function that can retrieve phase. The result is saved by a txt document so that during the optimization the programme can read very easily (line 23).
getphase(length,Intensity,pnumber,area) function is realized by
def getphase(length,Intensity,pnumber,area):ff=np.fft.ifftshift(Move(length,Intensity,pnumber,area))Result=np.fft.ifft2(ff)Phase=np.zeros((251, 151))Phase=math.phase(Result)plt.imshow(Phase)plt.colorbar()plt.axis()plt.show()return Phase
again only line 2 is import with the Move(length,Intensity,pnumber,area) gives centered fft result. Result gives the ifft result and phase gets the phase
Move(length,Intensity,pnumber,area) function two main functions are called, one is HanningWindow(length,Intensity,pnumber,area) which gives the windowed fft result and the other is Center(Intensity,pnumber,area) which gives the peak coordinate of fft diagram.
def Move(length,Intensity,pnumber,area):HanningWindowed=HanningWindow(length,Intensity,pnumber,area)global f # from fft, f is used to create a complex matrix.b=fx=Center(Intensity,pnumber,area)[1] # peak coordinatey=Center(Intensity,pnumber,area)[0] # peak coordinatefor i in range(-125,125):for j in range(-75,75):if -(length-1)/2<i<(length-1)/2 and -(length-1)/2<j<(length-1)/2:b[125+i][75+j]=HanningWindowed[int(y+i)][int(x+j)]else:b[125+i][75+j]=0Renormalizedc=c*255/np.max(c)cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\CenteredFFT.jpg'%(pnumber,area),Renormalizedc)return b
HanningWindow(length,Intensity,pnumber,area) function uses the result of DCFilter(Intensity,pnumber,area) which gives the result of DC Filtered fft. Also the Center(Intensity,pnumber,area) function is called.
def HanningWindow(length,Intensity,pnumber,area):windowed=DCFilter(Intensity,pnumber,area)x=Center(Intensity,pnumber,area)[1]y=Center(Intensity,pnumber,area)[0]window=scipy.signal.hanning(length, sym=True)for i in range(0,251):for j in range(0, 151):if abs(i-y)<(length-1)/2 and abs(j-x)<(length-1)/2:windowed[i][j]*=window[int(i-y+(length-1)/2)]*window[int(j-x+(length-1)/2)]else:windowed[i][j]=0windowedabs=np.abs(windowed)RenomalizedAbs = windowedabs*255/np.max(windowedabs)cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\HanningWindowedFFT.jpg'%(pnumber,area),RenomalizedAbs)return windowed
DCFilter(Intensity,pnumber,area) function is realized by:
def DCFilter(Intensity,pnumber,area):Filtered=FFT(pnumber,area)for i in range(0,251):for j in range(0,151):if (i-125)**2*(j-75)**2<Intensity**2:Filtered[i][j]=0return Filtered
with the FFT(pnumber,area):
def FFT(pnumber,area):global f # f is used in later functionsf=np.fft.fft2(Raw(pnumber,area)) # a 251*151 matrix recording the intenxity informationTransformed=np.fft.fftshift(f)abs=np.abs(Transformed)renormalizedabs=abs*255/np.max(abs)cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\FFTResult.jpg'%(pnumber,area),renormalizedabs)return Transformed
where Raw(pnumber,area)
def Raw(pnumber,area):result=np.zeros((251,151))raw = np.loadtxt('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\p%d-%d.txt'%(pnumber,area,pnumber,area))b,a=raw.shapex=int(a/2) # centery=int(b/2) # centerfor i in range (-125,125):for j in range (-75,75):if 0<i+y<b and 0<j+x<a:result[i+125][j+75]=raw[i+y][j+x]return result
Center(Intensity,pnumber,area)function looks long but the structure is qiute simple: since area indecates the posotion of the rectangles (1 for upper-left, etc.), we only remain that part and find the peak there.
def Center(Intensity,pnumber,area):a=np.zeros(shape=[251,151]) # an empty backgroundFilterAbs=DCFilterAbs(Intensity,pnumber,area) # put one of the for 1/4 area on the backgroundif area == 1:for i in range(0,125):for j in range(0,75):a[i][j]=FilterAbs[i][j]elif area == 2:for i in range(0,125):for j in range(75,151):a[i][j]=FilterAbs[i][j]elif area == 3:for i in range(126,251):for j in range(0,75):a[i][j]=FilterAbs[i][j]elif area == 4:for i in range(126,251):for j in range(76,151):a[i][j]=FilterAbs[i][j]m, n = a.shape # n=number of length in x, etc.index = int(a.argmax()) # the index of the peakResult=np.zeros((2))y = int(index / n) # find out in which rowx = index % n # find out in which colummResult[0]=yResult[1]=xreturn Result
Line 2 creates an empty background and line 3-19 put one of the for 1/4 on the background, line 20-24 searches for the peak. Line 3 called a function DCFilterAbs(Intensity,pnumber,area) which gives the scale of the fft result after DC Filter.
DCFilterAbs(Intensity,pnumber,area) is realized by
def DCFilterAbs(Intensity,pnumber,area):DCFiltered=DCFilter(Intensity,pnumber,area)abs=np.abs(DCFiltered)renomalizedabs=abs*255/np.max(abs)cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\filtered.jpg'%(pnumber,area),renomalizedabs)return abs
The retrieved phase is somehow different from these from the previous versions for area 1 to 4:
And it is quite notable that the phase for area 2 is not correctly linked although it passed the overlay phase check.
The second part is the optimazation part, with the optimazation function:scipy.optimize.fmin_l_bfgs_b(TotalCost,x,fprime=derivitive)
def TotalCost(x):# x is the vector containing phi and phicglobal start # starting pnumber, 255 hereglobal end # ending pnumber, 256 hereglobal beta # weight of regulizerphi=np.zeros((251,151)) # buffer to save reconstruced phase (main variable)phic=np.zeros((300,5)) # buffer to save reconstruced phic_s (main variable)for i in range(0,251):for j in range(0,151):phi[i][j]=x[151*i+j] # arrange the phis with xfor i in range(start,end):for j in range(1,5):phic[i][j]=x[151*251+4*(i-start)+j-1] # arrange the phics with xsum=0for pnumber in range(start,end):for area in range(1,5):sum+=CostFunction(pnumber,area,phi,phic)sum+=beta*(Regx(phi)+Regy(phi))return sum
with CostFunction(pnumber,area,phi,phic):
def CostFunction(pnumber,area,phi,phic):global PhaseShearVectorX=ShearVectorx(pnumber,area)ShearVectorY=ShearVectory(pnumber,area)sum=0for i in range(0,251):for j in range (0,151):if i+ShearVectorY<251 and j+ShearVectorX<151:sum+=(Phase[pnumber][area][i][j]-Delta(i,j,ShearVectorY,ShearVectorX,phi)-phic[pnumber][area])**2return sum
where the Delta(y,x,Deltay,Deltax,func) function:
def Delta(y,x,Deltay,Deltax,func):result = func[int(y+Deltay)][int(x+Deltax)]-func[int(y)][int(x)]return result
and:
def Regy(phi):sum=0phiplusone=phi[1:251,:]phiplusone=np.insert(phiplusone,250,np.zeros(151),axis=0)phimunusone=phi[0:250,:]phimunusone=np.insert(phimunusone,0,np.zeros(151),axis=0)result=phimunusone-2*phi+phiplusonesum=np.sum(result)return sum
and Regx(phi) is similar
derivitive(x):
def derivitive(x):global startglobal endglobal betaglobal Phasephi=np.zeros((251,151))phic=np.zeros((300,5))for i in range(0,251):for j in range(0,151):phi[i][j]=x[151*i+j] # assign to phi from xfor i in range(start,end):for j in range(1,5):phic[i][j]=x[151*251+4*(i-start)+j-1] # assign to phi from xresult=np.zeros((251*151+4*(end-start))) # the return of the functionmatrix=np.zeros((251,151)) # to contain the derivities of phi_{i,j}argument=np.zeros((251,151)) # argument=Phase-Delta-phi_c, derivitive = -2Delta(argument)anothermatrix=np.zeros((300,5)) # to contain the derivities of phic_sfor pnumber in range(start , end):for area in range(1,5):ShearVectorX=ShearVectorx(pnumber,area)ShearVectorY=ShearVectory(pnumber,area)for i in range(0,251):for j in range(0,151):if i-ShearVectorY>0 and i+ShearVectorY<251 and j - ShearVectorX>0 and j+ShearVectorX<151:argument[i][j]=Phase[pnumber][area][i][j]-Delta(i,j,ShearVectorX,ShearVectorY,phi)-phic[pnumber][area]for i in range(0,251):for j in range(0,151):if i-ShearVectorY>0 and i+ShearVectorY<251 and j - ShearVectorX>0 and j+ShearVectorX<151:matrix[i][j]+=-2*Delta(i,j,-ShearVectorX,-ShearVectorY,argument)anothermatrix[pnumber][area]+=-2*(argument[i][j])phiplusone=phi[:,1:151]phiplusone=np.insert(phiplusone,150,np.zeros(251),axis=1)phiplustwo=phiplusone[:,1:151]phiplustwo=np.insert(phiplustwo,150,np.zeros(251),axis=1)phimunusone=phi[:,0:150]phimunusone=np.insert(phimunusone,0,np.zeros(251),axis=1)phimunustwo=phimunusone[:,0:150]phimunustwo=np.insert(phimunustwo,0,np.zeros(251),axis=1)matrix+=2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)#This is to calculate the direvities of the regulizerphiplusone=phi[1:251,:]phiplusone=np.insert(phiplusone,250,np.zeros(151),axis=0)phiplustwo=phiplusone[1:251,:]phiplustwo=np.insert(phiplustwo,250,np.zeros(151),axis=0)phimunusone=phi[0:250,:]phimunusone=np.insert(phimunusone,0,np.zeros(151),axis=0)phimunustwo=phimunusone[0:250,:]phimunustwo=np.insert(phimunustwo,0,np.zeros(151),axis=0)#This is to calculate the direvities of the regulizermatrix+=2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)for i in range(0,251):for j in range(0,151):if j<2 or j >148:matrix[i][j]=0if i<2 or i>248:matrix[i][j]=0for i in range(0,251):for j in range(0,151):result[151*i+j]=matrix[i][j]for i in range(start,end):for j in range(1,5):result[151*251+4*(i-start)+j-1]=anothermatrix[i][j]return result
III. SUMMARY
This part of the work is to reorganize wo that it can run faster, but it's hard to judge whether the phase information is accurate. Also the cost function and the derivities are acquired.
IV. FUTURE WORK
In week 8 a first result of the whole programme can be acuired.