Geometric Transformations and Data Augmentation with OpenCV: Forward/Backward Mapping, Rotation, Translation, and Affine Operations
This article explains traditional image augmentation techniques focusing on geometric transformations such as translation and rotation, describes forward and backward mapping concepts, coordinate‑system conversion, matrix representations, and provides detailed C++ OpenCV examples for implementing these operations with warpAffine and getRotationMatrix2D.
Data augmentation is a common technique in deep learning to increase dataset diversity and improve model generalization. Traditional image algorithms use geometric transformations—scaling, translation, rotation, and affine transforms—to achieve augmentation.
The article introduces the concepts of forward mapping (mapping source pixels to output coordinates) and backward mapping (computing source coordinates from output pixels) and discusses issues such as pixel overlap, holes, and the need for interpolation.
It explains the coordinate‑system conversion between image coordinates (origin at the top‑left) and Cartesian coordinates (origin at the image center) required for rotation and translation, outlining a three‑step process: convert to Cartesian, apply rotation, convert back.
Matrix forms for forward and inverse (backward) mappings are presented, showing that the backward mapping matrix is the inverse of the forward matrix.
Implementation details using OpenCV 4.1.0+ on Windows 10 are provided. The key functions are warpAffine for applying affine transformations and getRotationMatrix2D for obtaining rotation matrices.
void boxFilter( InputArray src, OutputArray dst,
int ddepth,
Size ksize,
Point anchor = Point(-1,-1),
bool normalize = true,
int borderType = BORDER_DEFAULT );Example code for rotating an image by 45° demonstrates calculating the required output size, adjusting the rotation matrix to prevent cropping, and displaying the result:
Mat src = imread("../image/source3.jpg"); // read source image
Mat dst;
double angle = 45;
Size src_sz = src.size();
Size dst_sz(src_sz.height, src_sz.width);
int len = std::max(src.cols, src.rows);
Point2f center(len/2.0, len/2.0);
Mat rot_mat = getRotationMatrix2D(center, angle, 1.0);
warpAffine(src, dst, rot_mat, dst_sz);
imshow("image", src);
imshow("result", dst);
waitKey(0);
return 0;Another example shows how to translate an image by 300 pixels horizontally and vertically using a custom translation matrix with warpAffine.
Mat src = imread("../image/source2.jpg");
Mat dst;
Size dst_sz = src.size();
Mat t_mat = Mat::zeros(2, 3, CV_32FC1);
t_mat.at<float>(0,0) = 1; t_mat.at<float>(0,2) = 300; // x translation
t_mat.at<float>(1,1) = 1; t_mat.at<float>(1,2) = 300; // y translation
warpAffine(src, dst, t_mat, dst_sz);
imshow("image", src);
imshow("result", dst);
waitKey(0);
return 0;A full affine transformation example uses three source points and three destination points to compute the transformation matrix with getAffineTransform and applies it via warpAffine, displaying the original and transformed images.
The article also discusses how to compute the size of the rotated image to avoid clipping by calculating the bounding rectangle of the transformed corner points.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
