简体   繁体   English

Python - 如何对 2D 形状进行重采样?

[英]Python - How to resample a 2D shape?

I am writing a python script for some geometrical data manipulation (calculating motion trajectories for a multi-drive industrial machine).我正在为一些几何数据操作编写 python 脚本(计算多驱动工业机器的运动轨迹)。 Generally, the idea is that there is a given shape (let's say - an ellipse, but it general case it can be any convex shape, defined with a series of 2D points), which is rotated and it's uppermost tangent point must be followed.一般来说,想法是有一个给定的形状(比方说 - 一个椭圆,但一般情况下它可以是任何凸形,用一系列二维点定义),它被旋转并且必须遵循它的最上面的切点。 I don't have a problem with the latter part but I need a little hint with the 2D shape preparation.我对后一部分没有问题,但我需要一些关于 2D 形状准备的提示。

Let's say that the ellipse was defined with too little points, for example - 25. (As I said, ultimately this can be any shape, for example a rounded hexagon).假设椭圆定义的点太少,例如 - 25。(正如我所说,最终这可以是任何形状,例如圆形六边形)。 To maintain necessary precision I need far more points (let's say - 1000), preferably equally distributed over whole shape or with higher density of points near corners, sharp curves, etc.为了保持必要的精度,我需要更多的点(比方说 - 1000),最好在整个形状上均匀分布,或者在拐角、锐曲线等附近具有更高密度的点。

I have a few things ringing in my head, I guess that DFT (FFT) would be a good starting point for this resampling, analyzing the scipy.signal.resample() I have found out that there are far more functions in the scipy.signal package which sound promising to me...我脑子里闪过一些东西,我想 DFT (FFT) 将是这个重采样的一个很好的起点,分析scipy.signal.resample()我发现 scipy.signal.resample() 中有更多的功能scipy.signal对我来说听起来很有希望的scipy.signal包......

What I'm asking for is a suggestion which way I should follow, what tool I should try for this job, which may be the most suitable.我要的是一个建议,我应该遵循哪种方式,我应该为这份工作尝试什么工具,这可能是最合适的。 Maybe there is a tool meant exactly for what I'm looking for or maybe I'm overthinking this and one of the implementations of FFT like resample() will work just fine (of course, after some adjustments at the starting and ending point of the shape to make sure it's closing without issues)?也许有一个工具正好适合我正在寻找的东西,或者也许我想得太多了,像 resample() 这样的 FFT 实现之一就可以正常工作(当然,在开始和结束点进行一些调整之后形状以确保它没有问题地关闭)?

Scipy.signal sounds promising, however, as far as I understand, it is meant to work with time series data, not geometrical data - I guess this may cause some problems as my data isn't a function (in a mathematical understanding). Scipy.signal听起来很有希望,但是,据我所知,它旨在处理时间序列数据,而不是几何数据——我想这可能会导致一些问题,因为我的数据不是函数(在数学理解中)。

Thanks and best regards!谢谢并致以最诚挚的问候!

As far as I understood, what you want is to get an interpolated version of your original data.据我了解,您想要的是获取原始数据的插值版本。

The DFT (or FFT) will not achieve this purpose, since it will perform an Fourier Transform (which is not what you want). DFT(或 FFT)不会达到这个目的,因为它会执行傅里叶变换(这不是你想要的)。

Talking theoretically, what you need to interpolate your data is to define a function to calculate the result in the new-data-points.从理论上讲,您需要对数据进行插值是定义一个函数来计算新数据点中的结果。

So, let's say your data contains 5 points, in which one you have a 1D (to simplify) number stored, representing your data, and you want a new array with 10 points, filled with the linear-interpolation of your original data.因此,假设您的数据包含 5 个点,其中存储了一个 1D(简化)数字,代表您的数据,并且您想要一个包含 10 个点的新数组,其中填充了原始数据的线性插值。

Using numpy.interp :使用numpy.interp

import numpy as np
original_data = [2, 0, 3, 5, 1] # define your data in 1D
new_data_resolution = 0.5 # define new sampling distance (i.e, your x-axis resolution)
interp_data = np.interp(
    x = np.arange(0, 5-1+new_data_resolution , new_data_resolution), # new sampling points (new axis)
    xp = range(original_data),
    fp = original_data
)
# now interp_data contains (5-1) / 0.5 + 1 = 9 points

After this, you will have a (5-1) / new_resolution (which is greater than 5, since new_resolution < 1 )-length data, which values will be (in this case) a linear interpolation of your original data.在此之后,您将拥有(5-1) / new_resolution (大于 5,因为new_resolution < 1 )长度数据,这些值将是(在这种情况下)原始数据的线性插值。

After you have achieved/understood this example, you can dive in the scipy.interpolate module to get a better understanding in the interpolation functions (my example uses a linear function to get the data in the missing points).实现/理解此示例后,您可以深入了解scipy.interpolate 模块以更好地理解插值函数(我的示例使用线性函数来获取缺失点中的数据)。

Applying this to nD dimensional arrays is straight-forward, iterating over each dimension of your data.将此应用于 nD 维数组非常简单,遍历数据的每个维度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM