Tuesday, 15 January 2013

numpy: 1d histogram based on 2d-pixel euclidean distance from center -


I am using Python, Sippy, Nine, etc.

I want to calculate the histogram, the intensity value of a grayscale image based on the distance of pixels from the center of the mass of the image. The following solution works, but is very slow:

  Import import as SPL import from SPP as matplotlib.pyplot Import IMP # IMG is a 2-dimensional numerical array Img = np.random (300, 300) # Pixel's large scale center centerOfMass = np.array (easy to get list (ndim.measurements.center_of_mass (img)) # Histogram Baltics Histogram = NPJaros (100) # Announce histogram range, which is half the diagonal length of the image , In this case is sufficient. MaxDist = len (img) /math.sqrt (2.0) # Bucket size may be less than the width of the pixel, which is fine. Bucket size = maxDist / len (histogram) for category # I fill the histogram bucket (len (img)): for the range (len (img [i])): dist = np.linalg.norm (centerOfMass - np. Array ([i, j])) If (dist / Bucket size Lion histogram Histogram [= I 'Hot') plt.colorbar () plt.draw () #plow Histogram plt.subplot (122) plt.plot (histogram) plt.draw () plt.show ()   

As I said earlier, it works but is very slow Because you are not considered double-loop arrays in numpy in this way. Is there a more efficient way of doing the same thing? I think I need to apply some functions to all array elements, but I also need indexing coordinates. How can I do that? Currently it takes several seconds for 1kx1k image, which is ridiculously slow.

all numpy binning functions ( binwont , histogram 2d ... is a weight keyword argument that you can actually use for funny things, such as yours. Do this:

  Rows , Calls = 300, 300 IMG = N.P.Ramand.Red (Rows, Columns) # Calculate the center of the public status row_com = np.sum (np.arange (rows) [:, none] * img) / np. Sum (img) col_com = np.sum (np.arange (cols) * img) / np.sum (img) # head of distance for the center of mass dist = np Create Sqrt ((((np.arange (rows) - row_com) ** 2) [:, none] + (np.arange (cols) - col_com) ** 2) # Histogram formation, intensity as load = 100 hists, edges = np.histogram (dist, bins = bins, weights = img) # To reproduce your exact results, you have bin edges bins = np.linspace (0, len (img) / math. Sqrt (2.0), 101 must be specified) Hist 2, edges 2 = N. P. Aostogram (dist, cans = cans, weight = IMG)   

Both methods have not expired , But termin Judging from running late, it is very fast.

No comments:

Post a Comment