def generate_image(n,l, N=100): nmax = max(20,n) lfg = scitbx.math.log_factorial_generator(nmax) #rzfa = scitbx.math.zernike_2d_radial(n,l,lfg) #rap = scitbx.math.zernike_2d_polynome(n,l,rzfa) rap = scitbx.math.zernike_2d_polynome(n,l)#,rzfa) image = flex.vec3_double() original=open('original.dat','w') count = 0 for x in range(-N, N+1): for y in range(-N, N+1): rr = math.sqrt(x*x+y*y)/N if rr>1.0: value=0.0 else: tt = math.atan2(y,x) value = rap.f(rr,tt) value = value.real count = count + 1 image.append([x+N,y+N,value]) print(x+N,y+N, value, file=original) original.close() return image
def tst_2d_poly(n,l): nmax=max(n,20) np=50 x,y=0.1,0.9 r,t=math.sqrt(x*x+y*y),math.atan2(y,x) lfg = scitbx.math.log_factorial_generator(nmax) #rzfa = scitbx.math.zernike_2d_radial(n,l,lfg) #rap = scitbx.math.zernike_2d_polynome(n,l,rzfa) rap = scitbx.math.zernike_2d_polynome(n,l)#,rzfa) rt_value=rap.f(r,t) grid = scitbx.math.two_d_grid(np, nmax) zm2d = scitbx.math.two_d_zernike_moments(grid, nmax) xy_value=zm2d.zernike_poly(n,l,x,y) print(rt_value, xy_value, abs(rt_value), abs(xy_value))
def tst_2d_zernike_mom(n,l, N=100, filename=None): nmax = max(20,n) rebuilt=open('rebuilt.dat','w') tt1=time.time() if(filename is not None): image=read_data(filename) else: image=generate_image(n,l) NP=int(math.sqrt( image.size() )) N=NP/2 grid_2d = scitbx.math.two_d_grid(N, nmax) grid_2d.clean_space( image ) grid_2d.construct_space_sum() tt2=time.time() print("time used: ", tt2-tt1) zernike_2d_mom = scitbx.math.two_d_zernike_moments( grid_2d, nmax ) moments = zernike_2d_mom.moments() tt2=time.time() print("time used: ", tt2-tt1) coefs = flex.real( moments ) nl_array = scitbx.math.nl_array( nmax ) nls = nl_array.nl() nl_array.load_coefs( nls, coefs ) lfg = scitbx.math.log_factorial_generator(nmax) print(nl_array.get_coef(n,l)*2) for nl, c in zip( nls, moments): if(abs(c)<1e-3): c=0 print(nl, c) print() reconst=flex.complex_double(NP**2, 0) for nl,c in zip( nls, moments): n=nl[0] l=nl[1] if(l>0): c=c*2 #rzfa = scitbx.math.zernike_2d_radial(n,l,lfg) rap = scitbx.math.zernike_2d_polynome(n,l) #,rzfa) i=0 for x in range(0,NP): x=x-N for y in range(0,NP): y=y-N rr = math.sqrt(x*x+y*y)/N if rr>1.0: value=0.0 else: tt = math.atan2(y,x) value = rap.f(rr,tt) reconst[i]=reconst[i]+value*c i=i+1 i = 0 for x in range(0,NP): for y in range(0,NP): value=reconst[i].real if(value>0): print(x,y,image[i][2],value, file=rebuilt) i=i+1 rebuilt.close()