Exemple #1
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
Exemple #2
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()
def evaluate_c2(nls, moments, r, phi):
    c2 = 0
    two_pi = smath.pi * 2.0
    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)
        for nl1, c1 in zip(nls, moments):
            n1 = nl1[0]
            l1 = nl1[1]
            if (l != l1): continue
            if (l1 > 0): c1 = c1 * 2.0
            rap1 = math.zernike_2d_polynome(n1, l1)
            tmp_c2 = rap.f(r,
                           phi / 2)  # using phi/2 because it will add up later
            tmp_c2 = tmp_c2 * rap1.f(r, phi / 2)
            c2 = c2 + tmp_c2.real * c * c1
    return c2 * two_pi
Exemple #4
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
Exemple #5
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)
def compute_c2(nls, moments, r, phi, Nsample=100):
    c2 = 0
    two_pi = smath.pi * 2.0
    sp1 = flex.double(Nsample, 0)
    sp2 = flex.double(Nsample, 0)
    theta = flex.random_double(Nsample) * two_pi
    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)
        for ii in range(Nsample):
            value1 = rap.f(r, theta[ii] + phi).real * c
            value2 = rap.f(r, theta[ii]).real * c
            sp1[ii] = sp1[ii] + value1
            sp2[ii] = sp2[ii] + value2
    return flex.sum(sp1 * sp2) / Nsample
Exemple #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
Exemple #8
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()
Exemple #9
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()