image correlation without fft I want to stitch 2 images by crosscorrelation measure, but I don't want to use fft.

I tryed this but something seems to be wrong.any suggestions?

void test(IplImage* src,IplImage* temp)
{
double max_peak=0;
                Point pt;
                double per_x=0.5;
                double per_y=0.5;
                for(int cy=-h*per_y;cy&lt;h*per_y;++cy)
                {
                        for(int cx=-w*per_x;cx&lt;w*per_x;++cx)
                        {
                                uchar* s_p= (uchar*) src-&gt;imageData;
                                uchar* t_p= (uchar*) temp-&gt;imageData;
                                __int64 suma=0;
                                __int64 sumb=0;
                                for(int y=0;y&lt;h;++y)
                                {
                                        for(int x=0;x&lt;w;++x)
                                        {
                                                suma+= s_p[x];
                                                if((y+cy)&lt;h&amp;&amp;(x+cx)&lt;w&amp;&amp;
                                                        (y+cy)&gt;0&amp;&amp;(x+cx)&gt;0)
                                                        sumb+= t_p[x+cx];
                                        }
                                        s_p+=w;
                                        t_p+=w;
                                }
                                int meana = (int)(suma / (w * h)),
                                        meanb = (int)(sumb / (w * h)); 


                                s_p= (uchar*) src-&gt;imageData;
                                t_p= (uchar*) temp-&gt;imageData;

                                __int64 corr = 0,
                                        denoma = 0,
                                        denomb = 0;

                                for(int y=0;y&lt;h;++y)
                                {
                                        for(int x=0;x&lt;w;++x)
                                        {
                                                int da = (int)s_p[x] - meana;
                                                int db= 0;
                                                if((y+cy)&lt;h&amp;&amp;(x+cx)&lt;w&amp;&amp;
                                                        (y+cy)&gt;0&amp;&amp;(x+cx)&gt;0)
                                                        db = (int)t_p[x+cx] - meanb; 
                                                else
                                                        db= -meanb;

                                                corr += da * db;
                                                denoma += da * da;
                                                denomb += db * db;
                                        }
                                        s_p+=w;
                                        t_p+=w;
                                }

                                double peak= corr / sqrt( (double)denoma * denomb);
                                if (peak&gt;max_peak)
                                {
                                        pt.x= cx;
                                        pt.y= cy;
                                        max_peak=peak;
                                }
                        }
                }  
}
At the start of the inner loop, you assign

uchar* t_p= (uchar*) temp-&gt;imageData;


later on, you use it as

t_p[x+cx];


Aren't you missing the cy here?