Luca
Luca

Reputation: 10996

python numpy/scipy zoom changing center

I have a 2D numpy array, say something like:

import numpy as np
x = np.random.rand(100, 100)

Now, I want to keep zoom this image (keeping the size the same i.e. (100, 100)) and I want to change the centre of the zoom.

So, say I want to zoom keeping the point (70, 70) at the centre and normally how one would do it is to "translate" the image to that point and then zoom.

I wonder how I can achieve this with scipy. I wonder if there is way to specify say 4 coordinates from this numpy array and basically fill the canvas with the interpolated image from this region of interest?

Upvotes: 0

Views: 675

Answers (1)

Richard
Richard

Reputation: 3414

You could use ndimage.zoom to do the zooming part. I use ndimage a lot, and it works well and is fast. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html

The 4 coordinates part you mention is I presume two corners of region you want to zoom into. That's easy by just using numpy slicing of your image (presuming your image is an np array):

your_image[r1:r2, c1:c2]

Assuming you want your output image at 100x100, then your r1-r2, and c1-c2 differences will be the same, so your region is square.

nd.zoom takes a zoom factor (float). You would need to compute whta athat zoom factor is in order to take your sliced image and turn it into a 100x100 sized array:

ndimage.zoom(your_image[r1:r2, c1:c2], zoom=your_zoom_factor)

Upvotes: 1

Related Questions