用于纠正FishEye镜头的桶形失真校正算法 – 无法用Java实现

我用鱼眼镜头拍摄了大量照片。 因为我想对照片进行一些图像处理(例如边缘检测),我想要去除桶形失真,这会严重影响我的结果。

经过一些研究和大量阅读文章,我找到了这个页面 :他们描述了一个算法(和一些公式)来解决这个问题。

M = a * rcorr ^ 3 + b * rcorr ^ 2 + c * rcorr + d
rsrc =(a * rcorr ^ 3 + b * rcorr ^ 2 + c * rcorr + d)* rcorr

rsrc =像素距源图像中心的距离
rcorr =校正图像中像素距中心的距离
a,b,c =图像失真d =图像的线性缩放

我使用这些公式并尝试在Java应用程序中实现它。 不幸的是它不起作用,我没能使它工作。 “修正”图像看起来与原始照片完全不同,而是在中间显示一些神秘的圆圈。 看这里:

http://sofzh.miximages.com/java// (这曾经是蓝色墙前的白牛的照片)

这是我的代码:

protected int[] correction(int[] pixels) { // int[] pixelsCopy = pixels.clone(); // parameters for correction double paramA = 0.0; // affects only the outermost pixels of the image double paramB = -0.02; // most cases only require b optimization double paramC = 0.0; // most uniform correction double paramD = 1.0 - paramA - paramB - paramC; // describes the linear scaling of the image // for(int x = 0; x < dstView.getImgWidth(); x++) { for(int y = 0; y = 0 && srcY >= 0 && srcX < dstView.getImgWidth() && srcY < dstView.getImgHeight()) { int dstPos = dstY * dstView.getImgWidth() + dstX; pixels[dstPos] = pixelsCopy[srcY * dstView.getImgWidth() + srcX]; } } } return pixels; } 

我的问题是:
1)这个公式是否正确?
2)我将这个公式变成一个软件时犯了错误吗?
3)还有其他算法(例如, 如何通过openCV?或wiki / Distortion_(光学) 模拟鱼眼镜头效果 ),它们更好吗?

谢谢你的帮助!

您遇到的主要错误是该算法指定r_corr和r_src以min((xDim-1)/ 2,(yDim-1)/ 2)为单位。 需要进行此操作以规范化计算,以使参数值不依赖于源图像的大小。 使用代码就可以了,你需要为paramB使用更小的值,例如,对于paramB = 0.00000002(对于尺寸为2272 x 1704的图像),它可以正常工作。

您还有一个错误,即计算与中心的差异,导致生成的图像与源图像相比旋转180度。

修复这两个错误应该给你这样的东西:

 protected static int[] correction2(int[] pixels, int width, int height) { int[] pixelsCopy = pixels.clone(); // parameters for correction double paramA = -0.007715; // affects only the outermost pixels of the image double paramB = 0.026731; // most cases only require b optimization double paramC = 0.0; // most uniform correction double paramD = 1.0 - paramA - paramB - paramC; // describes the linear scaling of the image for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int d = Math.min(width, height) / 2; // radius of the circle // center of dst image double centerX = (width - 1) / 2.0; double centerY = (height - 1) / 2.0; // cartesian coordinates of the destination point (relative to the centre of the image) double deltaX = (x - centerX) / d; double deltaY = (y - centerY) / d; // distance or radius of dst image double dstR = Math.sqrt(deltaX * deltaX + deltaY * deltaY); // distance or radius of src image (with formula) double srcR = (paramA * dstR * dstR * dstR + paramB * dstR * dstR + paramC * dstR + paramD) * dstR; // comparing old and new distance to get factor double factor = Math.abs(dstR / srcR); // coordinates in source image double srcXd = centerX + (deltaX * factor * d); double srcYd = centerY + (deltaY * factor * d); // no interpolation yet (just nearest point) int srcX = (int) srcXd; int srcY = (int) srcYd; if (srcX >= 0 && srcY >= 0 && srcX < width && srcY < height) { int dstPos = y * width + x; pixels[dstPos] = pixelsCopy[srcY * width + srcX]; } } } return pixels; } 

使用此版本,您可以使用现有镜头数据库中的参数值,如LensFun(尽管您需要翻转每个参数的符号)。 现在可以在http://mipav.cit.nih.gov/pubwiki/index.php/Barrel_Distortion_Correction上找到描述算法的页面。

我认为你的圈子是由这条线引起的:

 double srcYd = centerY + (diffX * factor); 

我猜应该是:

 double srcYd = centerY + (diffY * factor); 

可能你的径向畸变参数太大了,图像变得很大。 尝试在abcd放入较小的值。

你的价值观非常极端,所以你会看到极端的结果。

尝试a = 0,b = 0,c = 1。 这描述没有任何更正,如果你的程序是正确的,你应该看到原始图像。 然后逐渐改变c和b。 以0.1为增量的变化是一个良好的开端。