#include <opencv2/opencv.hpp>
// Use the 3x3 M projection matrix from the intrinsic calibration
cv::Mat M = ...;
// Use D distortion from the intrinsic calibration (5 or 8 parameters in the standard OpenCV ordering)
cv::Mat D = ...;
// Use the 3x3 R rotation matrix from the extrinsic calibration
cv::Mat R = ...;
// Use the 3x4 P camera projection matrix from the extrinsic calibration
cv::Mat P = ...;
// Width of the output rectified image
int width = ...;
// Height of the output rectified image
int height = ...;
// Output lookup tables for the x and y output image dimensions
cv::Mat remap_x;
cv::Mat remap_y;
cv::initUndistortRectifyMap(M, D, R, P, cv::Size(width, height), CV_32FC1, remap_x, remap_y)
cv::Mat unrectified_image = cv::imread("unrectified_image.png);
cv::Mat rectified_image
// Interpolate using the remap lookup tables using bi-linear interpolation
cv::remap(unrectified_image, rectified_image, remap_x, remap_y, cv::INTER_LINEAR);
cv::imwrite("rectified.png", rectified_image);