I have a 2 dimensional numpy array representing spatial data. I need to increase its resolution. I also need to evenly distribute values across the space. For example, a value of
5
would become:
1.25 1.25
1.25 1.25
I've looked at imresize but I don't think the interpolation options will work for this. Maybe there's another way? I'd like to avoid iterating rows and columns if I can. Any help would be greatly appreciated! Thank you!
Simply divide by the number of elements in the block defined by its height and width and then replicate/ expand. To replicate, we can use np.repeat or np.lib.stride_tricks.as_strided.
With np.repeat -
def upscale_repeat(a, h, w):
return (a/float(h*w)).repeat(h, axis=0).repeat(h, axis=1)
With np.lib.stride_tricks.as_strided using tile_array -
def upscale_strided(a, h, w):
return tile_array(a/float(h*w), h, w)
Sample run -
In [140]: a
Out[140]:
array([[ 7, 6, 9],
[ 6, 6, 10]])
In [141]: upscale_repeat(a, 2, 2)
Out[141]:
array([[ 1.75, 1.75, 1.5 , 1.5 , 2.25, 2.25],
[ 1.75, 1.75, 1.5 , 1.5 , 2.25, 2.25],
[ 1.5 , 1.5 , 1.5 , 1.5 , 2.5 , 2.5 ],
[ 1.5 , 1.5 , 1.5 , 1.5 , 2.5 , 2.5 ]])
In [142]: upscale_repeat(a, 2, 3)
Out[142]:
array([[ 1.17, 1.17, 1.17, 1. , 1. , 1. , 1.5 , 1.5 , 1.5 ],
[ 1.17, 1.17, 1.17, 1. , 1. , 1. , 1.5 , 1.5 , 1.5 ],
[ 1. , 1. , 1. , 1. , 1. , 1. , 1.67, 1.67, 1.67],
[ 1. , 1. , 1. , 1. , 1. , 1. , 1.67, 1.67, 1.67]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With