对于桶形失真的图像,如何执行图像变形或从一个坐标系到另一个坐标系的转换?

Squ*_*eak 8 opencv r image warp

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

我在使用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
...
Run Code Online (Sandbox Code Playgroud)

依此类推,对于512 x 512图像中的512像素。

我一直在努力如何将其应用于原始的512 x 512图像。我已经看到了类似的打开CV页页提到了一些在这里和具体的预先定义的变化,或纬度/纵向偏移,使用SpatialObjectsDataFrame S,但不能从xy坐标到另一个的一个用户自定义列表。

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

im_coords <- RSAGA::grid.to.xyz(as.matrix(as.raster(im)))
Run Code Online (Sandbox Code Playgroud)

(请注意,我实际上并不想光栅化图像,仅是我当时发现的图像)

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

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

这是512x512 .png桶变形的样本示例:https : //imgur.com/a/W9Qz70W

在此处输入图片说明

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

更新:使用罗希特的建议,我可以使彩虹网格从以下位置变形

在此处输入图片说明

对此:

在此处输入图片说明

当我尝试使用桶形图像时,会得到以下奇怪的叠加图像:

在此处输入图片说明

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

在此处输入图片说明

Roh*_*hit 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.
})
Run Code Online (Sandbox Code Playgroud)

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

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

尝试不同的选项,interpolation看看哪种效果最好。

  • 所以我能够让它在我制作的扭曲图像的彩虹网格上工作,但是当我在桶形扭曲图片上尝试它时,它只是显示为全黑。我将用图片更新说明。 (2认同)