"""
http://stackoverflow.com/questions/21242011/most-efficient-way-to-calculate-radial-profile
"""
import numpy as np
import cv2
import matplotlib.pyplot as plt


def radial_profile(data, center):
    y, x = np.indices((data.shape))
    r = np.sqrt((x - center[0])**2 + (y - center[1])**2)
    r = r.astype(np.int)

    tbin = np.bincount(r.ravel(), data.ravel())
    nr = np.bincount(r.ravel())
    radialprofile = tbin / nr
    return radialprofile
    

img_num = 102557

img = cv2.imread('images_test_rev1/'+str(img_num)+'.jpg', 0)
img = img[100:300, 100:300]
img = cv2.resize(img, (100, 100))

#plt.imshow(img)
rad = radial_profile(img, (img.shape[0]/2.0, img.shape[1]/2.0))
plt.subplot(2,2,2), plt.plot(rad)
plt.xlabel('Radius')
plt.ylabel('Average')


img = cv2.imread('images_test_rev1/'+str(img_num)+'.jpg')
img = img[100:300, 100:300,:]
img = cv2.resize(img, (100, 100))

plt.subplot(2,2,1), plt.imshow(img)
plt.axis('off')
plt.title('Image')

img_num = 103163

img = cv2.imread('images_test_rev1/'+str(img_num)+'.jpg', 0)
img = img[100:300, 100:300]
img = cv2.resize(img, (100, 100))

#plt.imshow(img)
rad = radial_profile(img, (img.shape[0]/2.0, img.shape[1]/2.0))
plt.subplot(2,2,4), plt.plot(rad)
plt.xlabel('Radius')
plt.ylabel('Average')


img = cv2.imread('images_test_rev1/'+str(img_num)+'.jpg')
img = img[100:300, 100:300,:]
img = cv2.resize(img, (100, 100))

plt.subplot(2,2,3), plt.imshow(img)
plt.axis('off')
plt.title('Image')


plt.tight_layout()
#
#img_num = 102557
#
#img = cv2.imread('images_test_rev1/'+str(img_num)+'.jpg', 0)
#img = img[100:300, 100:300]
#img = cv2.resize(img, (100, 100))
#data=img
#center=(data.shape[0]/2,data.shape[1]/2)
#y,x = np.indices((data.shape)) # first determine radii of all pixels
#r = np.sqrt((x-center[0])**2+(y-center[1])**2)    
#
## radius of the image.
#r_max = np.max(r)  
#



#ring_brightness, radius = np.histogram(r, weights=data, bins=r_max/3)
#plt.plot(radius[1:], ring_brightness)
#plt.show()