Upsample and Interpolate a NumPy Array

2024/10/4 17:31:12

I have an array, something like:

array = np.arange(0,4,1).reshape(2,2)> [[0 12 3]]

I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an array is by using:

array = eratemp[0].repeat(2, axis = 0).repeat(2, axis = 1)
[[0 0 1 1][0 0 1 1][2 2 3 3][2 2 3 3]]

but I cannot figure out a way to interpolate the values to remove the 'blocky' nature between each 2x2 section of the array.

I want something like this:

[[0 0.4 1 1.1][1 0.8 1 2.1][2 2.3 3 3.1][2.1 2.3 3.1 3.2]]

Something like this (NOTE: these will not be the exact numbers). I understand that it may not be possible to interpolate this particular 2D grid, but using the first grid in my answer, an interpolation should be possible during the upsampling process as you are increasing the number of pixels, and can therefore 'fill in the gaps'.

I am not too fussed on the type of interpolation, providing the final output is a smoothed surface! I have tried to use the scipy.interp2d method but to no avail, would be grateful if someone could share their wisdom!

Answer

You can use SciPy interp2d for the interpolation, you can find the documentation here.

I've modified the example from the documentation a bit:

from scipy import interpolate
x = np.array(range(2))
y = np.array(range(2))
a = np.array([[0, 1], [2, 3]])
f = interpolate.interp2d(x, y, a, kind='linear')xnew = np.linspace(0, 2, 4)
ynew = np.linspace(0, 2, 4)
znew = f(xnew, ynew)

If you print znew it should look like this:

array([[ 0.        ,  0.66666667,  1.        ,  1.        ],[ 1.33333333,  2.        ,  2.33333333,  2.33333333],[ 2.        ,  2.66666667,  3.        ,  3.        ],[ 2.        ,  2.66666667,  3.        ,  3.        ]])
https://en.xdnf.cn/q/70586.html

Related Q&A

How to extract text from table in image?

I have data which in a structured table image. The data is like below:I tried to extract the text from this image using this code:import pytesseract from PIL import Imagevalue=Image.open("data/pic…

numpy.savetxt tuple index out of range?

Im trying to write a few lines into a text file, and heres the code I used:import numpy as np# Generate some test data data = np.arange(0.0,1000.0,50.0)with file(test.txt, w) as outfile: outfile.w…

Retrieve list of USB items using Python

How can I retrieve the items plugged into the computer through USB using python? Ive searched around on here and found some old examples which dont appear to work anymore as they are over 5 years old.…

History across ipdb sessions

This question has been asked before, but I couldnt find a good answer. So, I am trying to ask again.I would like my ipdb to remember commands across sessions. Right now, it can pull up commands execute…

Python Distributed Computing (works)

Im using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this?sock.pyfrom socket import socket from socket import AF_INET from socket import…

Django - Stream request from external site as received

How can Django be used to fetch data from an external API, triggered by a user request, and stream it directly back in the request cycle without (or with progressive/minimal) memory usage?BackgroundAs…

django rest framework - always INSERTs, never UPDATES

I want to be able to UPDATE a user record by POST. However, the id is always NULL. Even if I pass the id it seems to be ignoredView Code:JSON POSTED:{"id": 1, "name": "Craig Ch…

Copy fields from one instance to another in Django

I have the following code which takes an existing instance and copies, or archives it, in another model and then deletes it replacing it with the draft copy. Current Codedef archive_calc(self, rev_num,…

Python selenium get Developer Tools →Network→Media logs

I am trying to programmatically do something that necessarily involves getting the "developer tools"→network→media logs. I will spare you the details, long story short, I need to visit thou…

deep copy nested iterable (or improved itertools.tee for iterable of iterables)

PrefaceI have a test where Im working with nested iterables (by nested iterable I mean iterable with only iterables as elements). As a test cascade considerfrom itertools import tee from typing import …