@@ -48,8 +48,7 @@ void alignImages(Mat &im1, Mat &im2, Mat &im1Reg, Mat &h)
48
48
49
49
50
50
// Extract location of good matches
51
- std::vector<Point2f> points1;
52
- std::vector<Point2f> points2;
51
+ std::vector<Point2f> points1, points2;
53
52
54
53
for ( size_t i = 0 ; i < matches.size (); i++ )
55
54
{
@@ -60,19 +59,40 @@ void alignImages(Mat &im1, Mat &im2, Mat &im1Reg, Mat &h)
60
59
// Find homography
61
60
h = findHomography ( points1, points2, RANSAC );
62
61
63
- // Use homography
62
+ // Use homography to warp image
64
63
warpPerspective (im1, im1Reg, h, im2.size ());
65
64
66
65
}
67
66
68
67
69
68
int main (int argc, char **argv)
70
69
{
71
- Mat imReference = imread (" form.jpg" );
72
- Mat im = imread (" scanned-form.jpg" );
70
+ // Read reference image
71
+ string refFilename (" form.jpg" );
72
+ cout << " Reading reference image : " << refFilename << endl;
73
+ Mat imReference = imread (refFilename);
74
+
75
+
76
+ // Read image to be aligned
77
+ string imFilename (" scanned-form.jpg" );
78
+ cout << " Reading image to align : " << imFilename << endl;
79
+ Mat im = imread (imFilename);
80
+
81
+
82
+ // Registered image will be resotred in imReg.
83
+ // The estimated homography will be stored in h.
73
84
Mat imReg, h;
74
85
86
+ // Align images
87
+ cout << " Aligning images ..." << endl;
75
88
alignImages (im, imReference, imReg, h);
76
- imwrite (" aligned.jpg" , imReg);
89
+
90
+ // Write aligned image to disk.
91
+ string outFilename (" aligned.jpg" );
92
+ cout << " Saving aligned image : " << outFilename << endl;
93
+ imwrite (outFilename, imReg);
94
+
95
+ // Print estimated homography
96
+ cout << " Estimated homography : \n " << h << endl;
77
97
78
98
}
0 commit comments