Most efficient way to calculate radial profile

后端 未结 4 1425
误落风尘
误落风尘 2020-12-02 12:32

I need to optimize this part of an image processing application.
It is basically the sum of the pixels binned by their distance from the central spot.

d         


        
4条回答
  •  情歌与酒
    2020-12-02 13:14

    You can use numpy.histogram to add up all the pixels that appear in a given "ring" (range of values of r from the origin). Each ring is one of the histogram bins. You choose the number of bins depending on how wide you want the rings to be. (Here I found 3 pixel wide rings work well to make the plot not too noisy.)

    def radial_profile(data, center):
        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()
    

    (By the way, if this really needs to be efficient, and there are a lot of images the same size, then everything before the call to np.histogram can be precomputed.)

提交回复
热议问题