Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Getting a fully blurred undistored image from cv::undistort

Hello,

I am new to opencv and I am trying to undistort my camera's frames. I have used previously the callibration program from opencv to find the elements of the CameraMatrix and distCoeffs and I wrote the following program:

#include <string>
#include <iostream>
#include <opencv/cv.hpp>

using namespace std;
using namespace cv;

const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;

int main()
{
 Mat frame1, frame2,frame1undist;
 double data[3][3];
 double data2[5];
 data[0][0] = 9.5327626068874099e+02;
 data[0][1] = 0.0;
 data[0][2] = 320.;
 data[1][0] = 0.0;
 data[1][1] = 9.5327626068874099e+02;
 data[1][2] = 240.0;
 data[2][0] = 0.0;
 data[2][1] = 0.0;
 data[2][2] = 1.0;
 data2[0] = -1.1919013558906022e-01;
 data2[1] = -2.9472820562856015e+00;
 data2[2] = 0.0;
 data2[3] = 0.0;
 data2[4] = -1.9208489842061063e+01;
 double avg_reprojection_error = 3.5640854681839190e-01;
 Mat CameraMatrix(3,3,CV_64F,&data);
 Mat NewCameraMatrix;
 Mat distCoeffs(1,5,CV_64F,&data2);
 Mat Result1, Result2;
 double data3[3][3];
 for (int i=0;i<3;i++){
    for (int j=0;j<3;j++){
        data3[i][j] = 0;
        if(i==j) data[i][j] = 1;
    }
 }
 //Mat R(3,3,CV_32FC1,&data3);
 //initUndistortRectifyMap(CameraMatrix,distCoeffs,R,NewCameraMatrix,CvSize(FRAME_WIDTH,FRAME_HEIGHT),CV_32FC1,Result1,Result2);
 VideoCapture capture(0);
 if(!capture.isOpened())    return -1;
 capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
 capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);

 while(true){

 capture.read(frame1);
 //capture.read(frame2);
 undistort(frame1,frame1undist,CameraMatrix,distCoeffs);
 imshow("Frame 1",frame1);
 imshow("Frame 1 undistored", frame1undist);
 //imshow("Frame 2",frame2);

 waitKey(21);
 }


 return 0;
}

The results can be seen in the following printscreen:

image description

As you can see the undistorted frame is not even close to the "distorted" frame that I get from my camera and also it has some weird symbols on it, which disappear if I change the int _type in the Mat() functions that I use. I am quite confused on what is really going on, I thought maybe the int _type i use for the Mat function was the reason, but I don't know how could I fix that.

I am on Codeblocks at Ubuntu 14.04.

Thank you for your answers and for your time in advance,

Chris