Guest User

Untitled

a guest
May 24th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. from scipy.interpolate import UnivariateSpline
  2. import matplotlib.pyplot as plt
  3. import pylab as pl
  4. import random
  5. import math
  6. import time
  7. cimport cython
  8. import numpy as np
  9. @cython.boundscheck(False)
  10. @cython.wraparound(False)
  11. def gaussian_2D_distribution(x,y,int mean_x,int mean_y,double sigma):
  12. #cdef double[:,:]f
  13. f = np.exp((-0.5*(np.square(x-mean_x) + np.square(y-mean_y))/sigma))/(sigma*np.sqrt(2*math.pi));
  14. return f
  15.  
  16. def Centroid2(x,y,f):
  17. temp_x = np.multiply(x,f)
  18. temp_y = np.multiply(y,f)
  19. X_c = (temp_x.sum()/f.sum());
  20. Y_c = -((temp_y.sum()/f.sum())+1);
  21. return(X_c,Y_c);
  22.  
  23. @cython.boundscheck(False)
  24. @cython.wraparound(False)
  25. def benchmark_func(int no_of_frames):
  26. cdef:
  27. int i
  28. double start
  29. double end
  30. #int shift[10000][2]
  31. double Centroid[10000][2]
  32. #---------------------------------------------Creating Matrices--------------------------------------------------------#
  33. start = time.time()
  34. a = np.arange(-50,50,1);
  35. x = np.matlib.repmat(a,100 ,1);
  36. y = x.transpose();
  37. f = np.zeros([no_of_frames,100,100])
  38.  
  39. print "Done stage 1 ..."
  40. #---------------------------------------Creating Gaussian Images------------------------------------------------------#
  41. shift = np.zeros([no_of_frames,2])
  42. shift[:,0] = 15
  43. shift[:,1] = 25
  44.  
  45. for i in range(int (no_of_frames)):
  46. #shift[i][0] = random.randint(-50,50)
  47. #shift[i][1] = random.randint(-50,50)
  48. f[i,:,:] = gaussian_2D_distribution(x,y,shift[i][0],shift[i][1],0.1)
  49. f[i,:,:]= np.flip(f[i,:,:],0)
  50.  
  51. print "Done stage 2 ..."
  52. #---------------------------------------To find the centroid----------------------------------------------------------#
  53.  
  54. #start = time.time()
  55. for i in range(int (no_of_frames)):
  56. Centroid[i][:] = Centroid2(x,y,f[i])
  57. end = time.time()
  58.  
  59. #print Centroid[2]
  60. print(end - start),
  61. print "seconds"
  62. print "Done stage 3 ..."
  63. return Centroid
Add Comment
Please, Sign In to add comment