warpAffine
Usage
Performs affine transformation on an image based on a given matrix and returns the transformed image. Affine transformation is two-dimensional and may include rotation, scaling, translation, and cropping.
Interface
1 | void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags = INTER_LINEAR, int borderMode = BORDER_CONSTANT, const Scalar& borderValue = Scalar()); |
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Input image. |
Not null |
Input |
dst |
Output image. |
Mat class |
Output |
M |
Affine transformation matrix. |
2x3 |
Input |
dsize |
Size of the output image. |
(-inf, inf) |
Input |
flags |
Interpolation method. |
INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, and INTER_LANCZOS4 |
Input |
borderMode |
Border mode, which determines how to process image borders. |
BORDER_CONSTANT, BORDER_REPLICATE, and BORDER_REFLECT |
Input |
borderValue |
Constant used to populate borders. |
(-inf, inf) |
Input |
Return Values
- Success: KP_CV_SUCCESS
- Failure: an error code
Error Codes
Error Code |
Description |
|---|---|
INVALID_PARAM_MSG |
The input image is null. |
NOT_SUPPORT_MSG |
The input image type is incorrect or the flags value is not supported. |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <opencv2/opencv.hpp> #include <iostream> int main() { // Create a 5x5 image. cv::Mat src = (cv::Mat_<uint8_t>(5, 5) << 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0); // Define the affine transformation matrix. cv::Mat M = (cv::Mat_<float>(2, 3) << 1, 0, 1, 0, 1, 1); // Apply affine transformation. cv::Mat dst; cv::warpAffine(src, dst, M, cv::Size(5, 5), cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar(0)); // Output image std::cout << std::endl << dst << std::endl; return 0; } |
Output:
1 2 3 4 5 | [[0 0 0 0 0] [0 0 0 0 0] [0 0 1 1 1] [0 0 1 0 1] [0 0 1 1 1]] |