Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from scipy.interpolate import UnivariateSpline
- import matplotlib.pyplot as plt
- import pylab as pl
- import random
- import math
- import time
- cimport cython
- import numpy as np
- @cython.boundscheck(False)
- @cython.wraparound(False)
- def gaussian_2D_distribution(x,y,int mean_x,int mean_y,double sigma):
- #cdef double[:,:]f
- f = np.exp((-0.5*(np.square(x-mean_x) + np.square(y-mean_y))/sigma))/(sigma*np.sqrt(2*math.pi));
- return f
- def Centroid2(x,y,f):
- temp_x = np.multiply(x,f)
- temp_y = np.multiply(y,f)
- X_c = (temp_x.sum()/f.sum());
- Y_c = -((temp_y.sum()/f.sum())+1);
- return(X_c,Y_c);
- @cython.boundscheck(False)
- @cython.wraparound(False)
- def benchmark_func(int no_of_frames):
- cdef:
- int i
- double start
- double end
- #int shift[10000][2]
- double Centroid[10000][2]
- #---------------------------------------------Creating Matrices--------------------------------------------------------#
- start = time.time()
- a = np.arange(-50,50,1);
- x = np.matlib.repmat(a,100 ,1);
- y = x.transpose();
- f = np.zeros([no_of_frames,100,100])
- print "Done stage 1 ..."
- #---------------------------------------Creating Gaussian Images------------------------------------------------------#
- shift = np.zeros([no_of_frames,2])
- shift[:,0] = 15
- shift[:,1] = 25
- for i in range(int (no_of_frames)):
- #shift[i][0] = random.randint(-50,50)
- #shift[i][1] = random.randint(-50,50)
- f[i,:,:] = gaussian_2D_distribution(x,y,shift[i][0],shift[i][1],0.1)
- f[i,:,:]= np.flip(f[i,:,:],0)
- print "Done stage 2 ..."
- #---------------------------------------To find the centroid----------------------------------------------------------#
- #start = time.time()
- for i in range(int (no_of_frames)):
- Centroid[i][:] = Centroid2(x,y,f[i])
- end = time.time()
- #print Centroid[2]
- print(end - start),
- print "seconds"
- print "Done stage 3 ..."
- return Centroid
Add Comment
Please, Sign In to add comment