Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resample and resize numpy array

I would like to resample a numpy array as suggested here Resampling a numpy array representing an image however this resampling will do so by a factor i.e.

x = np.arange(9).reshape(3,3)
print scipy.ndimage.zoom(x, 2, order=1)

Will create a shape of (6,6) but how can I resample an array to its best approximation within a (4,6),(6,8) or (6,10) shape for instance?

like image 851
user3592687 Avatar asked Apr 13 '26 02:04

user3592687


1 Answers

Instead of passing a single number to the zoom parameter, give a sequence:

scipy.ndimage.zoom(x, zoom=(1.5, 2.), order=1)
#array([[0, 0, 1, 1, 2, 2],
#       [2, 2, 3, 3, 4, 4],
#       [4, 4, 5, 5, 6, 6],
#       [6, 6, 7, 7, 8, 8]])

With the sequences (2., 2.75) and (2., 3.5) you will get output arrays with shapes (6, 8) and (6, 10), respectively.

like image 128
Saullo G. P. Castro Avatar answered Apr 14 '26 16:04

Saullo G. P. Castro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!