Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. def moments(image):
  2. c0,c1 = np.mgrid[:image.shape[0],:image.shape[1]] # A trick in numPy to create a mesh grid
  3. totalImage = np.sum(image) #sum of pixels
  4. m0 = np.sum(c0*image)/totalImage #mu_x
  5. m1 = np.sum(c1*image)/totalImage #mu_y
  6. m00 = np.sum((c0-m0)**2*image)/totalImage #var(x)
  7. m11 = np.sum((c1-m1)**2*image)/totalImage #var(y)
  8. m01 = np.sum((c0-m0)*(c1-m1)*image)/totalImage #covariance(x,y)
  9. mu_vector = np.array([m0,m1]) # Notice that these are mu_x, mu_y respectively
  10. covariance_matrix = np.array([[m00,m01],[m01,m11]]) # Do you see a similarity between the covariance matrix
  11. return mu_vector, covariance_matrix
  12.  
  13. def deskew(image):
  14. c,v = moments(image)
  15. alpha = v[0,1]/v[0,0]
  16. affine = np.array([[1,0],[alpha,1]])
  17. ocenter = np.array(image.shape)/2.0
  18. offset = c-np.dot(affine,ocenter)
  19. return interpolation.affine_transform(image,affine,offset=offset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement