Get only “valid” points in 2D cloud point interpolation using Scipy / Numpy

I have a cloud point obtained from photogrammetry from a person back. I am trying to interpolate it to get a regular grid, and for this I have been using scipy.interpolatewith good results so far. The problem is that the function I use ( scipy.interpolate.griddata) uses the convex hull of the cloud point in the x, y plane, which results in some values ​​that do not exist on the original surface, which has a concave perimeter.

The following figure shows the starting point of cloudiness on the left (what appears as horizontal lines is actually a dense linear cloud of points), the result that griddatagives me in the middle, and the result that I need to get on the right is the “shadow” view cloud points on the x, y plane, where nonexistent points on the original surface are zeros or Nans.

enter image description here

, Z cloudpoint , , , . numpy , numpy 2D- "" griddata, ( "Numpy/Scipy" ).

?

!

+5
1

KDTree. , griddata, "" , .

:

import numpy as np
from scipy.spatial import cKDTree as KDTree
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

# Some input data
t = 1.2*np.pi*np.random.rand(3000)
r = 1 + np.random.rand(t.size)
x = r*np.cos(t)
y = r*np.sin(t)
z = x**2 - y**2

# -- Way 1: seed input with nan

def excluding_mesh(x, y, nx=30, ny=30):
    """
    Construct a grid of points, that are some distance away from points (x, 
    """

    dx = x.ptp() / nx
    dy = y.ptp() / ny

    xp, yp = np.mgrid[x.min()-2*dx:x.max()+2*dx:(nx+2)*1j,
                      y.min()-2*dy:y.max()+2*dy:(ny+2)*1j]
    xp = xp.ravel()
    yp = yp.ravel()

    # Use KDTree to answer the question: "which point of set (x,y) is the
    # nearest neighbors of those in (xp, yp)"
    tree = KDTree(np.c_[x, y])
    dist, j = tree.query(np.c_[xp, yp], k=1)

    # Select points sufficiently far away
    m = (dist > np.hypot(dx, dy))
    return xp[m], yp[m]

# Prepare fake data points
xp, yp = excluding_mesh(x, y, nx=35, ny=35)
zp = np.nan + np.zeros_like(xp)

# Grid the data plus fake data points
xi, yi = np.ogrid[-3:3:350j, -3:3:350j]
zi = griddata((np.r_[x,xp], np.r_[y,yp]), np.r_[z, zp], (xi, yi),
              method='linear')
plt.imshow(zi)
plt.show()

"" , nan. , .

:

# -- Way 2: blot out afterward

xi, yi = np.mgrid[-3:3:350j, -3:3:350j]
zi = griddata((x, y), z, (xi, yi))

tree = KDTree(np.c_[x, y])
dist, _ = tree.query(np.c_[xi.ravel(), yi.ravel()], k=1)
dist = dist.reshape(xi.shape)
zi[dist > 0.1] = np.nan

plt.imshow(zi)
plt.show()
+4

All Articles