2
* @function Geometric_Transforms_Demo.cpp
3
* @brief Demo code for Geometric Transforms
7
#include "opencv2/imgcodecs.hpp"
8
#include "opencv2/highgui/highgui.hpp"
9
#include "opencv2/imgproc/imgproc.hpp"
17
const char* source_window = "Source image";
18
const char* warp_window = "Warp";
19
const char* warp_rotate_window = "Warp + Rotate";
24
int main( int, char** argv )
29
Mat rot_mat( 2, 3, CV_32FC1 );
30
Mat warp_mat( 2, 3, CV_32FC1 );
31
Mat src, warp_dst, warp_rotate_dst;
34
src = imread( argv[1], 1 );
36
/// Set the dst image the same type and size as src
37
warp_dst = Mat::zeros( src.rows, src.cols, src.type() );
39
/// Set your 3 points to calculate the Affine Transform
40
srcTri[0] = Point2f( 0,0 );
41
srcTri[1] = Point2f( src.cols - 1.f, 0 );
42
srcTri[2] = Point2f( 0, src.rows - 1.f );
44
dstTri[0] = Point2f( src.cols*0.0f, src.rows*0.33f );
45
dstTri[1] = Point2f( src.cols*0.85f, src.rows*0.25f );
46
dstTri[2] = Point2f( src.cols*0.15f, src.rows*0.7f );
48
/// Get the Affine Transform
49
warp_mat = getAffineTransform( srcTri, dstTri );
51
/// Apply the Affine Transform just found to the src image
52
warpAffine( src, warp_dst, warp_mat, warp_dst.size() );
54
/** Rotating the image after Warp */
56
/// Compute a rotation matrix with respect to the center of the image
57
Point center = Point( warp_dst.cols/2, warp_dst.rows/2 );
61
/// Get the rotation matrix with the specifications above
62
rot_mat = getRotationMatrix2D( center, angle, scale );
64
/// Rotate the warped image
65
warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() );
69
namedWindow( source_window, WINDOW_AUTOSIZE );
70
imshow( source_window, src );
72
namedWindow( warp_window, WINDOW_AUTOSIZE );
73
imshow( warp_window, warp_dst );
75
namedWindow( warp_rotate_window, WINDOW_AUTOSIZE );
76
imshow( warp_rotate_window, warp_rotate_dst );
78
/// Wait until user exits the program