[关闭]
@Gaiussheh 2018-08-24T06:18:32.000000Z 字数 23028 阅读 568

Ronchi Reports

Ronchi


Report 0 (Week 0 to Week 2)

I. INTRODUCTION

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.

II. PROGRESS

My understanding on this topic:

III. SUMMARY

The main steps of this project should be

IV. FUTURE WORK

By next week these works will be carried out

Report 1 (Week 3 to Week 4)

I. Introduction

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)

II. PROGRESS

  1. f=np.fft.fft2(Raw) #Raw is the image with mask
  2. Transformed=np.fft.fftshift(f)
  3. Abs=abs(Transformed)

this is what we get from fft:p255-3.jpg-297.8kB
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.

  1. for i in range(0,2047):
  2. for j in range(0,2047):
  3. if (i-1023)**2*(j-1023)**2<4000000\\emperical parameter,may not be universal:
  4. Abs[i][j]=0
  1. a=np.empty(shape=[2048, 2048])
  2. for i in range(0,1023):
  3. for j in range(0,2047):
  4. a[i][j]=Abs[i][j]
  5. m, n = a.shape
  6. index = int(a.argmax())
  7. x = int(index / n)
  8. y = index % n
  1. for i in range(0,2047):
  2. for j in range(0,2047):
  3. if (i-x)**2+(j-y)**2>10000:
  4. Abs[i][j]=0
  5. Transformed[i][j]=0
  1. b=f #set up a complex matrix with f
  2. for i in range(0,2047):
  3. for j in range(0,2047):
  4. b[i][j]=0 #set b as 0
  5. for i in range(-100,100):
  6. for j in range(-100,100):
  7. b[1023+i][1023+j]=Transformed[x+i][y+j]
  1. ff=np.fft.ifftshift(b)
  2. Result=np.fft.ifft2(ff)
  3. Phase=np.zeros((2048, 2048))
  4. for i in range(0,2047):
  5. for j in range (0,2047):
  6. Phase[i][j]=cmath.phase(cmath.log(Result[i][j]))*Mask[i][j]
  1. for i in range(0,2047):
  2. for j in range(0,2046):
  3. if Phase[i][j]-Phase[i][j+1]>0.9*2*math.pi:
  4. print(i,j)
  5. for k in range(j+1,2047):
  6. Phase[i][k]+=2*math.pi
  7. elif Phase[i][j]-Phase[i][j+1]<-0.9*2*math.pi:
  8. print(i,j)
  9. for k in range(j+1,2047):
  10. Phase[i][k]-=2*math.pi

III. SUMMARY

IV. FUTURE WORK

The future work is listed in the order of priority:

Report 2 (Week 5)

I. INTRODUCTION

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.

II. PROGRESS

  1. window=scipy.signal.hanning(51, sym=True)
  2. for i in range(0,2048):
  3. for j in range(0,2048):
  4. if abs(i-x)>25 or abs(j-y)>25:
  5. Abs[i][j]=0
  6. Transformed[i][j]=0
  7. else:
  8. Abs[i][j]*=window[i-(x-25)]*window[j-(y-25)]
  9. Transformed[i][j]*=window[i-(x-25)]*window[j-(y-25)]

III. SUMMARY

IV. FUTURE WORK

The future works are the same with those in Report1. The cost functon is of the most importance.

Report 3 (Week 6 and 7)

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.

  1. def Link(start,end):
  2. for i in range(start,end): # i is p number
  3. for j in range(1,5): # j is the area
  4. link(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 )

  1. def link(length,Intensity,pnumber,area):
  2. Phase=getphase(length,Intensity,pnumber,area)
  3. for i in range(0,251):
  4. for j in range(0,150):
  5. if Phase[i][j]-Phase[i][j+1]>0.95*2*math.pi:
  6. for k in range(j+1,151):
  7. Phase[i][k]+=2*math.pi
  8. elif Phase[i][j]-Phase[i][j+1]<-0.95*2*math.pi:
  9. for k in range(j+1,151):
  10. Phase[i][k]-=2*math.pi
  11. for j in range(0,151):
  12. for i in range(0,250):
  13. if Phase[i][j]-Phase[i+1][j]>0.95*2*math.pi:
  14. for k in range(i+1,251):
  15. Phase[k][j]+=2*math.pi
  16. if Phase[i][j]-Phase[i+1][j]<-0.95*2*math.pi:
  17. for k in range(i+1,251):
  18. Phase[k][j]-=2*math.pi
  19. plt.imshow(Phase)
  20. plt.colorbar()
  21. plt.axis()
  22. plt.show()
  23. np.savetxt('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\LinkedRetrivedPhase%d-%d.txt'%(pnumber,area,pnumber,area),Phase)
  24. 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).

  1. def getphase(length,Intensity,pnumber,area):
  2. ff=np.fft.ifftshift(Move(length,Intensity,pnumber,area))
  3. Result=np.fft.ifft2(ff)
  4. Phase=np.zeros((251, 151))
  5. Phase=math.phase(Result)
  6. plt.imshow(Phase)
  7. plt.colorbar()
  8. plt.axis()
  9. plt.show()
  10. 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

  1. def Move(length,Intensity,pnumber,area):
  2. HanningWindowed=HanningWindow(length,Intensity,pnumber,area)
  3. global f # from fft, f is used to create a complex matrix.
  4. b=f
  5. x=Center(Intensity,pnumber,area)[1] # peak coordinate
  6. y=Center(Intensity,pnumber,area)[0] # peak coordinate
  7. for i in range(-125,125):
  8. for j in range(-75,75):
  9. if -(length-1)/2<i<(length-1)/2 and -(length-1)/2<j<(length-1)/2:
  10. b[125+i][75+j]=HanningWindowed[int(y+i)][int(x+j)]
  11. else:
  12. b[125+i][75+j]=0
  13. Renormalizedc=c*255/np.max(c)
  14. cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\CenteredFFT.jpg'%(pnumber,area),Renormalizedc)
  15. return b
  1. def HanningWindow(length,Intensity,pnumber,area):
  2. windowed=DCFilter(Intensity,pnumber,area)
  3. x=Center(Intensity,pnumber,area)[1]
  4. y=Center(Intensity,pnumber,area)[0]
  5. window=scipy.signal.hanning(length, sym=True)
  6. for i in range(0,251):
  7. for j in range(0, 151):
  8. if abs(i-y)<(length-1)/2 and abs(j-x)<(length-1)/2:
  9. windowed[i][j]*=window[int(i-y+(length-1)/2)]*window[int(j-x+(length-1)/2)]
  10. else:
  11. windowed[i][j]=0
  12. windowedabs=np.abs(windowed)
  13. RenomalizedAbs = windowedabs*255/np.max(windowedabs)
  14. cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\HanningWindowedFFT.jpg'%(pnumber,area),RenomalizedAbs)
  15. return windowed
  1. def DCFilter(Intensity,pnumber,area):
  2. Filtered=FFT(pnumber,area)
  3. for i in range(0,251):
  4. for j in range(0,151):
  5. if (i-125)**2*(j-75)**2<Intensity**2:
  6. Filtered[i][j]=0
  7. return Filtered

with the FFT(pnumber,area):

  1. def FFT(pnumber,area):
  2. global f # f is used in later functions
  3. f=np.fft.fft2(Raw(pnumber,area)) # a 251*151 matrix recording the intenxity information
  4. Transformed=np.fft.fftshift(f)
  5. abs=np.abs(Transformed)
  6. renormalizedabs=abs*255/np.max(abs)
  7. cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\FFTResult.jpg'%(pnumber,area),renormalizedabs)
  8. return Transformed

where Raw(pnumber,area)

  1. def Raw(pnumber,area):
  2. result=np.zeros((251,151))
  3. raw = np.loadtxt('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\p%d-%d.txt'%(pnumber,area,pnumber,area))
  4. b,a=raw.shape
  5. x=int(a/2) # center
  6. y=int(b/2) # center
  7. for i in range (-125,125):
  8. for j in range (-75,75):
  9. if 0<i+y<b and 0<j+x<a:
  10. result[i+125][j+75]=raw[i+y][j+x]
  11. return result
  1. def Center(Intensity,pnumber,area):
  2. a=np.zeros(shape=[251,151]) # an empty background
  3. FilterAbs=DCFilterAbs(Intensity,pnumber,area) # put one of the for 1/4 area on the background
  4. if area == 1:
  5. for i in range(0,125):
  6. for j in range(0,75):
  7. a[i][j]=FilterAbs[i][j]
  8. elif area == 2:
  9. for i in range(0,125):
  10. for j in range(75,151):
  11. a[i][j]=FilterAbs[i][j]
  12. elif area == 3:
  13. for i in range(126,251):
  14. for j in range(0,75):
  15. a[i][j]=FilterAbs[i][j]
  16. elif area == 4:
  17. for i in range(126,251):
  18. for j in range(76,151):
  19. a[i][j]=FilterAbs[i][j]
  20. m, n = a.shape # n=number of length in x, etc.
  21. index = int(a.argmax()) # the index of the peak
  22. Result=np.zeros((2))
  23. y = int(index / n) # find out in which row
  24. x = index % n # find out in which columm
  25. Result[0]=y
  26. Result[1]=x
  27. return 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.

  1. def DCFilterAbs(Intensity,pnumber,area):
  2. DCFiltered=DCFilter(Intensity,pnumber,area)
  3. abs=np.abs(DCFiltered)
  4. renomalizedabs=abs*255/np.max(abs)
  5. cv2.imwrite('C:\\Users\\18217\\OneDrive\\files\\Research\\Ronchi\\data\\x287_ronchi\\%d\\%d\\filtered.jpg'%(pnumber,area),renomalizedabs)
  6. return abs

The retrieved phase is somehow different from these from the previous versions for area 1 to 4:
Figure_2.png-43.9kB
Figure_2.png-25kB
Figure_2.png-48.6kB
Figure_2.png-43.8kB
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)

  1. def TotalCost(x):
  2. # x is the vector containing phi and phic
  3. global start # starting pnumber, 255 here
  4. global end # ending pnumber, 256 here
  5. global beta # weight of regulizer
  6. phi=np.zeros((251,151)) # buffer to save reconstruced phase (main variable)
  7. phic=np.zeros((300,5)) # buffer to save reconstruced phic_s (main variable)
  8. for i in range(0,251):
  9. for j in range(0,151):
  10. phi[i][j]=x[151*i+j] # arrange the phis with x
  11. for i in range(start,end):
  12. for j in range(1,5):
  13. phic[i][j]=x[151*251+4*(i-start)+j-1] # arrange the phics with x
  14. sum=0
  15. for pnumber in range(start,end):
  16. for area in range(1,5):
  17. sum+=CostFunction(pnumber,area,phi,phic)
  18. sum+=beta*(Regx(phi)+Regy(phi))
  19. return sum

with CostFunction(pnumber,area,phi,phic):

  1. def CostFunction(pnumber,area,phi,phic):
  2. global Phase
  3. ShearVectorX=ShearVectorx(pnumber,area)
  4. ShearVectorY=ShearVectory(pnumber,area)
  5. sum=0
  6. for i in range(0,251):
  7. for j in range (0,151):
  8. if i+ShearVectorY<251 and j+ShearVectorX<151:
  9. sum+=(Phase[pnumber][area][i][j]-Delta(i,j,ShearVectorY,ShearVectorX,phi)-phic[pnumber][area])**2
  10. return sum

where the Delta(y,x,Deltay,Deltax,func) function:

  1. def Delta(y,x,Deltay,Deltax,func):
  2. result = func[int(y+Deltay)][int(x+Deltax)]-func[int(y)][int(x)]
  3. return result

and:

  1. def Regy(phi):
  2. sum=0
  3. phiplusone=phi[1:251,:]
  4. phiplusone=np.insert(phiplusone,250,np.zeros(151),axis=0)
  5. phimunusone=phi[0:250,:]
  6. phimunusone=np.insert(phimunusone,0,np.zeros(151),axis=0)
  7. result=phimunusone-2*phi+phiplusone
  8. sum=np.sum(result)
  9. return sum

and Regx(phi) is similar

  1. def derivitive(x):
  2. global start
  3. global end
  4. global beta
  5. global Phase
  6. phi=np.zeros((251,151))
  7. phic=np.zeros((300,5))
  8. for i in range(0,251):
  9. for j in range(0,151):
  10. phi[i][j]=x[151*i+j] # assign to phi from x
  11. for i in range(start,end):
  12. for j in range(1,5):
  13. phic[i][j]=x[151*251+4*(i-start)+j-1] # assign to phi from x
  14. result=np.zeros((251*151+4*(end-start))) # the return of the function
  15. matrix=np.zeros((251,151)) # to contain the derivities of phi_{i,j}
  16. argument=np.zeros((251,151)) # argument=Phase-Delta-phi_c, derivitive = -2Delta(argument)
  17. anothermatrix=np.zeros((300,5)) # to contain the derivities of phic_s
  18. for pnumber in range(start , end):
  19. for area in range(1,5):
  20. ShearVectorX=ShearVectorx(pnumber,area)
  21. ShearVectorY=ShearVectory(pnumber,area)
  22. for i in range(0,251):
  23. for j in range(0,151):
  24. if i-ShearVectorY>0 and i+ShearVectorY<251 and j - ShearVectorX>0 and j+ShearVectorX<151:
  25. argument[i][j]=Phase[pnumber][area][i][j]-Delta(i,j,ShearVectorX,ShearVectorY,phi)-phic[pnumber][area]
  26. for i in range(0,251):
  27. for j in range(0,151):
  28. if i-ShearVectorY>0 and i+ShearVectorY<251 and j - ShearVectorX>0 and j+ShearVectorX<151:
  29. matrix[i][j]+=-2*Delta(i,j,-ShearVectorX,-ShearVectorY,argument)
  30. anothermatrix[pnumber][area]+=-2*(argument[i][j])
  31. phiplusone=phi[:,1:151]
  32. phiplusone=np.insert(phiplusone,150,np.zeros(251),axis=1)
  33. phiplustwo=phiplusone[:,1:151]
  34. phiplustwo=np.insert(phiplustwo,150,np.zeros(251),axis=1)
  35. phimunusone=phi[:,0:150]
  36. phimunusone=np.insert(phimunusone,0,np.zeros(251),axis=1)
  37. phimunustwo=phimunusone[:,0:150]
  38. phimunustwo=np.insert(phimunustwo,0,np.zeros(251),axis=1)
  39. matrix+=2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)
  40. #This is to calculate the direvities of the regulizer
  41. phiplusone=phi[1:251,:]
  42. phiplusone=np.insert(phiplusone,250,np.zeros(151),axis=0)
  43. phiplustwo=phiplusone[1:251,:]
  44. phiplustwo=np.insert(phiplustwo,250,np.zeros(151),axis=0)
  45. phimunusone=phi[0:250,:]
  46. phimunusone=np.insert(phimunusone,0,np.zeros(151),axis=0)
  47. phimunustwo=phimunusone[0:250,:]
  48. phimunustwo=np.insert(phimunustwo,0,np.zeros(151),axis=0)
  49. #This is to calculate the direvities of the regulizer
  50. matrix+=2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)
  51. for i in range(0,251):
  52. for j in range(0,151):
  53. if j<2 or j >148:
  54. matrix[i][j]=0
  55. if i<2 or i>248:
  56. matrix[i][j]=0
  57. for i in range(0,251):
  58. for j in range(0,151):
  59. result[151*i+j]=matrix[i][j]
  60. for i in range(start,end):
  61. for j in range(1,5):
  62. result[151*251+4*(i-start)+j-1]=anothermatrix[i][j]
  63. 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.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注