Skip to content

Commit 1f66286

Browse files
committed
More comments in alignment code
1 parent 217c616 commit 1f66286

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

ImageAlignment-FeatureBased/align.cpp

+26-6
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ void alignImages(Mat &im1, Mat &im2, Mat &im1Reg, Mat &h)
4848

4949

5050
// Extract location of good matches
51-
std::vector<Point2f> points1;
52-
std::vector<Point2f> points2;
51+
std::vector<Point2f> points1, points2;
5352

5453
for( size_t i = 0; i < matches.size(); i++ )
5554
{
@@ -60,19 +59,40 @@ void alignImages(Mat &im1, Mat &im2, Mat &im1Reg, Mat &h)
6059
// Find homography
6160
h = findHomography( points1, points2, RANSAC );
6261

63-
// Use homography
62+
// Use homography to warp image
6463
warpPerspective(im1, im1Reg, h, im2.size());
6564

6665
}
6766

6867

6968
int main(int argc, char **argv)
7069
{
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.
7384
Mat imReg, h;
7485

86+
// Align images
87+
cout << "Aligning images ..." << endl;
7588
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;
7797

7898
}

ImageAlignment-FeatureBased/align.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import cv2
23
import numpy as np
34

@@ -51,9 +52,27 @@ def alignImages(im1, im2):
5152

5253

5354
if __name__ == '__main__':
54-
im = cv2.imread("scanned-form.jpg", cv2.IMREAD_COLOR)
55-
imReference = cv2.imread("form.jpg", cv2.IMREAD_COLOR)
5655

56+
# Read reference image
57+
refFilename = "form.jpg"
58+
print("Reading reference image : ", refFilename)
59+
imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR)
60+
61+
# Read image to be aligned
62+
imFilename = "scanned-form.jpg"
63+
print("Reading image to align : ", imFilename);
64+
im = cv2.imread(imFilename, cv2.IMREAD_COLOR)
65+
66+
print("Aligning images ...")
67+
# Registered image will be resotred in imReg.
68+
# The estimated homography will be stored in h.
5769
imReg, h = alignImages(im, imReference)
58-
cv2.imwrite("aligned.jpg", imReg)
70+
71+
# Write aligned image to disk.
72+
outFilename = "aligned.jpg"
73+
print("Saving aligned image : ", outFilename);
74+
cv2.imwrite(outFilename, imReg)
5975

76+
# Print estimated homography
77+
print("Estimated homography : \n", h)
78+

0 commit comments

Comments
 (0)