平滑连续2D点

时间:2016-12-25 21:01:10

标签: r spline smoothing

更新

感谢@ user20650和@李哲源Zheyuan Li,这是我提出的解决方案:

SharedPreferences

enter image description here

原始帖子

我试图平滑动物轨道的锯齿状路径,以更准确地确定它们的长度。数据采用(x,y)2D坐标的形式。

我拥有的示例数据集相当大(3600行),以更好地说明问题的范围。它在此处以.Rdata文件形式提供:

https://osu.box.com/v/tracks

SharedPreferences

enter image description here

将smooth.spine()应用于整个数据集是不合适的,因为这些动物蜿蜒得很多(走路等等)。

enter image description here

然后,我有了一个想法:将数据拆分为更小的路径并将smooth.spline()应用于每个列表元素。最终目标是将列表重新整合到一个连续,平滑的轨道中。

# Example data set: df
# 3600 observations/points
# Create a vector of the cumulative distances between all of the points
require(Momocs)
cumdist <- coo_perimcum(df)

# Apply splines parametrically - define a spline interpolated mapping R --> R^2 of some curve c
# c(t) = (x(t), y(t))
# 't' is the set of cumulative distances (as defined above)
# Set the number of points to some fraction of the number of observations in the data set (5% in this case)

splines <- cbind.data.frame(x = c(spline(cumdist, df[, 1], method = "natural",
                                         n = ceiling(nrow(df)*0.05))$y),
                            y = c(spline(cumdist, df[, 2], method = "natural",
                                         n = ceiling(nrow(df)*0.05))$y))

plot(df, col = "gray")
lines(splines, col = "red", lwd = 2)

distance <- function(df, mm) # data frame must be in the form (x,y); mm = pixel to mm conversion factor
{
  require(Momocs)
  cumdist <- coo_perimcum(df) # calculates the cumulative Euclidean distance between points
  splines <- cbind.data.frame(x = c(spline(cumdist, df[, 1], method = "natural",
                                           n = ceiling(nrow(df)*0.05))$y),
                              y = c(spline(cumdist, df[, 2], method = "natural",
                                           n = ceiling(nrow(df)*0.05))$y))
  assemble  <- Mod(diff(splines$x+1i*splines$y))*mm
  distance  <- sum(assemble)/1000 # sum the distances and convert to meters
  distance
}

distance(df, 0.444444)
distance(splines, 0.444444)

产生错误:

with(df, plot(x,y, type = "l"))

我可能在这里遗漏了一些非常简单的东西......有什么想法吗?

1 个答案:

答案 0 :(得分:5)

分别平滑<div style="text-align:center"> <ul> <li>test</li> <li>test</li> <li>test</li> <li>test</li> <li>test</li> </ul> </div> x-coord。如果您有曲线y-coord,则可以通过y = y(x)来表示。

x = x(t), y = y(t)

enter image description here