我有大量的魚眼鏡頭拍攝的照片。由於我想對照片進行一些圖像處理(例如邊緣檢測),因此我想消除桶形失真,這會嚴重影響我的效果。糾正FishEye鏡頭的桶形失真校正算法 - 未能用Java實現
經過一番研究和大量的閱讀文章,我發現這page:他們描述了一個算法(和一些公式)來解決這個問題。
M = A * rcorr^3 + B * rcorr^2 + C * rcorr + d
RSRC =(A * rcorr^3 + B * rcorr^2 + C * rcorr + d)* rcorrRSRC =從源圖像的中心的像素的距離
rcorr =校正圖像
A,b,C =圖像 d的失真=線性圖像的縮放從中心像素的距離
我用這些公式並試圖在Java應用程序中實現這一點。不幸的是,它不工作,我沒有成功。 「修正」圖像看起來不像原始照片,而是在中間顯示一些神祕的圈子。看看這裏:
http://imageshack.us/f/844/barreldistortioncorrect.jpg/ (這曾經是一個白色的牛在前面的藍色牆壁的照片)
這裏是我的代碼:
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 < dstView.getImgHeight(); y++) {
int dstX = x;
int dstY = y;
// center of dst image
double centerX = (dstView.getImgWidth() - 1)/2.0;
double centerY = (dstView.getImgHeight() - 1)/2.0;
// difference between center and point
double diffX = centerX - dstX;
double diffY = centerY - dstY;
// distance or radius of dst image
double dstR = Math.sqrt(diffX * diffX + diffY * diffY);
// 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 + (diffX * factor);
double srcYd = centerY + (diffX * factor);
// no interpolation yet (just nearest point)
int srcX = (int)srcXd;
int srcY = (int)srcYd;
if(srcX >= 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)還有其他的算法(例如How to simulate fisheye lens effect by openCV?或wiki/Distortion_(光學)),它們更好嗎?
感謝您的幫助!
邊緣附近的像素的正方形網格說明了很多問題可能是什麼。無論您的算法是否適用於任何照片,我都不知道。一個可能的原因是它不起作用,因爲你可能會過度糾正失真。 – AJMansfield
正如我在下面提到的,我嘗試將b設置爲一個無限小的值。它給出了不同的結果(不再有球面校正),但仍然不顯示相同的圖像。請看這裏:http://imageshack.us/f/191/barreldistortioncorrect.jpg/ – Lucas
可能無限小的b值在_other_方向過矯? – AJMansfield