Ask Your Question

Revision history [back]

Distorting image using fisheye::distortPoints()

I am trying to distort an undistorted image to the fisheye image. I tried following answer to this question: http://answers.opencv.org/question/172922/correct-use-of-fisheyedistortpoints/, however I still cannot find the right solution. I am using code below:

Matx33d K;
K << 685.9644616537298, 0.0, 969.9887121786347, 0.0, 682.2969510421883, 1010.9140515134505, 0.0, 0.0, 1.0;
Vec4d D;
D << -0.021830238046820022, 0.0018477526860139503, -0.008062484741672862, 0.002183790781560894;
Matx33d K_inv = K.inv();

string path = "lena.jpg";
Mat img = imread(path);
Mat distorted_img;

vector<Point2f> srcp; //array of undistorded points
vector<Point2f> dstp; //array of distorded points

for (int i = 0; i < img.rows; i++)
{
    for (int j = 0; j < img.cols; j++)
    {
        srcp.push_back(Point2f(i, j));
    }
}

for (size_t i = 0; i < srcp.size(); i++) {
    Vec3d srcv = Vec3d(srcp[i].x, srcp[i].y, 1.0); //Creating a vector in homogeneous coords
    Vec3d dstv = K_inv * srcv; //Doing martix by vector multiplication
    srcp[i].x = dstv[0]; //Extracting resulting normalised x coord
    srcp[i].y = dstv[1]; //Extracting resulting normalised y coord
}

fisheye::distortPoints(srcp, dstp, K, D); //Performing distortion. D is distortion vector

Mat srcp_mat = Mat(img.rows, img.cols, CV_32F, srcp.data());
Mat dstp_mat = Mat(img.rows, img.cols, CV_32F, dstp.data());

remap(img, distorted_img, srcp_mat, dstp_mat, INTER_CUBIC);

Results look like this: image description

Size of both images is 1960 x 1960. Am i doing the remaping wrong? Is the problem caused by using normalized coordinates with remap function?