Click here to Skip to main content
15,886,844 members
Articles / Programming Languages / C

Image Alignment Algorithms - Part II

Rate me:
Please Sign up or sign in to vote.
4.13/5 (23 votes)
21 Apr 2008CPOL11 min read 93.6K   4.9K   75  
Implementing and comparing the forwards compositional and the Hager-Belhumeur algorithms.
// Module: main.cpp
// Brief:  
// Author: Oleg A. Krivtsov
// Email:  olegkrivtsov@mail.ru
// Date:  April 2008

#include <stdio.h>
#include <cv.h>			// Include header for computer-vision part of OpenCV.
#include <highgui.h>	// Include header for GUI part of OpenCV.
#include "auxfunc.h"    // Header for our warping functions.
#include "forwadditive.h"  // Include header for Lucas-Kanade algorithm
#include "invcomp.h"       // Include header for Baker-Dellaert-Matthews algorithm
#include "forwcomp.h"      // Include header for forwards compositional algorithm
#include "invadditive.h"   // Include header for Hager-Belhumeur algorithm

// Our plan:
// 
// 1. Ask user to enter image warp parameter vector p=(wz, tx, ty, s)
// 2. Define our template rectangle to be bounding rectangle of the butterfly
// 3. Warp image I with warp matrix W(p)
// 4. Show template T and image T, wait for a key press
// 5. Estimate warp parameters using Lucas-Kanade method, wait for a key press
// 6. Estimate warp parameters using Hager-Belhumeur method, wait for a key press
// 7. Estimate warp parameters using forwads compositional method, wait for a key press
// 8. Estimate warp parameters using Baker-Dellaert-Matthews, wait for a key press
//

int main(int argc, char* argv[])
{
	// Ask user to enter image warp paremeters vector.
	// p = (wz, tx, ty, s),
	
	float WZ=0, TX=0, TY=0, S=1;
	printf("Please enter WZ, TX, TY and S, separated by space.\n");
	printf("Example: 0.01 0 3 1.01\n");
	printf(">");
	scanf("%f %f %f %f", &WZ, &TX, &TY, &S);

	// Here we will store our images.
	IplImage* pColorPhoto = 0; // Photo of a butterfly on a flower.
	IplImage* pGrayPhoto = 0;  // Grayscale copy of the photo.
	IplImage* pImgT = 0;	   // Template T.
	IplImage* pImgI = 0;	   // Image I.

	// Here we will store our warp matrix.
	CvMat* W = 0;  // Warp W(p)
	
	// Create two simple windows. 
	cvNamedWindow("template"); // Here we will display T(x).
	cvNamedWindow("image"); // Here we will display I(x).

	// Load photo.
	pColorPhoto = cvLoadImage("..\\data\\photo.jpg");

	// Create other images.
	CvSize photo_size = cvSize(pColorPhoto->width,pColorPhoto->height);
	pGrayPhoto = cvCreateImage(photo_size, IPL_DEPTH_8U, 1);
	pImgT = cvCreateImage(photo_size, IPL_DEPTH_8U, 1);
	pImgI = cvCreateImage(photo_size, IPL_DEPTH_8U, 1);

	// Convert photo to grayscale, because we need intensity values instead of RGB.	
	cvCvtColor(pColorPhoto, pGrayPhoto, CV_RGB2GRAY);
	
	// Create warp matrix, we will use it to create image I.
	W = cvCreateMat(3, 3, CV_32F);

	// Create template T
	// Set region of interest to butterfly's bounding rectangle.
	cvCopy(pGrayPhoto, pImgT);
	CvRect omega = cvRect(110, 100, 200, 150);
	
	// Create I by warping photo with sub-pixel accuracy. 
	init_warp(W, WZ, TX, TY, S);
	warp_image(pGrayPhoto, pImgI, W, 0.5);
	
	// Draw the initial template position approximation rectangle.
	cvSetIdentity(W);
	draw_warped_rect(pImgI, omega, W);

	// Show T and I and wait for key press.
	cvSetImageROI(pImgT, omega);
	cvShowImage("template", pImgT);
	cvShowImage("image", pImgI);
	printf("Press any key to start Lucas-Kanade algorithm.\n");
	cvWaitKey(0);
	cvResetImageROI(pImgT);

	// Print what parameters were entered by user.
	printf("==========================================================\n");
	printf("Ground truth:  WZ=%f TX=%f TY=%f S=%f\n", WZ, TX, TY, S);
	printf("==========================================================\n");

	init_warp(W, WZ, TX, TY, S);

	// Restore image I
	warp_image(pGrayPhoto, pImgI, W, 0.5);

	/* Lucas-Kanade */	
	align_image_forwards_additive(pImgT, omega, pImgI);

	// Restore image I
	warp_image(pGrayPhoto, pImgI, W, 0.5);

	printf("Press any key to start Hager-Belhumeur algorithm.\n");
	cvWaitKey();

	/* Hager-Belhumeur*/
	align_image_inverse_additive(pImgT, omega, pImgI);

	// Restore image I
	warp_image(pGrayPhoto, pImgI, W, 0.5);

	printf("Press any key to start forwards compositional algorithm.\n");
	cvWaitKey();

	/* Forwards compositional*/
	align_image_forwards_compositional(pImgT, omega, pImgI);

	// Restore image I
	warp_image(pGrayPhoto, pImgI, W, 0.5);

	printf("Press any key to start Baker-Dellaert-Matthews algorithm.\n");
	cvWaitKey();

	/* Baker-Dellaert-Matthews*/
	align_image_inverse_compositional(pImgT, omega, pImgI);
	
	printf("Press any key to exit the demo.\n");
	cvWaitKey();

	// Release all used resources and exit
	cvDestroyWindow("template");
	cvDestroyWindow("image");
	cvReleaseImage(&pImgT);
	cvReleaseImage(&pImgI);
	cvReleaseMat(&W);
	
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions