Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2015
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. def _slow_znl(blank_image, Y,X,P,n,l):
  2.     def _polar(r,theta):
  3.         x = r * cos(theta)
  4.         y = r * sin(theta)
  5.         return 1.*x+1.j*y
  6.  
  7.     v = 0.+0.j
  8.     def _factorial(n):
  9.         if n == 0: return 1.
  10.         return n * _factorial(n - 1)
  11.     y,x = Y[0],X[0]
  12.     for x,y,p in zip(X,Y,P):
  13.         Vnl = 0.
  14.         for m in range( int( (n-l)//2 ) + 1 ):
  15.             Vnl += (-1.)**m * _factorial(n-m) /  \
  16.                 ( _factorial(m) * _factorial((n - 2*m + l) // 2) * _factorial((n - 2*m - l) // 2) ) * \
  17.                 ( sqrt(x*x + y*y)**(n - 2*m) * _polar(1.0, l*atan2(y,x)) )
  18.         v += p * conjugate(Vnl)
  19.  
  20.         #This is probably wrong, I am not sure if v represents the coefficients, does it?
  21.         blank_image[x][y] = v
  22.  
  23.     v *= (n+1)/pi
  24.  
  25.     #I get a bunch of zeros at this point, probably v being casted to uint8, so I presume something must happen before this point
  26.     print blank_image
  27.  
  28.     #This transpose method is not the one you refer, I am quite sure.
  29.     cv2.imshow("transposed image", cv2.transpose(blank_image))
  30.  
  31.     return v
  32.  
  33. def _slow_zernike(img, radius, D, cof=None):
  34.     zvalues = []
  35.  
  36.     Y,X = np.where(img > 0)
  37.     P = img[Y,X].ravel()
  38.  
  39.     #blank image
  40.     height, width = img.shape
  41.     blank_image = np.zeros((height, width, 3), np.uint8)
  42.  
  43.     if cof is None:
  44.         cofy,cofx = center_of_mass(img)
  45.     else:
  46.         cofy,cofx = cof
  47.     Yn = ( (Y -cofy)/radius).ravel()
  48.     Xn = ( (X -cofx)/radius).ravel()
  49.     k = (np.sqrt(Xn**2 + Yn**2) <= 1.)
  50.     frac_center = np.array(P[k], np.double)
  51.     frac_center /= frac_center.sum()
  52.     Yn = Yn[k]
  53.     Xn = Xn[k]
  54.     frac_center = frac_center.ravel()
  55.  
  56.     for n in range(D+1):
  57.         for l in range(n+1):
  58.             if (n-l)%2 == 0:
  59.                 z = _slow_znl(blank_image, Yn, Xn, frac_center, float(n), float(l))
  60.                 zvalues.append(abs(z))
  61.     return np.array(zvalues)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement