Given a set of 2D points, how can I apply the opposite of undistortPoints
?
I have the camera intrinsics and distCoeffs
and would like to (for example) create a square, and distort it as if the camera had viewed it through the lens.
I have found a 'distort' patch here : http://code.opencv.org/issues/1387 but it would seem this is only good for images, I want to work on sparse points.
This question is rather old but since I ended up here from a google search without seeing a neat answer I decided to answer it anyway.
There is a function called
projectPoints
that does exactly this. The C version is used internally by OpenCV when estimating camera parameters with functions likecalibrateCamera
andstereoCalibrate
EDIT:
To use 2D points as input, we can set all z-coordinates to 1 with
convertPointsToHomogeneous
and useprojectPoints
with no rotation and no translation.A simple solution is to use
initUndistortRectifyMap
to obtain a map from undistorted coordinates to distorted ones:I edit to clarify the code is correct:
I cite the documentation of
initUndistortRectifyMap
:undistortPoint
is a simple reverse version of project pointsIn my case I would like to do the following:
Undistort points:
This will undistort the points to the very similar coordinate to the origin of the image, but without distortion. This is the default behavior for the cv::undistort() function.
Redistort points:
The little tricky thing here is to first project the points to the z=1 plane with a linear camera model. After that, you must project them with the original camera model.
I found these useful, I hope it also works for you.
This is main.cpp. It is self-sufficient and does not need anything else but opencv. I don't remember where I found this, it works, I used it in my project. The program eats the set of standard chessboard images and generates json/xml files with all the distortions of the camera.
I have had exactly the same need. Here is a possible solution :