OpenCV undistort

时间:2014-05-09 14:49:57

标签: c++ opencv graphics

我在Windows 7上使用OpenCV(2.4.8),我试图校准并取消保存从GoPro相机拍摄的图像。

使用GoPro上的实时选项通过WiFi完成校准。

我使用此代码(取自Here并由我修改一下):

int numBoards = 10;
int numCornersHor = 9;
int numCornersVer = 6;

int numSquares = numCornersHor * numCornersVer;
Size board_sz = Size(numCornersHor, numCornersVer);

vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;

vector<Point2f> corners;
int successes=0;

Mat image;
Mat gray_image;
*videoCapture >> image;
cvtColor(image, gray_image, CV_BGR2GRAY);
vector<Point3f> obj;
for(int j=0;j<numSquares;j++)
{
    obj.push_back(Point3f(j/numCornersHor, j%numCornersHor, 0.0f));
}

while(successes<numBoards)
{
    bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

    if(found)
    {
        cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
        drawChessboardCorners(gray_image, board_sz, corners, found);
        imshow("Image", image);
        imshow("Grayscale Image", gray_image);
    }


    *videoCapture >> image;

    int key = waitKey(1);
    if(key==27)
        return false;

    if(key==' ' && found!=0)
    {
        image_points.push_back(corners);
        object_points.push_back(obj);
        printf("Snap stored!\n");

        successes++;

        if(successes>=numBoards)
            break;
    }
}
//cameraMatrix = Mat(3, 3, CV_32FC1);
FileStorage fs(CALIBRATION_XML_FILE, FileStorage::WRITE);
vector<Mat> rvecs;
vector<Mat> tvecs;
//cameraMatrix.ptr<float>(0)[0] = 1;
//cameraMatrix.ptr<float>(1)[1] = 1;
cv::calibrateCamera(object_points, image_points, image.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
time_t rawtime; time(&rawtime);

fs << "calibrationDate" << asctime(localtime(&rawtime));
fs << "cameraMatrix" << cameraMatrix;
fs << "distCoeffs" << distCoeffs;
fs.release();
Mat imageUndistorted;
while(true)
{
    *videoCapture >> image;
    undistort(image, imageUndistorted, cameraMatrix, distCoeffs);

    imshow("Original", image);
    imshow("Undistorted", imageUndistorted);

    int key = waitKey(1);
    if(key==27)
        return true;
}

并且在校准结束后,Undistoreted窗口会很好地显示未失真的视频Feed。

这些矩阵通过OpenCV的FileStorage保存到XML文件中。

然后我关闭应用程序并以另一种模式运行它以扭曲之前保存的图像(也使用相同的相机拍摄)。

首先我正在阅读XML文件(我在加载后打印矩阵,它们匹配XML文件以便加载正常)再次使用FileStorage类

然后我使用此代码来取消图片的分类:

Mat outMat;
undistort(inputImage, outMat, cameraMatrix, distCoeffs);
return outMat;

但结果太可怕了。

输入图片: enter image description here

未失真的图像: enter image description here

在调用undistort之前,我是否遗漏了需要完成的事情?

由于

修改

如果重要,这是使用FileStorage保存的XML文件:

<?xml version="1.0"?>
<opencv_storage>
<calibrationDate>"Fri May 09 17:12:58 2014&#x0a;"</calibrationDate>
<cameraMatrix type_id="opencv-matrix">
    <rows>3</rows>
    <cols>3</cols>
    <dt>d</dt>
    <data>
        2.2187712262636470e+002 0. 2.2825108444773281e+002 0.
        2.1495762743910956e+002 1.0916298701649789e+002 0. 0. 1.</data></cameraMatrix>
<distCoeffs type_id="opencv-matrix">
    <rows>1</rows>
    <cols>5</cols>
    <dt>d</dt>
    <data>
        -3.0914887115528705e-001 1.1236389287266597e-001
         2.0789692021046750e-002 -2.6151676079193696e-003
        -1.7270919007796915e-002</data></distCoeffs>
</opencv_storage>

0 个答案:

没有答案