Animation and extrapolation

I am working on a project using numpy and scipy and I need to fill in nanvalues. I am currently using scipy.interpolate.rbf, but it continues to cause python to crash, so try hard / except that it won't even save it. However, after launching it several times, it seems that it can continue to fail in cases where there is data in the middle, surrounded by all nans, like an island. Is there a better solution for this that won't crash?

By the way, this is a lot of data that I need to extrapolate. Sometimes up to half the image (70x70, shades of gray), but it should not be ideal. This is part of the image stitching program, so if it looks like actual data, it will work. I tried the closest neighbor to populate nans, but the results are too different.

EDIT

It seems that the image always triggers. Isolation of this image allowed him to transfer the image ONCE before the failure. Bad image

I use at least version NumPy 1.8.0 and SciPy 0.13.2.

+3
source share
2 answers

Using SciPy LinearNDInterpolator. If all images are the same size, the grid coordinates can be pre-computed and reused.

import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate

x = np.linspace(0, 1, 500)
y = x[:, None]
image = x + y

# Destroy some values
mask = np.random.random(image.shape) > 0.7
image[mask] = np.nan

valid_mask = ~np.isnan(image)
coords = np.array(np.nonzero(valid_mask)).T
values = image[valid_mask]

it = interpolate.LinearNDInterpolator(coords, values, fill_value=0)

filled = it(list(np.ndindex(image.shape))).reshape(image.shape)

f, (ax0, ax1) = plt.subplots(1, 2)

ax0.imshow(image, cmap='gray', interpolation='nearest')
ax0.set_title('Input image')
ax1.imshow(filled, cmap='gray', interpolation='nearest')
ax1.set_title('Interpolated data')
plt.show()

Interpolated missing values

+3

. :

ipn_kernel = np.array([[1,1,1],[1,0,1],[1,1,1]]) # kernel for inpaint_nans

def inpaint_nans(im):
    nans = np.isnan(im)
    while np.sum(nans)>0:
        im[nans] = 0
        vNeighbors = scipy.signal.convolve2d((nans==False),ipn_kernel,mode='same',boundary='symm')
        im2 = scipy.signal.convolve2d(im,ipn_kernel,mode='same',boundary='symm')
        im2[vNeighbors>0] = im2[vNeighbors>0]/vNeighbors[vNeighbors>0]
        im2[vNeighbors==0] = np.nan
        im2[(nans==False)] = im[(nans==False)]
        im = im2
        nans = np.isnan(im)
    return im
+2

All Articles