2016-02-15 156 views
1

我在這個鏈接上看到http://www.tannerhelland.com/4743/simple-algorithm-correcting-lens-distortion/一個算法來取消魚眼鏡頭畸變,我試圖用opencv在C++中實現它。當參數強度接近於零時,輸​​出圖像與輸入圖像完全相同,並且在較高值的情況下我得到了不好的結果。如果有人知道可能是什麼問題(在我的代碼或更一般的算法中),這將是非常有用的。 非常感謝。魚眼畸變校正

#include "opencv2\core\core.hpp" 
#include "opencv2\highgui\highgui.hpp" 
#include "opencv2\calib3d\calib3d.hpp" 
#include <stdio.h> 
#include <iostream> 
#include <math.h> 

using namespace std; 
using namespace cv; 

int main() { 

    cout << " Usage: display_image ImageToLoadAndDisplay" << endl; 
    Mat_<Vec3b> eiffel; 
    eiffel = imread("C:/Users/Administrator/Downloads/TestFisheye.jpg", CV_LOAD_IMAGE_COLOR); // Read the file 
    if (!eiffel.data)        // Check for invalid input 
    { 
     cout << "Could not open or find the image" << endl; 
     return -1; 
    } 
    cout << "Input image depth: " << eiffel.depth() << endl; 

    namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display. 
    imshow("Display window", eiffel);     // Show our image inside it. 

    //waitKey(0);           // Wait for a keystroke in the window 

    int halfWidth = eiffel.rows/2; 
    int halfHeight = eiffel.cols/2; 
    double strength = 0.0001; 
    double correctionRadius = sqrt(pow(eiffel.rows, 2) + pow(eiffel.cols, 2))/strength; 
    Mat_<Vec3b> dstImage = eiffel; 

    int newX, newY; 
    double distance; 
    double theta; 
    int sourceX; 
    int sourceY; 
    double r; 
    for (int i = 0; i < dstImage.rows; ++i) 
    { 
     for (int j = 0; j < dstImage.cols; j++) 
     { 
      newX = i - halfWidth; 
      newY = j - halfHeight; 
      distance = sqrt(pow(newX, 2) + pow(newY, 2)); 
      r = distance/correctionRadius; 
      if (r == 0.0) 
       theta = 1; 
      else 
       theta = atan(r)/r; 

      sourceX = round(halfWidth + theta*newX); 
      sourceY = round(halfHeight + theta * newY); 

      dstImage(i, j)[0] = eiffel(sourceX, sourceY)[0]; 
      dstImage(i, j)[1] = eiffel(sourceX, sourceY)[1]; 
      dstImage(i, j)[2] = eiffel(sourceX, sourceY)[2]; 
     } 
    } 

    namedWindow("Display window 2", WINDOW_AUTOSIZE); 
    imshow("Display window 2", dstImage);     // Show our image inside it. 
    waitKey(0); 

    return 0; 
} 

PS:我目前正在處理鏈接中發佈的第一張圖片。

+0

[This](http://marcodiiga.github.io/radial-lens-undistortion-filtering)也許會讓你感興趣 –

回答

2

你有2個問題在這裏:

1 - 你需要從0.0001提高強度會更合理(試行5)。

2 - 您使用的是相同的原點和目標矩陣。這Mat_<Vec3b> dstImage = eiffel;不會分配任何新的內存。 dstImage只是一個指向原始圖像的智能指針。所以當你修改它時,你同時修改源圖像。這會給你很糟糕的結果。相反,做Mat_<Vec3b> dstImage = eiffel.clone()

有了這些變化,我得到了下面的圖片: enter image description here

不是很大,但快速&簡單最少。