user3794176
user3794176

Reputation: 85

OpenCV How to apply camera distortion to an image

I have an rendered Image. I want to apply radial and tangential distortion coefficients to my image that I got from opencv. Even though there is undistort function, there is no distort function. How can I distort my images with distortion coefficients?

enter image description here

Upvotes: 3

Views: 2140

Answers (1)

Gopiraj
Gopiraj

Reputation: 657

I was also looking for the same type of functionality. I couldn't find it, so I implemented it myself. Here is the C++ code.

First, you need to normalize the image point using the focal length and centers

rpt(0) = (pt_x - cx) / fx
rpt(1) = (pt_y - cy) / fy

then distort the normalized image point

double x = rpt(0), y = rpt(1);

//determining the radial distortion
double r2 = x*x + y*y;
double icdist = 1 / (1 - ((D.at<double>(4) * r2 + D.at<double>(1))*r2 + D.at<double>(0))*r2);

//determining the tangential distortion
double deltaX = 2 * D.at<double>(2) * x*y + D.at<double>(3) * (r2 + 2 * x*x);
double deltaY = D.at<double>(2) * (r2 + 2 * y*y) + 2 * D.at<double>(3) * x*y;
x = (x + deltaX)*icdist;
y = (y + deltaY)*icdist;

then you can translate and scale the point using the center of projection and focal length:

x = x * fx + cx
y = y * fy + cy

Upvotes: 4

Related Questions