def build_grid(Nq, Nphi, nls, moments):
    two_pi = smath.pi * 2.0
    dq = 1
    dphi = two_pi / Nphi
    qps = flex.vec2_double()
    xys = flex.vec2_double()
    image = flex.vec3_double()
    for xx in range(0, Nq + 1):
        for yy in range(0, Nq + 1):
            r = smath.sqrt(xx * xx + yy * yy) / Nq
            if (r <= 1):
                phi = smath.atan2(yy, xx)
                if (phi < 0): phi = phi + two_pi
                qps.append([r, phi])
                xys.append([xx, yy])
                c2 = compute_c2(nls, moments, r, phi)
            else:
                c2 = 0
            image.append([Nq + xx, Nq + yy, c2])
            if (xx > 0 and yy > 0):
                image.append([Nq - xx, Nq - yy, c2])
            if (yy > 0):
                image.append([Nq + xx, Nq - yy, c2])
            if (xx > 0):
                image.append([Nq - xx, Nq + yy, c2])
    return image
Exemplo n.º 2
0
def generate_image(n,l, N=100):
  nmax = max(20,n)
  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)

  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 = 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)
        value = value.real
        count = count + 1
      image.append([x+N,y+N,value])
      print>>original, x+N,y+N, value
  original.close()
  return image
Exemplo n.º 3
0
 def build_images(self):
   for ii in range(self.np):
     for jj in range(self.np):
       rr = smath.sqrt( (ii+0.5-self.ox)**2.0 + (jj+0.5-self.oy)**2.0 )
       self.r_image[ (ii,jj) ] = rr
       tt = smath.atan2( jj+0.5-self.oy,ii+0.5-self.ox )
       self.t_image[ (ii,jj) ] = tt
Exemplo n.º 4
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.º 5
0
def generate_image(n, moments, N=100):
    # Generate images from the zernike moments, N is the number of points from 0 to 1
    nmax = n

    image = flex.vec3_double()
    NP = 2 * N + 1
    reconst = flex.complex_double(NP**2, 0)
    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()

    r_theta = flex.vec2_double()

    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:
                tt = 0.0
            else:
                tt = smath.atan2(y, x)
            r_theta.append([rr, tt])

    for nl, c in zip(nls, moments):
        n = nl[0]
        l = nl[1]
        #if(l/2*2 != l): continue
        if (l > 0): c = c * 2.0

        rap = math.zernike_2d_polynome(n, l)
        i = 0
        for pt in r_theta:
            rr = pt[0]
            if rr > 1.0:
                value = 0.0
            else:
                tt = pt[1]
                value = rap.f(rr, tt)
            reconst[i] = reconst[i] + value * c
            i = i + 1

    return reconst

    i = 0
    for x in range(0, NP):
        for y in range(0, NP):
            value = reconst[i].real
            image.append([x, y, value])
            i = i + 1
    return image
Exemplo n.º 6
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.º 7
0
def generate_image2(n, moments, template_image, np):
    nmax = n

    image = flex.vec3_double()
    np_tot = template_image.size()
    reconst = flex.complex_double(np_tot, 0)
    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()

    r_theta = flex.vec2_double()
    for pt in template_image:
        x = pt[0] - np
        y = pt[1] - np
        rr = smath.sqrt(x * x + y * y) / np
        if rr > 1.0:
            tt = 0.0
        else:
            tt = smath.atan2(y, x)
        r_theta.append([rr, tt])

    for nl, c in zip(nls, moments):
        n = nl[0]
        l = nl[1]
        #if(l/2*2 != l): continue
        if (l > 0): c = c * 2.0
        rap = math.zernike_2d_polynome(n, l)
        i = 0
        for pt in r_theta:
            rr = pt[0]
            if rr > 1.0:
                value = 0.0
            else:
                tt = pt[1]
                value = rap.f(rr, tt)
            reconst[i] = reconst[i] + value * c
            i = i + 1

    for i in range(np_tot):
        x = template_image[i][0]
        y = template_image[i][1]
        value = reconst[i].real
        image.append([x, y, value])
    return image
def analytical_c2(Nq, Nphi, nls, moments):
    image = flex.vec3_double()
    for xx in range(0, Nq + 1):
        for yy in range(0, Nq + 1):
            r = smath.sqrt(xx * xx + yy * yy) / Nq
            if (r <= 1):
                phi = smath.atan2(yy, xx)
                if (phi < 0): phi = phi + two_pi
                c2 = evaluate_c2(nls, moments, r, phi)
            else:
                c2 = 0
            image.append([Nq + xx, Nq + yy, c2])
            if (xx > 0 and yy > 0):
                image.append([Nq - xx, Nq - yy, c2])
            if (yy > 0):
                image.append([Nq + xx, Nq - yy, c2])
            if (xx > 0):
                image.append([Nq - xx, Nq + yy, c2])
    return image
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()