if you want to distort image points you have to apply the distortion with the world2cam function using the inverse polynomial coefficients.
Those can be obtained during the calibration procedure in Matlab. Just press 'Export Data' after calibration and extract the inv. coefficients from the textfile.
The distort function in C++ would look like this:
```c++
void distortPoints(std::vector<cv::Point2f> src, std::vector<cv::Point2f>& dst, cv::Mat ocamModel, float viewfield, cv::Size imageSize)
{
dst.clear();
// get inverse camera matrix of src points
cv::Matx33d Kinv = getPinholeCameraMatrix(viewfield, imageSize).inv();
for (size_t i = 0; i < src.size(); i++)
{
// undistorted point in homogenous coordinates
cv::Vec3d undisPoint = cv::Vec3d(src.at(i).x, src.at(i).y, 1);
// normalize undistorted point
cv::Vec3d normPoint = Kinv * undisPoint;
// get distorted point from world2cam with INVERSE polynomial coefficients
cv::Point2f disPoint = world2cam(normPoint, ocamModel);
dst.push_back(disPoint);
}
}
```
Hope that helped.
On 28.06.2016 13:30, Filip wrote:
> Hi Pete,
>
> I've calibrated camera with 150 field of view and undistorted fisheye image. I'm trying to find good way to apply effect of distortion on some points in undistorted image. I suppose good way is to use cam2world(using C++ code) and after getting X,Y,Z mapping I don't know how to get image coordinates back to the distorted image. I'm aware of losing information when undistorting image but I need to apply only distortion effect back onto undistorted parts (some points of undistorted image) of the image.
>
> I tried with your code to undistort but how I can do the inverse process, transforming points from undistorted image into "same points" in distorted image ? Thank you in advance.
>
>
> Kind regards,
>
> Filip.
double kt[9];
kt[0] = 118.311481351196; kt[1] = 0; kt[2] = 359.5;
kt[3] = 0; kt[4] = 118.311481351196; kt[5] = 239.5;
kt[6] = 0; kt[7] = 0; kt[8] = 1;
double x11, y11, z11;
cam2world(point3D, point2D);
printf("~~!!![0] = %2.4f , [1]=%2.4f , [2]=%2.4f \n", point3D[0], point3D[1], point3D[2]);
point3D[0] /= point3D[2];
point3D[1] /= point3D[2];
point3D[2] = 1.0;
x11 = kt[0] * point3D[0] + kt[1] * point3D[1] + kt[2] * point3D[2];
y11 = kt[3] * point3D[0] + kt[4] * point3D[1] + kt[5] * point3D[2];
z11 = kt[6] * point3D[0] + kt[7] * point3D[1] + kt[8] * point3D[2];
x11 /= z11;
y11 /= z11;
printf("[0] = %2.4f , [1]=%2.4f , [2]=%2.4f \n", x11, y11 , z11);
getchar();
//--------------------------------------
Best regards,
Lin