校正R中x,y系列中的镜筒透镜畸变 [英] Correcting barrel lens distortion in an x,y series in R

查看:121
本文介绍了校正R中x,y系列中的镜筒透镜畸变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理想情况下,我想使用与ImageMagick的-distort函数相同的算法,但是在R中实现,以便转换原始x,y坐标,而不是图像.使用ImageMagick还可以使我直观地看到失真的效果.

Ideally, I would like to use an identical algorithm to ImageMagick's -distort function, but implemented in R so as to transform raw x,y coordinates, rather than images. Using ImageMagick would also allow me to gain a visual proof of the effectiveness of the undistortion.

我有一个x,y系列,它是通过跟踪软件对视频进行分析得出的.为了提高跟踪坐标的准确性,视频中的原始图像有些桶形镜头失真,我想对其进行校正.

I have an x,y series, produced from analysis of a video by a tracking software. The original images in the video have some barrel lens distortion that I would like to correct in order to improve the accuracy of the tracking coordinates.

我可以使用ImageMagick中的distort运算符执行看起来合适的校正,例如:

I can perform what looks like a suitable correction using the distort operator in ImageMagick, e.g.:

convert input.jpg -distort barrel '0 -0.022 0' output.jpg

现在,我知道可以对视频中的每个帧(跟踪之前)应用此校正,但这似乎不是最好的选择,因为我有数十个视频,每个视频都包含> 7e4帧.在跟踪之后,将校正应用于x,y坐标本身似乎要容易得多.

Now I know I could apply this correction to each frame in the video (before tracking), but this doesn't seem like the best option as I have dozens of videos, each of which consists of >7e4 frames. It seems like it would be much easier to apply the correction to the x,y coordinates themselves after tracking.

从ImageMagick 文档中,桶​​形失真方程为:

From the ImageMagick documentation, the barrel distortion equation is:

Rsrc = r *(A r ^ 3 + B r ^ 2 + C * r + D)

Rsrc = r * ( Ar^3 + Br^2 + C*r + D )

其中"r"是目标半径,"Rsrc"是从中获取像素颜色的源像素.半径被标准化,以使输入图像的最小最小宽度或高度的半径='1.0'."

Where "r" is the destination radius and "Rsrc" is the source pixel to get the pixel color from. the radii are normalized so that radius = '1.0' for the half minimum width or height of the input image."

但是我不知道如何在R中实现此功能以转换x,y系列.有人可以提供任何帮助吗?谢谢!

But I'm unaware of how to implement this in R in order to transform an x,y series. Could anyone offer any help? Thanks!

我已经试验了自己的功能,修改了发现的简单算法

I've experimented with my own function, modifying a simple algorithm I found here, but this seems to introduce a greater barrel distortion if anything (and the polarity of which does not seem to be able to be reversed):

undistortFun <- function(X, Y, strength) {

    imWidth <- 640
    imHeight <- 480

    radius_u <- sqrt(imWidth^2 + imHeight^2) / strength

    normX <- X - (imWidth / 2)
    normY <- Y - (imHeight / 2)

    distance <- sqrt(normX^2 + normY^2)
    r <- distance / radius_u

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

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

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

}

类似的问题

我发现了两个类似的问题,在这里此处,但这些关注的是不失真的图像,而不是原始的x,y坐标,并以我不熟悉的Java和C ++实现.

Similar questions

I found two similar questions, here and here, but these are concerned with undistorting images rather than raw x,y coordinates, and are implemented in Java and C++, which I'm not familiar with.

推荐答案

通过使用ImageMagick失真算法修改问题中提供的功能,我得到了一个比较满意的解决方案:

I've got a somewhat satisfactory solution by modifying the function provided in the question, using the ImageMagick distortion algorithm:

Rsrc = r *(A r ^ 3 + B r ^ 2 + C * r + D)

Rsrc = r * ( Ar^3 + Br^2 + C*r + D )

像这样:

undistortFun <- function(x, y, a, b, c, d = 1, imWidth = 640, imHeight = 480) {

    normX <- X - (imWidth / 2)
    normY <- Y - (imHeight / 2)

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

    r <- sqrt(normX^2 + normY^2) /  radius_u

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

尽管我不确定theta的计算,但指定零失真(a = 0,b = 0,c = 0)仍会导致变换.但是,它似乎可以满足我在这些示例中的要求:

Although I'm uncertain about the calculation of theta, and specifying zero distortion (a=0, b=0, c=0) still leads to a transformation. Nevertheless, it appears to do what I wanted in these samples:

原始x,y数据图:

调整后的x,y数据的图(其中:a = 0,b = -0.02,c = 0):

Plot of adjusted x,y data (where: a =0, b= -0.02, c = 0):

这篇关于校正R中x,y系列中的镜筒透镜畸变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆