Skip to content

Commit

Permalink
More comments in alignment code
Browse files Browse the repository at this point in the history
  • Loading branch information
spmallick committed Mar 9, 2018
1 parent 217c616 commit 1f66286
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
32 changes: 26 additions & 6 deletions ImageAlignment-FeatureBased/align.cpp
Expand Up @@ -48,8 +48,7 @@ void alignImages(Mat &im1, Mat &im2, Mat &im1Reg, Mat &h)


// Extract location of good matches
std::vector<Point2f> points1;
std::vector<Point2f> points2;
std::vector<Point2f> points1, points2;

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

// Use homography
// Use homography to warp image
warpPerspective(im1, im1Reg, h, im2.size());

}


int main(int argc, char **argv)
{
Mat imReference = imread("form.jpg");
Mat im = imread("scanned-form.jpg");
// Read reference image
string refFilename("form.jpg");
cout << "Reading reference image : " << refFilename << endl;
Mat imReference = imread(refFilename);


// Read image to be aligned
string imFilename("scanned-form.jpg");
cout << "Reading image to align : " << imFilename << endl;
Mat im = imread(imFilename);


// Registered image will be resotred in imReg.
// The estimated homography will be stored in h.
Mat imReg, h;

// Align images
cout << "Aligning images ..." << endl;
alignImages(im, imReference, imReg, h);
imwrite("aligned.jpg", imReg);

// Write aligned image to disk.
string outFilename("aligned.jpg");
cout << "Saving aligned image : " << outFilename << endl;
imwrite(outFilename, imReg);

// Print estimated homography
cout << "Estimated homography : \n" << h << endl;

}
25 changes: 22 additions & 3 deletions ImageAlignment-FeatureBased/align.py
@@ -1,3 +1,4 @@
from __future__ import print_function
import cv2
import numpy as np

Expand Down Expand Up @@ -51,9 +52,27 @@ def alignImages(im1, im2):


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

# Read reference image
refFilename = "form.jpg"
print("Reading reference image : ", refFilename)
imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR)

# Read image to be aligned
imFilename = "scanned-form.jpg"
print("Reading image to align : ", imFilename);
im = cv2.imread(imFilename, cv2.IMREAD_COLOR)

print("Aligning images ...")
# Registered image will be resotred in imReg.
# The estimated homography will be stored in h.
imReg, h = alignImages(im, imReference)
cv2.imwrite("aligned.jpg", imReg)

# Write aligned image to disk.
outFilename = "aligned.jpg"
print("Saving aligned image : ", outFilename);
cv2.imwrite(outFilename, imReg)

# Print estimated homography
print("Estimated homography : \n", h)

0 comments on commit 1f66286

Please sign in to comment.