Ask Your Question

Revision history [back]

Yes it's a bug. Now you can make an issue. You ca attached this source file :

static void magSpectrums(InputArray _src, OutputArray _dst) { Mat src = _src.getMat(); int depth = src.depth(), cn = src.channels(), type = src.type(); int rows = src.rows, cols = src.cols; int j, k;

CV_Assert(type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2);

if (src.depth() == CV_32F)
    _dst.create(src.rows, src.cols, CV_32FC1);
else
    _dst.create(src.rows, src.cols, CV_64FC1);

Mat dst = _dst.getMat();
dst.setTo(0);//Mat elements are not equal to zero by default!

bool is_1d = (rows == 1 || (cols == 1 && src.isContinuous() && dst.isContinuous()));

if (is_1d)
    cols = cols + rows - 1, rows = 1;

int ncols = cols*cn;
int j0 = cn == 1;
int j1 = ncols - (cols % 2 == 0 && cn == 1);

if (depth == CV_32F)
{
    const float* dataSrc = src.ptr<float>();
    float* dataDst = dst.ptr<float>();

    size_t stepSrc = src.step / sizeof(dataSrc[0]);
    size_t stepDst = dst.step / sizeof(dataDst[0]);

    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];
            if (rows % 2 == 0)
                dataDst[(rows - 1)*stepDst] = dataSrc[(rows - 1)*stepSrc] * dataSrc[(rows - 1)*stepSrc];

            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;
        }
    }

    for (; rows--; dataSrc += stepSrc, dataDst += stepDst)
    {
        if (is_1d && cn == 1)
        {
            dataDst[0] = dataSrc[0] * dataSrc[0];
            if (cols % 2 == 0)
                dataDst[j1] = dataSrc[j1] * dataSrc[j1];
        }

        for (j = j0; j < j1; j += 2)
        {
            dataDst[j] = (float)std::sqrt((double)dataSrc[j] * dataSrc[j] + (double)dataSrc[j + 1] * dataSrc[j + 1]);
        }
    }
}
else
{
    const double* dataSrc = src.ptr<double>();
    double* dataDst = dst.ptr<double>();

    size_t stepSrc = src.step / sizeof(dataSrc[0]);
    size_t stepDst = dst.step / sizeof(dataDst[0]);

    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];
            if (rows % 2 == 0)
                dataDst[(rows - 1)*stepDst] = dataSrc[(rows - 1)*stepSrc] * dataSrc[(rows - 1)*stepSrc];

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

            if (k == 1)
                dataSrc -= cols - 1, dataDst -= cols - 1;
        }
    }

    for (; rows--; dataSrc += stepSrc, dataDst += stepDst)
    {
        if (is_1d && cn == 1)
        {
            dataDst[0] = dataSrc[0] * dataSrc[0];
            if (cols % 2 == 0)
                dataDst[j1] = dataSrc[j1] * dataSrc[j1];
        }

        for (j = j0; j < j1; j += 2)
        {
            dataDst[j] = std::sqrt(dataSrc[j] * dataSrc[j] + dataSrc[j + 1] * dataSrc[j + 1]);
        }
    }
}

}

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&lt;&lt;"\n"; magspectrums2(dst,="" dstmag);="" cout="" &lt;&lt;="" dstmag="" &lt;&lt;="" "\n";="" }<="" p="">

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 tiny shift between your image phaseCorrelate could give you a small shift...

Yes it's a bug. Now you can make an issue. You ca attached this source file :

static void magSpectrums(InputArray _src, OutputArray _dst)
{
 Mat src = _src.getMat();
int depth = src.depth(), cn = src.channels(), type = src.type();
int rows = src.rows, cols = src.cols;
int j, k;

CV_Assert(type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2);

if (src.depth() == CV_32F)
    _dst.create(src.rows, src.cols, CV_32FC1);
else
    _dst.create(src.rows, src.cols, CV_64FC1);

Mat dst = _dst.getMat();
dst.setTo(0);//Mat elements are not equal to zero by default!

bool is_1d = (rows == 1 || (cols == 1 && src.isContinuous() && dst.isContinuous()));

if (is_1d)
    cols = cols + rows - 1, rows = 1;

int ncols = cols*cn;
int j0 = cn == 1;
int j1 = ncols - (cols % 2 == 0 && cn == 1);

if (depth == CV_32F)
{
    const float* dataSrc = src.ptr<float>();
    float* dataDst = dst.ptr<float>();

    size_t stepSrc = src.step / sizeof(dataSrc[0]);
    size_t stepDst = dst.step / sizeof(dataDst[0]);

    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];
            if (rows % 2 == 0)
                dataDst[(rows - 1)*stepDst] = dataSrc[(rows - 1)*stepSrc] * dataSrc[(rows - 1)*stepSrc];

            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]);
           
copy of https://github.com/opencv/opencv/blob/master/modules/imgproc/src/phasecorr.cpp#L43-L154
 }
 if (k == 1)
dataSrc -= cols - 1, dataDst -= cols - 1;
}
}
for (; rows--; dataSrc += stepSrc, dataDst += stepDst)
{
if (is_1d && cn == 1)
{
dataDst[0] = dataSrc[0] * dataSrc[0];
if (cols % 2 == 0)
dataDst[j1] = dataSrc[j1] * dataSrc[j1];
}
for (j = j0; j < j1; j += 2)
{
dataDst[j] = (float)std::sqrt((double)dataSrc[j] * dataSrc[j] + (double)dataSrc[j + 1] * dataSrc[j + 1]);
}
}
}
else
{
const double* dataSrc = src.ptr<double>();
double* dataDst = dst.ptr<double>();
size_t stepSrc = src.step / sizeof(dataSrc[0]);
size_t stepDst = dst.step / sizeof(dataDst[0]);
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];
if (rows % 2 == 0)
dataDst[(rows - 1)*stepDst] = dataSrc[(rows - 1)*stepSrc] * dataSrc[(rows - 1)*stepSrc];
for (j = 1; j <= rows - 2; j += 2)
{
dataDst[j*stepDst] = std::sqrt(dataSrc[j*stepSrc] * dataSrc[j*stepSrc] +
dataSrc[(j + 1)*stepSrc] * dataSrc[(j + 1)*stepSrc]);
}
if (k == 1)
dataSrc -= cols - 1, dataDst -= cols - 1;
}
}
for (; rows--; dataSrc += stepSrc, dataDst += stepDst)
{
if (is_1d && cn == 1)
{
dataDst[0] = dataSrc[0] * dataSrc[0];
if (cols % 2 == 0)
dataDst[j1] = dataSrc[j1] * dataSrc[j1];
}
for (j = j0; j < j1; j += 2)
{
dataDst[j] = std::sqrt(dataSrc[j] * dataSrc[j] + dataSrc[j + 1] * dataSrc[j + 1]);
}
}
}

}

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&lt;&lt;"\n"; magspectrums2(dst,="" dstmag);="" cout="" &lt;&lt;="" dstmag="" &lt;&lt;="" "\n";="" }<="" p="">

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]

0]

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

Yes it's a bug. Now you can make an issue. You ca attached 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 tiny shift between your image phaseCorrelate could give you a small shift...

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 tiny a shift between your image phaseCorrelate could give you a small shift...bias when gaussian centroid is calculated