Exemplo n.º 1
0
def tst_2d_zernike_mom(n, l, filename, h5file, flag):
    rebuilt = open(filename, 'w')
    # image = generate_image()
    # image=ImageToDat("/Users/wyf/Desktop/test11.png")
    image = preprocess_image(h5file, flag)
    # image = ImageToDat('cha.png')

    NP = int(smath.sqrt(image.size()))
    N = int(NP / 2)
    print "=====", NP, N
    grid_2d = math.two_d_grid(N, nmax)
    grid_2d.clean_space(image)
    grid_2d.construct_space_sum()
    zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax)
    moments = zernike_2d_mom.moments()
    coefs = flex.real(moments)
    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()
    nl_array.load_coefs(nls, coefs)
    lfg = 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

    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 = math.zernike_2d_radial(n,l,lfg)
        rap = 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 = smath.sqrt(x * x + y * y) / N
                if rr > 1.0:
                    value = 0.0
                else:
                    tt = smath.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
            print >> rebuilt, x, y, value
            i = i + 1
    rebuilt.close()
Exemplo n.º 2
0
 def __init__(self, base_image, nmax):
     self.bi = base_image.deep_copy()
     self.np = self.bi.focus()[0]
     self.nmax = nmax
     self.n = int(self.np / 2)
     print self.bi.focus(), self.np, self.n
     print "Building mom->img grid"
     t1 = time.time()
     self.z2dg = math.zernike_grid_2d(self.n, self.nmax)
     t2 = time.time()
     print "time used: %d (seconds)" % (t2 - t1)
     print "Build img->mom grid"
     self.grid_2d = math.two_d_grid(self.n, self.nmax)
Exemplo n.º 3
0
def tst_2d_poly(n,l):
  nmax=max(n,20)
  np=50
  x,y=0.1,0.9
  r,t=smath.sqrt(x*x+y*y),smath.atan2(y,x)
  lfg =  math.log_factorial_generator(nmax)
  #rzfa = math.zernike_2d_radial(n,l,lfg)
  #rap = math.zernike_2d_polynome(n,l,rzfa)
  rap = math.zernike_2d_polynome(n,l)#,rzfa)
  rt_value=rap.f(r,t)
  grid = math.two_d_grid(np, nmax)
  zm2d = 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)
Exemplo n.º 4
0
def tst_2d_zm(n,l):
  nmax=max(n,20)
  np=100
  points=flex.double(range(-np,np+1))/np
  grid = math.two_d_grid(np, nmax)
  zm2d = math.two_d_zernike_moments(grid, nmax)
  image = flex.vec3_double()

  output=file('testmap.dat','w')

  for x in points:
    for y in points:
      r=smath.sqrt(x*x+y*y)
      if(r>1.0):
        value=0.0
      else:
        value=zm2d.zernike_poly(n,l,x,y).real
      image.append([x*np+np,y*np+np, value])

  grid.clean_space( image )
  grid.construct_space_sum()
  zernike_2d_mom = math.two_d_zernike_moments( grid, nmax )

  moments = zernike_2d_mom.moments()

  coefs = flex.real( moments )
  nl_array = math.nl_array( nmax )
  nls = nl_array.nl()

  for nl, c in zip( nls, moments):
    if(abs(c)<1e-3):
      c=0
    print nl, c

  NP=np*2+1
  reconst=zernike_2d_mom.zernike_map(nmax, np)
  i = 0
  for x in range(0,NP):
    for y in range(0,NP):
      value=reconst[i].real
      if(value>0):
        print>>output, x,y,image[i][2],value
      i=i+1


  output.close()
Exemplo n.º 5
0
def tst_zernike_expansion(pdbfile, nmax=10):
    projection_obj = projection.projection(pdbfile)
    image = projection_obj.project()
    ori_image = image.deep_copy()
    output = open('ori.dat', 'w')

    # get moments
    NP = int(smath.sqrt(image.size()))
    N = NP / 2
    grid_2d = math.two_d_grid(N, nmax)
    grid_2d.clean_space(image)
    grid_2d.construct_space_sum()
    zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax)
    # use moments to reconstruct image
    reconst = image_tools.generate_image2(nmax, zernike_2d_mom.moments(),
                                          ori_image, N)

    for pp, qq in zip(image, reconst):
        print pp[0], pp[1], pp[2], qq[2]
Exemplo n.º 6
0
def zernike_expansion(image, nmax=30):
  ori_image=image.deep_copy()
  output=open('ori_c2.dat', 'w')

  # get moments
  NP=int(smath.sqrt( image.size() ))
  nimg = flex.vec3_double()
  ori_image = nimg.deep_copy()
  for ii in range(NP):
    for jj in range(NP):
      nimg.append( (ii,jj,image[(ii,jj)]) )
  ori_image = nimg.deep_copy()
  N=NP/2
  grid_2d = math.two_d_grid(N, nmax)
  grid_2d.clean_space( nimg )
  grid_2d.construct_space_sum()
  zernike_2d_mom = math.two_d_zernike_moments( grid_2d, nmax )
  # use moments to reconstruct image
  reconst = image_tools.generate_image2(nmax,zernike_2d_mom.moments(),ori_image,N)
  for pp,qq, in zip( ori_image, reconst):
    print pp[0], pp[1], pp[2], qq[2]
Exemplo n.º 7
0
def tst_2d_zernike_mom(n, l, file):
  # image = preprocess_image(h5file)
  image = ImageToDat(file)
  feature_maxtix = []
  NP=int(smath.sqrt( image.size() ))  
  N=int(NP/2)
  # print"=====",NP, N
  grid_2d = math.two_d_grid(N, nmax)
  grid_2d.clean_space( image )
  grid_2d.construct_space_sum()
  zernike_2d_mom = math.two_d_zernike_moments( grid_2d, nmax )
  moments = zernike_2d_mom.moments()
  coefs = flex.real( moments )
  nl_array = math.nl_array( nmax )
  nls = nl_array.nl()
  # nl_array.load_coefs( nls, coefs )
  # lfg = math.log_factorial_generator(nmax)
  for nl, c in zip( nls, moments):
    if(abs(c)<1e-3):
      c=0
    feature_maxtix.append(c.real)
    # print nl,c
  return feature_maxtix
Exemplo n.º 8
0
def tst_fft_proj(pdbfile, nmax=20):
    dx = 1.0
    projection_obj = projection.projection(pdbfile, dx=dx, fraction=0.5)
    dq = projection_obj.get_dq()
    image = projection_obj.project()
    output = open('prj1.dat', 'w')
    for pt in image:
        print >> output, pt[0], pt[1], pt[2]
    output.close()

    values = projection_obj.get_value()
    np = projection_obj.get_np()

    flex_grid = flex.grid(np, np)
    fft_input = flex.complex_double(flex_grid)
    for ii in range(fft_input.size()):
        fft_input[ii] = complex(values[ii], 0)
    fft_obj = fftpack.complex_to_complex_2d((np, np))
    result = fft_obj.forward(fft_input)

    sp_image = flex.vec3_double()

    for ii in range(np):
        for jj in range(np):
            kk = ii
            ll = jj
            if kk > np / 2: kk = kk - np
            if ll > np / 2: ll = ll - np
            sp_image.append(
                [kk + np / 2, ll + np / 2,
                 abs(result[(ii, jj)])**2.0])

    Nq = np / 2
    Nphi = Nq
    c2_image, normalized_sp = from_I_to_C2(sp_image,
                                           Nq,
                                           Nphi,
                                           N=np,
                                           further_reduct=True)

    image = normalized_sp
    NP = int(smath.sqrt(image.size()))
    N = NP / 2
    sp_n = N
    grid_2d = math.two_d_grid(N, nmax)
    grid_2d.clean_space(image)
    grid_2d.construct_space_sum()
    zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax)

    sp_moments = zernike_2d_mom.moments()

    #  sp_reconst =  image_tools.generate_image2(nmax, sp_moments, normalized_sp, sp_n)
    #  c2_image, normalized_sp = from_I_to_C2(sp_reconst, Nq, Nphi, N=np)

    sp_out = open('sp_image.dat', 'w')
    for pt in sp_image:
        print >> sp_out, (pt[0] - sp_n) * dq, (pt[1] - sp_n) * dq, pt[2]
    sp_out.close()

    c2_output = open('c2.dat', 'w')
    for pt in c2_image:
        print >> c2_output, pt[0], pt[1], pt[2]
    c2_output.close()

    image = c2_image
    NP = int(smath.sqrt(image.size()))
    N = NP / 2
    c2_n = N
    #  grid_2d = math.two_d_grid(N, nmax)
    #  grid_2d.clean_space( image )
    #  grid_2d.construct_space_sum()
    #  zernike_2d_mom = math.two_d_zernike_moments( grid_2d, nmax )

    #  c2_moments = zernike_2d_mom.moments()

    #  coefs = ( c2_moments )
    nls = math.nl_array(nmax).nl()
    #  for nl, c2m in zip( nls, coefs ):
    #    print nl, c2m

    sp_moments[0] = 0
    nmax4c2 = 2 * nmax
    Inm = math.nl_c_array(nmax4c2)
    coef_table = image_tools.calc_integral_triple_zernike2d(nmax4c2)
    Inm.load_coefs(nls, sp_moments)
    cnm = image_tools.calc_Cnm_from_Inm(Inm, coef_table, nmax4c2)
    cnm_coef = cnm.coefs()

    #  for nl, mm,mmm in zip( nls, c2_moments, cnm_coef ):
    #    print abs(mm), abs(mmm)

    #cnm_coef = c2_moments
    c2_reconst = image_tools.generate_image2(nmax, cnm_coef, c2_image, c2_n)

    c2_r = open('c2r.dat', 'w')
    for pt in c2_reconst:
        print >> c2_r, pt[0], pt[1], pt[2]
    c2_r.close()

    exit()
Exemplo n.º 9
0
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(smath.sqrt( image.size() ))
  N=NP/2
  grid_2d = 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 = 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 = math.nl_array( nmax )
  nls = nl_array.nl()
  nl_array.load_coefs( nls, coefs )
  lfg =  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 = math.zernike_2d_radial(n,l,lfg)
    rap = 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 = smath.sqrt(x*x+y*y)/N
        if rr>1.0:
          value=0.0
        else:
          tt = smath.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>>rebuilt, x,y,image[i][2],value
      i=i+1


  rebuilt.close()
Exemplo n.º 10
0
def tst_image(n, N=100, filename=None):
    nmax = n
    nmax0 = 8
    nl_array0 = math.nl_array(nmax0)
    nls0 = nl_array0.nl()

    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()
    if (filename is not None):
        image = read_data(filename)
    else:
        moments = flex.random_double(nls0.size())
        #    moments=flex.double(nls.size(),0 )
        #    moments[3] = 1.0
        #    moments[7] = 1.0
        image = generate_image(n, moments)
        orig = open('original.dat', 'w')
        for pt in image:
            print >> orig, pt[0], pt[1], pt[2]
        orig.close()

    Nq = 100
    Nphi = 100
    c2_image = from_I_to_C2(nmax, image, Nq, Nphi)

    c2_output = open('c2.dat', 'w')
    for pt in c2_image:
        print >> c2_output, pt[0], pt[1], pt[2]
    c2_output.close()

    coef_table = calc_integral_triple_zernike2d(nmax)
    moments[0] = 0
    new_mom = moments.concatenate(flex.double(nls.size() - nls0.size(), 0))
    nmax4c2 = 2 * nmax
    Inm = math.nl_c_array(nmax4c2)

    Inm.load_coefs(nls, new_mom)
    cnm = calc_Cnm_from_Inm(Inm, coef_table, nmax4c2)
    cnm_coef = cnm.coefs()
    print "#cnm[0]", cnm_coef[0]
    #  cnm_coef[0]=0

    ### calculate 2d zernike moments for C2_image ###
    image = c2_image
    NP = int(smath.sqrt(image.size()))
    N = NP / 2
    grid_2d = math.two_d_grid(N, nmax)
    grid_2d.clean_space(image)
    grid_2d.construct_space_sum()
    zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax)

    c2_moments = zernike_2d_mom.moments()

    #coefs = flex.real( c2_moments )
    #cnm_coef  = flex.real( c2_moments )
    cnm_coef = c2_moments
    c2_reconst = generate_image2(n, cnm_coef, c2_image, Nq)

    c2_r = open('c2r.dat', 'w')
    for pt in c2_reconst:
        print >> c2_r, pt[0], pt[1], pt[2]
    c2_r.close()

    ls_score = 0
    np_tot = c2_image.size()
    for p1, p2 in zip(c2_image, c2_reconst):
        ls_score += (p1[2] - p2[2])**2.0

    print nmax, nls.size(), ls_score, np_tot * smath.log(
        ls_score / np_tot), "SUM"

    for nl, c2m in zip(nls, cnm_coef):
        print nl, c2m

    exit()  #

    ### calculate 2d zernike moments for C2_image ###
    image = c2_image
    NP = int(smath.sqrt(image.size()))
    N = NP / 2
    grid_2d = math.two_d_grid(N, nmax)
    grid_2d.clean_space(image)
    grid_2d.construct_space_sum()
    tt2 = time.time()
    zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax)

    c2_moments = zernike_2d_mom.moments()

    #coefs = flex.real( c2_moments )
    coefs = (c2_moments)
    for nl, c2m, c2mm in zip(nls, cnm_coef, coefs):
        if (nl[0] / 2 * 2 == nl[0]):
            print c2m, c2mm

    coefs = flex.real(moments)
    nl_array.load_coefs(nls, coefs)

    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.0
        rap = math.zernike_2d_polynome(n, l)
        i = 0
        for x in range(0, NP):
            x = x - N
            for y in range(0, NP):
                y = y - N
                rr = smath.sqrt(x * x + y * y) / N
                if rr > 1.0:
                    value = 0.0
                else:
                    tt = smath.atan2(y, x)
                    value = rap.f(rr, tt)
                reconst[i] = reconst[i] + value * c
                i = i + 1

    rebuilt = open('rebuilt.dat', 'w')
    i = 0
    for x in range(0, NP):
        for y in range(0, NP):
            value = reconst[i].real
            print >> rebuilt, x, y, image[i][2], value
            i = i + 1
    rebuilt.close()
def tst_line(nmax=20, width=2, N=201):

    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()
    moments = flex.random_double(nls.size()) * 0
    nl_array.load_coefs(nls, moments)
    nl_array.set_coef(8, 6, 1.0)
    nl_array.set_coef(6, 2, 1.0)
    nl_array.set_coef(4, 4, 1.0)
    moments = nl_array.coefs()
    this_nls = [[8, 6], [6, 2], [4, 4]]
    #  this_nls = [[6,2]]
    this_moments = [1.0, 1.0, 1.0]

    N = N
    half_N = N / 2
    Nq = half_N
    Nphi = Nq

    ## The following two methods should give consistent results ##
    #c2_image = build_grid(Nq, Nphi, this_nls, this_moments )
    c2_image = analytical_c2(Nq, Nphi, this_nls, this_moments)

    c2_output = open('c2_raw.dat', 'w')
    for pt in c2_image:
        print >> c2_output, pt[0], pt[1], pt[2]
    c2_output.close()

    #sp_out = open('sp_image.dat', 'w')
    #for pt in raw_image:
    #  print>>sp_out, pt[0], pt[1], pt[2]
    #sp_out.close()

    ## get cnm from sp_moments ##

    sp_moments = flex.complex_double(moments, moments * 0)
    #sp_moments[0]=0
    nmax4c2 = nmax * 2
    Inm = math.nl_c_array(nmax4c2)
    coef_table = image_tools.calc_integral_triple_zernike2d(nmax4c2)
    Inm.load_coefs(nls, sp_moments)
    cnm = image_tools.calc_Cnm_from_Inm(Inm, coef_table, nmax4c2)
    cnm_coef = cnm.coefs()

    c2_reconst = image_tools.generate_image2(nmax4c2, cnm_coef, c2_image,
                                             half_N)

    c2_output = open('c2_reconst.dat', 'w')
    for pt in c2_reconst:
        print >> c2_output, pt[0], pt[1], pt[2]
    c2_output.close()

    ## get zm for sp_image ##
    image = c2_image
    NP = int(smath.sqrt(image.size()))
    N = NP / 2
    sp_n = N
    grid_2d = math.two_d_grid(N, nmax)
    grid_2d.clean_space(image)
    grid_2d.construct_space_sum()
    zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax)
    sp_moments = zernike_2d_mom.moments()

    print "#C2"
    for nl, m1, m2 in zip(nls, cnm_coef, sp_moments):
        print nl, m1, m2.real, m1 / (m2.real + 1e-23)