Ask Your Question
1

phasecorr.cpp function magSpectrums() DC component missed the square root operation

asked 2016-11-09 04:19:37 -0600

lowoodz gravatar image

There is a possible bug in function magSpectrums(), it seems that the calculation of magnitude of DC component (the first element) is wrong, it has missed the square root operation.

    if( !is_1d && cn == 1 )
    {
        for( k = 0; k < (cols % 2 ? 1 : 2); k++ )
        {
            if( k == 1 )
                dataSrc += cols - 1, dataDst += cols - 1;
            dataDst[0] = dataSrc[0]*dataSrc[0]; /// DC component
            if( rows % 2 == 0 )
                dataDst[(rows-1)*stepDst] = dataSrc[(rows-1)*stepSrc]*dataSrc[(rows-1)*stepSrc]; /// DC component

            for( j = 1; j <= rows - 2; j += 2 )
            {
                dataDst[j*stepDst] = (float)std::sqrt((double)dataSrc[j*stepSrc]*dataSrc[j*stepSrc] +
                                                      (double)dataSrc[(j+1)*stepSrc]*dataSrc[(j+1)*stepSrc]);
            }

            if( k == 1 )
                dataSrc -= cols - 1, dataDst -= cols - 1;
        }
    }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-09 10:20:03 -0600

LBerger gravatar image

updated 2016-11-09 11:11:00 -0600

Yes it's a bug. Now you can make an issue and attach this source file :

static void magSpectrums(InputArray _src, OutputArray _dst)
{

 copy of https://github.com/opencv/opencv/blob/master/modules/imgproc/src/phasecorr.cpp#L43-L154
 }

int main(int argc, char* argv[])
{
    Mat img_gray = (Mat_<double>(7, 7) <<
        0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0,
        0, 0, 1, 2, 1, 0, 0,
        0, 0, 28, 3, 12, 0, 0,
        0, 0, 1, 32, 11, 0, 0,
        0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0);
    Mat dst,dstmag,C;
    dft(img_gray,dst, DFT_REAL_OUTPUT);
    cout<<dst<<"\n";
    magSpectrums(dst, dstmag);
    cout << dstmag << "\n";
}

results :

[91, -61.63472896073699, -34.88832597398297, 11.00381187631017, 23.18032144561113, 5.130917084426835, 10.78088161567718;
 -79.27437524333573, 46.39923994314788, 24.99073220976408, -13.16493292356233, -2.875798188950187, 17.00825525377055, -19.80872293253553;
 -3.465827909609736, -22.95419807523526, -5.848067168416643, 14.03408882148513, -19.64791112908571, -36.02086931060224, 25.02980993212081;
 50.63973729010014, -0.6313910288281832, -14.4528792974728, -14.02459017941099, 37.36465232426451, 48.066671235367, -25.08331899656442;
 0.9537034740022036, 23.11153972637843, 27.59590549379765, 10.76784891431971, -45.62384962092409, -50.42471662106156, 19.95865199940767;
 -16.86536204676441, -44.98366507435955, -33.36161345721872, -3.268486848572098, 44.84668396036388, 42.5533931971839, -10.99753539452452;
 4.102253842053943, 60.69320346963365, 35.96424819352938, -5.347739660569584, -37.24409879127953, -26.31365083908447, 0.12023377641879]
[8281, 70.82397265990102, 0, 25.65952412129041, 0, 11.93958619628459, 0;
 79.35010102904866, 52.70129185970634, 0, 13.47537287445006, 0, 26.10873897750277, 0;
 0, 23.68744601858644, 0, 24.14531136236979, 0, 43.86336069123728, 0;
 50.64871709260446, 14.46666425333092, 0, 39.90997836398306, 0, 54.21787321107557, 0;
 0, 35.99551733961577, 0, 46.87731033745762, 0, 54.23098593929324, 0;
 17.35710009054185, 56.0047085162581, 0, 44.96563208184722, 0, 43.95153077365971, 0;
 0, 70.54850881154138, 0, 37.62607093827123, 0, 26.31392552703308, 0]

8281 should be 91. Now bug means when there is a shift between your image phaseCorrelate could give you a small bias when gaussian centroid is calculated

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-11-09 04:10:35 -0600

Seen: 459 times

Last updated: Nov 09 '16