1 /**
2 * @function Geometric_Transforms_Demo.cpp
3 * @brief Demo code for Geometric Transforms
4 * @author OpenCV team
5 */
6
7 #include "opencv2/imgcodecs.hpp"
8 #include "opencv2/highgui.hpp"
9 #include "opencv2/imgproc.hpp"
10 #include <iostream>
11
12 using namespace cv;
13 using namespace std;
14
15 /**
16 * @function main
17 */
main(int argc,char ** argv)18 int main( int argc, char** argv )
19 {
20 //! [Load the image]
21 CommandLineParser parser( argc, argv, "{@input | lena.jpg | input image}" );
22 Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
23 if( src.empty() )
24 {
25 cout << "Could not open or find the image!\n" << endl;
26 cout << "Usage: " << argv[0] << " <Input image>" << endl;
27 return -1;
28 }
29 //! [Load the image]
30
31 //! [Set your 3 points to calculate the Affine Transform]
32 Point2f srcTri[3];
33 srcTri[0] = Point2f( 0.f, 0.f );
34 srcTri[1] = Point2f( src.cols - 1.f, 0.f );
35 srcTri[2] = Point2f( 0.f, src.rows - 1.f );
36
37 Point2f dstTri[3];
38 dstTri[0] = Point2f( 0.f, src.rows*0.33f );
39 dstTri[1] = Point2f( src.cols*0.85f, src.rows*0.25f );
40 dstTri[2] = Point2f( src.cols*0.15f, src.rows*0.7f );
41 //! [Set your 3 points to calculate the Affine Transform]
42
43 //! [Get the Affine Transform]
44 Mat warp_mat = getAffineTransform( srcTri, dstTri );
45 //! [Get the Affine Transform]
46
47 //! [Apply the Affine Transform just found to the src image]
48 /// Set the dst image the same type and size as src
49 Mat warp_dst = Mat::zeros( src.rows, src.cols, src.type() );
50
51 warpAffine( src, warp_dst, warp_mat, warp_dst.size() );
52 //! [Apply the Affine Transform just found to the src image]
53
54 /** Rotating the image after Warp */
55
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 );
58 double angle = -50.0;
59 double scale = 0.6;
60 //! [Compute a rotation matrix with respect to the center of the image]
61
62 //! [Get the rotation matrix with the specifications above]
63 Mat rot_mat = getRotationMatrix2D( center, angle, scale );
64 //! [Get the rotation matrix with the specifications above]
65
66 //! [Rotate the warped image]
67 Mat warp_rotate_dst;
68 warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() );
69 //! [Rotate the warped image]
70
71 //! [Show what you got]
72 imshow( "Source image", src );
73 imshow( "Warp", warp_dst );
74 imshow( "Warp + Rotate", warp_rotate_dst );
75 //! [Show what you got]
76
77 //! [Wait until user exits the program]
78 waitKey();
79 //! [Wait until user exits the program]
80
81 return 0;
82 }
83