0
$\begingroup$

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<h*per_y;++cy)
                {
                        for(int cx=-w*per_x;cx<w*per_x;++cx)
                        {
                                uchar* s_p= (uchar*) src->imageData;
                                uchar* t_p= (uchar*) temp->imageData;
                                __int64 suma=0;
                                __int64 sumb=0;
                                for(int y=0;y<h;++y)
                                {
                                        for(int x=0;x<w;++x)
                                        {
                                                suma+= s_p[x];
                                                if((y+cy)<h&&(x+cx)<w&&
                                                        (y+cy)>0&&(x+cx)>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->imageData;
                                t_p= (uchar*) temp->imageData;

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

                                for(int y=0;y<h;++y)
                                {
                                        for(int x=0;x<w;++x)
                                        {
                                                int da = (int)s_p[x] - meana;
                                                int db= 0;
                                                if((y+cy)<h&&(x+cx)<w&&
                                                        (y+cy)>0&&(x+cx)>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>max_peak)
                                {
                                        pt.x= cx;
                                        pt.y= cy;
                                        max_peak=peak;
                                }
                        }
                }  
}
$\endgroup$
2
$\begingroup$

At the start of the inner loop, you assign

uchar* t_p= (uchar*) temp->imageData;

later on, you use it as

t_p[x+cx];

Aren't you missing the cy here?

$\endgroup$
1
$\begingroup$

Isn't that what the following function in OpenCV does? Be sure to check out the method param.

void cvMatchTemplate(const CvArr* image, const CvArr* templ, CvArr* result, int method)

$\endgroup$
  • $\begingroup$ cvMatchTemplate use DFT, I need to do it without fft. $\endgroup$ – mrgloom Dec 12 '12 at 9:35
  • 2
    $\begingroup$ Why would you not want to use FFT? Its just a faster implementation of DFT and shows no performance or quality loss. Is this a homework problem? $\endgroup$ – Naresh Dec 12 '12 at 9:56
  • $\begingroup$ hmm. Had to go read the source. You are correct it does use fft. Just curious, why is that a limitation? $\endgroup$ – Zigner Dec 12 '12 at 9:56
  • $\begingroup$ I want to check my fft implementation works just like original implementation and I want to do performance comparision.Also maybe it will be simpler to parallelize code without fft. $\endgroup$ – mrgloom Dec 12 '12 at 10:10
  • $\begingroup$ @mrgloom Im not sure you will be able to beat doing a correlation without using the FFT. $\endgroup$ – Spacey Dec 13 '12 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.