平滑连续2D点

Tav*_*nis 2 r spline smoothing

更新

感谢@ user20650和@ ??? 李哲远,这是我想出的解决方案:

# 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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

原始帖子

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

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

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

with(df, plot(x,y, type = "l"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

对所有数据集应用smooth.spine()是不合适的,因为这些动物会蜿蜒很多(循环行走等)。

在此处输入图片说明

然后,我有了一个主意:将数据分成较小的路径,然后将smooth.spline()应用于每个列表元素。最终目标是将列表重新整合到连续,流畅的轨道中。

chunks <- list(split(df, (as.numeric(rownames(df))-1) %/% 90))

smooth.tracks <- function(x)
{
  smooth.spline(x, spar = 0.55)
}

df.smooth <- lapply(chunks, smooth.tracks)
Run Code Online (Sandbox Code Playgroud)

结果错误:

Error in xy.coords(x, y) : 
  'x' is a list, but does not have components 'x' and 'y
Run Code Online (Sandbox Code Playgroud)

我可能在这里错过了一些非常简单的内容...有什么想法吗?

李哲源*_*李哲源 5

既平稳x-coordy-coord分开。如果您有曲线y = y(x),则可以肯定地用表示x = x(t), y = y(t)

## load your data frame "df"
t <- 1:nrow(df)
x <- df$x
y <- df$y

sx <- smooth.spline(t, x, df = 50)
sy <- smooth.spline(t, y, df = 50)

plot(df, cex = 0.25, col = "gray")
lines(sx[[2]], sy[[2]], col = 2, lwd = 2)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明