Reputation: 19215
Is there any indexing shorthand in numpy to get the center pixels of an image (or any ND array)?
Example:
cutout = xc[xc.shape[0]/2-30:xc.shape[0]/2+30,xc.shape[1]/2-30:xc.shape[1]/2+30]
I could define a function
def get_center_pixels(arr, npix):
slices = [slice(shape/2-npix,shape/2+npix) for shape in arr.shape]
return arr[slices]
cutout = get_center_pixels(xc,30)
Is there a better way or a built-in way to do this?
Upvotes: 3
Views: 1896
Reputation: 67437
The closest standard function in numpy I can think of is numpy.fft.fftshift, which rolls the data along the selected axis, so that the center point now is at [0,0].
Upvotes: 2