7

我用这个问题来帮助我想出一个不失真的图像协调系统。现在,我不确定如何在图像中实现新的坐标系,以便能够生成不失真的图像。

我在使用 R 时无法找到不涉及 Matlab、OpenCV 或C++ 的答案。

我从引用的问题中使用的答案给了我以下转换后的 xy 坐标:

1 -19.50255239, -19.50255239
2 -18.26735544, -18.26735544
3 -17.03391152, -17.03391152
4 -15.80221494, -15.80221494
5 -14.57225998, -14.57225998
6 -13.34404095, -13.34404095
...

以此类推,512 x 512 图像中的 512 像素。

如何将此应用回原始的 512 x 512 图像是我正在努力解决的问题。我在此处的 Open CV 页面和特定的预定义 shifts或latitudinal /longitudinal shifts等页面上看到了一些提及,使用SpatialObjectsDataFrame s,但不是从一个用户定义的 xy 坐标列表到另一个。

- 获取源图像坐标的示例:

im_coords <- RSAGA::grid.to.xyz(as.matrix(as.raster(im)))

(注意,我实际上并不想对图像进行光栅化,这正是我当时发现的)

- 我用来获取转换后坐标的代码:

undistort <- function(X, Y, a, b, c, d = 1, imWidth = 512, imHeight = 512) {

    #radial barrel distortion
    normX <- X - (imWidth / 2)
    normY <- Y - (imHeight / 2)

    #rmax
    radius_u <- sqrt(imWidth^2 + imHeight^2)

    #normalize r so that its between 0 and 1
    r <- sqrt(normX^2 + normY^2) /  radius_u

    #Barrel distorition equation: where "r" is the destination radius and "Rsrc" is the source pixel to get the pixel color from
    Rsrc <- r * (a*r^3 + b*r^2 + c*r + d)

    theta <- ifelse(Rsrc == 0, 1, 1 / atan(Rsrc) * Rsrc)

    newX <- (imWidth / 2) + theta * normX
    newY <- (imHeight / 2) + theta * normY

    return(data.frame(X = newX, Y = newY))
}

这是一个示例样本 512x512 .png 桶形扭曲图像:https ://imgur.com/a/W9Qz70W

在此处输入图像描述

我想知道克里金法是否有用?还是gdalwarpproj4string?不知道如何实现这些。

更新:使用 Rohit 的建议,我能够从以下位置扭曲彩虹网格:

在此处输入图像描述

对此:

在此处输入图像描述

当我用桶形图像尝试它时,我得到了这个奇怪的叠加图像:

在此处输入图像描述

好的,我认为这取决于您使用的系数,如下所示:

在此处输入图像描述

4

1 回答 1

3

您实际上不需要计算转换后的 xy 坐标。您只需要获取 x 和 y 坐标并返回未失真坐标的函数。给定您的undistort函数,围绕它编写一个仅使用 x 和 y 作为输入的包装器:

im2 <- imwarp(im1, function(x,y){ 
  undistort(x,y,a=1,b=2,c=4) # Give appropriate values for arguments, I'm not an expert.
})

如果您想专门从一个列表映射到另一个列表,那么您也可以这样做:

df <- expand.grid(x=1:512,y=1:512) # Original grid coordinates
df1 <- undistort(X=df$x,Y=df$y) # Undistorted grid coordinates
im2 <- imwarp(im1, function(x,y){
  df1[df$x==x & df$y==y,] # Map appropriately. Should still work.
})

尝试不同的选项interpolation以查看最有效的选项。

于 2019-03-18T12:25:45.963 回答