Ask Your Question
0

Getting a fully blurred undistored image from cv::undistort

asked 2016-10-08 13:01:13 -0500

patrchri gravatar image

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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-10-08 13:44:26 -0500

Tetragramm gravatar image

updated 2016-10-08 13:44:42 -0500

There's nothing wrong with the distortion coefficients, the camera matrix, or the undistort function. They produce a perfectly reasonable image when I grab one from my computer.

But those aren't the values you are using. Look in the nested for loops. You are altering the values in data, which is the values you are using in CameraMatrix. When you construct a Mat from a pointer, it doesn't copy the memory. So altering the values inside the data array alters the CameraMatrix.

edit flag offensive delete link more

Comments

Wow !!! I certainly didn't notice that...I feel stupid now, thanks a lot :)

patrchri gravatar imagepatrchri ( 2016-10-08 13:53:11 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-10-08 13:01:13 -0500

Seen: 86 times

Last updated: Oct 08 '16