libCalib
C++ library documentation
distortion_maps/main.cpp

Shows how a distortion map can be used to undistort an image to be pinhole-like.

// Read lens distortion maps and use them to undistort a new camera image.
//
// (c) Calib.io ApS
#include <opencv2/opencv.hpp>
bool readCalibDistortionMap(const std::string &filePathBase, cv::Matx33d &K,
cv::Mat_<float> &mapU, cv::Mat_<float> &mapV) {
cv::FileStorage fs(filePathBase + "_K.json", cv::FileStorage::READ);
try {
fs["K"] >> K;
} catch (...) {
return false;
}
mapU = cv::imread(filePathBase + "_mapU.exr", cv::IMREAD_UNCHANGED);
mapV = cv::imread(filePathBase + "_mapV.exr", cv::IMREAD_UNCHANGED);
return true;
}
int main(int argc, char *argv[]) {
const cv::String keys =
"{help h usage ? | | print this message}"
"{@distortionPathBase |<none>| common part of mapU, mapV and K files}"
"{@imagePath |<none>| the camera image to be undistorted}";
cv::CommandLineParser parser(argc, argv, keys);
std::string distortionPathBase = parser.get<std::string>(0);
std::string imagePath = parser.get<std::string>(1);
if (parser.has("help") || !parser.check()) {
parser.printMessage();
return 0;
}
cv::Matx33d K;
cv::Mat_<float> mapU, mapV;
readCalibDistortionMap(distortionPathBase, K, mapU, mapV);
cv::Mat im = cv::imread(imagePath);
cv::Mat imUndistorted;
cv::remap(im, imUndistorted, mapU, mapV, cv::INTER_CUBIC);
cv::imshow("im", im);
cv::imshow("imUndistorted", imUndistorted);
cv::waitKey(0);
return 0;
}