Need help and suggestions for image comparison method

Things to note:
I am using C++, Qt, and OpenCV.

Background info:
I am trying to create a program (actually a method) in C++ that takes two (filtered to two color black & white) images from a moving robot, one within X amount of time of the other, then trys to find the amount of shift between each frame. It does this by overlaying the second picture ontop of the first, in every possible placement, and comparing the overlapping area, pixel per pixel.

I’ve gotten all of the bugs out (to the extent of my knowledge), with the exception of one. There is a method that takes two images, one from the overlapping area of each frame, counts each matching pixel, and returns a “score” of how similar the two images that were passed in. My issue with this is the “score” returned. This score only returns the number of matching pixels. If I were to give it larger images, it would return a larger score than if I gave it smaller ones, even if the smaller ones were more similar. I’ve already tried dividing the score by the area of the images.

The program does that for many different images to figure out which is the best match, which is dictated by the “score”.

I was wondering what the best way to calculate the “score” is, that would always give the pair of images with the most in common the best score, regardless of size or shape.

Here is the troublesome code:

*double ImgCompare (Mat Img1, Mat Img2, int ImgWidth, int ImgHeight) {

double Matches = 0;

double ImgArea = ImgWidth * ImgHeight;

double MatchesY = 0;

for (int Y = -1; Y < abs(ImgHeight-2); Y=Y) {

    double MatchesX = 0;

    for (int X = -1; X < abs(ImgWidth-2); X=X) {

        if (<double>(X,Y) ==<double>(X,Y) ) {





    MatchesY += MatchesX/ImgWidth;



Score = MatchesY/ImgHeight;

//printf("Matches = %f

", Matches);