Smoothing Continuous 2D Points

纵饮孤独 提交于 2019-12-11 02:14:45

问题


UPDATE

Thanks to @user20650 and @李哲源 Zheyuan Li, here is the solution I came up with:

# 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)

ORIGINAL POST

I am attempting to smooth the jagged paths of animal tracks to determine their lengths with greater accuracy. The data is in the form of (x,y) 2D coordinates.

The example data set I have is rather large (3600 rows) to better illustrate the scope of the problem. It is available as an .Rdata file here:

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

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

Applying smooth.spine() to the entire data set was inappropriate as these animals meander quite a lot (walking in loops and such).

Then, I got an idea: split up the data into smaller paths and apply smooth.spline() to each list element. The end goal being to re-integrate the list into a continuous, smooth track.

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)

With the resulting error:

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

I'm probably missing something very simple here... Any thoughts?


回答1:


Just smooth x-coord and y-coord separately. If you have a curve y = y(x), you can certainly represent it by 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)



来源:https://stackoverflow.com/questions/41324489/smoothing-continuous-2d-points

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!