www.pudn.com > Face3DModel.zip > matchingpoints.cpp


// MatchingPoints.cpp 
// 
////////////////////////////////////////////////////////////////////// 
 
 
#include "stdafx.h" 
#include "global.h" 
#include "memory.h" 
#include "math.h" 
//#include "CornerMatch.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
double average(int nWidth, int nHeight, BYTE *pbImage, POINT point, int windowSize) 
{ 
    int i, j; 
    int sum; 
 
    sum = 0; 
    for (i = point.y - windowSize; i <= point.y + windowSize; i++) 
    { 
        for (j = point.x - windowSize; j <= point.x + windowSize; j++) 
        { 
            sum += pbImage[i*nWidth+j]; 
        } 
 
    } 
    return (double)sum / (2 * windowSize + 1) / (2 * windowSize + 1); 
} 
 
double Correlation(int nWidth, int nHeight, BYTE *pbImage1, BYTE *pbImage2, POINT point1, POINT point2, int windowSize) 
{ 
    //use zero mean cross-correlation in "Effective Corner Matching" 
    //P.Smith, D.Sinclair, R.Cipolla and K.Wood  
    //use fast cross correlation in "A Fast Stereo Matching Method" 
    //Changming Sun 
    int i, j; 
    int ori1, ori2; //original displacement of the points 
    int disp;   //displacement from the original point 
    double aver1, aver2;    //average values around point1 and point2 
    int numInt; 
    int sum1, sum2;         //num: numerator; sum1: sum(i^2); sum2: sum(j^2) 
    double num;             //use the expression in the paper 
                                 
    aver1 = average(nWidth, nHeight, pbImage1, point1, windowSize); 
    aver2 = average(nWidth, nHeight, pbImage2, point2, windowSize); 
    numInt = 0; 
    sum1 = 0; 
    sum2 = 0; 
    ori1 = point1.y * nWidth + point1.x; 
    ori2 = point2.y * nWidth + point2.x; 
 
    for (i = -windowSize; i <= windowSize; i++) 
    { 
        for (j = -windowSize; j <= windowSize; j++) 
        { 
            disp = i * nWidth + j; 
 
            //num += (pbImage1[ori1+disp] - aver1) * (pbImage2[ori2+disp] - aver2); 
            numInt += pbImage1[ori1+disp] * pbImage2[ori2+disp];          
            sum1 += pbImage1[ori1+disp] * pbImage1[ori1+disp]; 
            sum2 += pbImage2[ori2+disp] * pbImage2[ori2+disp]; 
        } 
    } 
 
    int total = (2 * windowSize + 1) * (2 * windowSize + 1);    //the number of elements in the window 
    num = numInt - total * aver1 * aver2; 
     
    if (num < 0)  
    { 
        return 0; 
    } 
    else  
    { 
        return num * num / (sum1 - total * aver1 * aver1) / (sum2 - total * aver2 * aver2); 
    } 
} 
 
BOOL OutRange(int nWidth, int nHeight, POINT point, int windowSize) 
{ 
    if (point.x > nWidth - 1 - windowSize || point.x < windowSize 
        || point.y > nHeight - 1 -windowSize || point.y < windowSize) 
    { 
        return TRUE; 
    } 
 
    return FALSE; 
}